2

I'm new to python and after installing it I've accomplished to plot my 3d data using matplotlib. Sadly the only thing I don't know how to get done is the color part. My image just shows the surface but doesn't use the color bar at all. Here is my code.

from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import matplotlib.pyplot as plt
from matplotlib.mlab import griddata
import numpy as np

fig = plt.figure()
ax = fig.gca(projection='3d')

data = np.genfromtxt('Uizq.txt')
x = data[:,0]
y = data[:,1]
z = data[:,2]

xi = np.linspace(min(x), max(x))
yi = np.linspace(min(y), max(y))

ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('U')

X, Y = np.meshgrid(xi, yi)
Z = griddata(x, y, z, xi, yi)
ax.set_zlim3d(np.min(Z), np.max(Z))


surf = ax.plot_surface(X, Y, Z, rstride=2, cstride=2, cmap=cm.jet,
                   linewidth=0.5, antialiased=False)

fig.colorbar(surf, shrink=0.5, aspect=5)

plt.show()

plot

you can obviously see that it is all blue, and I want to relate the color with "U" using the full cm.jet spectrum. This might be a very noob question, so sorry if you rolled your eyes.

2
  • 1
    Does the code here matplotlib.org/mpl_toolkits/mplot3d/tutorial.html produce what you expect? Can you provide the data file you use? (these questions are to sort out if this is due to something in your data messing with the colormap) What version of matplotlib are you using? Commented Oct 30, 2012 at 14:57
  • That is what I am looking for, the first one below 'Surface plots'. Here is my data s000.tinyupload.com/?file_id=14963344006410044490. I'm using the last version, 1.1.1. Commented Oct 30, 2012 at 15:04

1 Answer 1

2

Add the line

surf.set_clim([np.min(Z),np.max(Z)])

before you add the color bar.

It seems that the 3D plotting does not take into account the masking, so you are including NaN in the data, which confuses the automatic color limits.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, that solved the problem, although the boundaries of the surface are a little strange, but it does the job. By the way, what do you mean with "the masking"?
A masked array is an array which carries with it a mask of what data is valid. griddata uses this to mark points which it can not extrapolate (due to being outside of your data) and returns NaN which is then masked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.