if I'm plotting a contour like this, how could I change the color bar for "jet" colour? Is it necessary to call a figure first?
plt.contourf(X,Y,Z)
plt.colorbar().set_label(label='values',size=12)
plt.grid(True)
You can supply a parameter to the contourf function:
plt.contourf(X, Y, Z, cmap=plt.cm.jet)
or
plt.contourf(X, Y, Z, cmap='jet')
Related
I have this set of data, which I would like to plot it in a 3D surface and in a contour by using python (numpy and matplotlib, basically). In this case, I have the X and Y axis as my free coordinates, and the results are described as a dataset on the z-axis.
I've checked many times the data, and it is formatted correctly to my needs.
Here is a plot of the set in a scatter manner
The problem is when I try to use the surface plot, where the end points of the (X, Y) mesh are connected, closing the surface and making not visible the part of the image that contains the information I would like to have. Here is the output I have regarding the surface plot and here is the output regarding the contour plot.
The code related to the surface plot (only ploting part) is
#...
#X and Y are the coordinates values
#E is the results I am trying to plot
fig = plt.figure()
ax = fig.gca(projection='3d')
surf = ax.plot_surface(X, Y, E, cmap=cm.coolwarm, linewidth=0, antialiased=False)
ax.scatter3D(X, Y, E, cmap='Greens')
fig.colorbar(surf, shrink=0.5, aspect=5)
plt.show()
Here is the code related to the contour plot
fig, ax = plt.subplots()
CS = ax.contourf(X, Y, E, 10, cmap='RdGy')
ax.clabel(CS, inline=1, fontsize=10)
ax.set_title('Simplest default with labels')
fig.colorbar(CS, shrink=0.5, aspect=5)
plt.show()
I have a surface plot, and I need this specific point of view that I have chosen. See the image below:
Now, as you can see, the very bottom part of my axis legend is missing, because matplotlib is cutting it off. Is there any way to programmatically zoom out of the plot so everything fits in the window?
This is my original code:
values_all = zip(*values_all)
x = range(len(values_all[0]))
y = range(len(values_all))
figure = plt.figure(1, figsize=(10, 7))
ax = Axes3D(figure, azim=-124, elev=40, zlim=(0, 0.4))
x, y = np.meshgrid(x, y)
surface = ax.plot_surface(x, y, values_all, linewidth=0, rstride=1, cstride=1, cmap=cm.jet)
plt.colorbar(surface, shrink=0.4, aspect=10)
plt.show()
Call
plt.tight_layout()
before
plt.show()
I have a question regarding the colorbar of the matplotlib. I have a surface-plot, which is working fine, and the colors are used correctly. But somehow, the scale of my colorbar is messed up. I think it should go from 0 to 0.4. But the actual code gives me 0 to 0.16. What am I missing here? Strange that the value 0.16 is the square of 0.4.
Here is my plot:
And of course, here is my code:
values_all = zip(*values_all)
x = range(len(values_all[0]))
y = range(len(values_all))
figure = plt.figure(1, figsize=(10, 7))
ax = Axes3D(figure, azim=-124, elev=40, zlim=(0, 0.4))
x, y = np.meshgrid(x, y)
surface = ax.plot_surface(x, y, values_all, linewidth=0, rstride=1, cstride=1, cmap=cm.jet)
plt.colorbar(surface, shrink=0.4, aspect=10)
plt.show()
If I edit my code the following way, the colorbar is scaled correctly, but the plot itself is not colored correctly anymore:
surface = ax.plot_surface(x, y, values_all, linewidth=0, rstride=1, cstride=1, cmap=cm.jet,vmin=0,vmax=0.4)
Results in:
With other sample data, you can see that this is not only an issue of the plot's borders (The values that are giving the strange peak are between 0.3-0.35):
I am trying to make a grid on the surface of my surfaceplot, now I know that wireframe does not work and the grid command is something else entirely. But how do you plot things with grids like this?
Here is the plot command I am using
fig = plt.figure()
ax = fig.add_subplot(111,projection="3d")
plot = ax.plot_surface(x,y,z, rstride=1, cstride=1, cmap=cm.jet, shade=True,
linewidth=0, antialiased=False)
From the (second) example in the matplotlib surface plot documentation, from which the image in the OP comes from (see the source code here), it is clear that the plot_surface function draws grid lines on surface plots by default. However, the plotting command
plot = ax.plot_surface(x,y,z, rstride=1, cstride=1, cmap=cm.jet, shade=True,
linewidth=0, antialiased=False)
sets the width of the grid lines to zero, so they are not visible, but are present, when using this call. Remove the linewidth=0 argument or set this to a value greater than 0.
When plotting surfaces using mpl_toolkits.mplot3d.Axes3D.plot_surface(), lines appear that seem to follow the curve of the surfaces being plotted. For example:
X, Y = numpy.meshgrid(numpy.arange(some_range), numpy.arange(some_other_range))
Z1, Z2 = numpy.array(getRate())
#getRate is a function that returns an array of shape (len(some_range), len(some_other_range)
fig = pyplot.figure()
ax = mplot3d.Axes3D(fig)
ax.plot_surface(X, Y, Z1, color='w', alpha=0.2)
ax.plot_surface(X, Y, Z2, color='b', alpha=0.2)
pyplot.show()
Is there any way to get rid of the bloody things so you just have a smooth surface? I've attached an image to show what I mean.
Try
ax.plot_surface(X, Y, Z1, color='w', alpha=0.2, linewidth=0)
ax.plot_surface(X, Y, Z2, color='b', alpha=0.2, linewidth=0)
You may want to increase your alpha values a bit, though, if taking away the lines makes parts of the surfaces too hard to see.