Plotting a grid on a surfaceplot? - python

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.

Related

End points of 3D surface being connected

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()

How to change colorbar using Matplotlib?

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')

Set separation between ticks when using log scale on matplotlib

This is the chart I did in 3D using Google Sheets.
I want to achieve the same scale on matplotlib but using a 3D surface.
The problem is that the ticks on logscale are being placed where they "should be" if it was a normal scale.
Here is my code:
X, Y = np.meshgrid(numrec, numtreino)
Z = (numerador/(((rec[0])+(treino[0]*60))/((rec[1])+(treino[1]*60))))*X
# Plot the surface.
surf = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm,
linewidth=2, antialiased=True, alpha=0.8)
ax.set_xscale('symlog')
ax.set_yscale('symlog')
ax.invert_xaxis()
# Add a color bar which maps values to colors.
fig.colorbar(surf, shrink=0.5, aspect=5)
plt.show()
As pointed out by ImportanceOfBeingErnest, it seems to be a bug in matplotlib.
If you enable a log scale when doing a 3D scatter plot, nothing is created and the program crashes.
Instead of ax.set_xscale('log'), ax.set_xscale('symlog') should be used.
To fix the scaling problem I've changed:
Z = (numerador/(((rec[0])+(treino[0]*60))/((rec[1])+(treino[1]*60))))*X
# Plot the surface.
to:
Z = (numerador/(((rec[0])+(treino[0]*60))/((rec[1])+(treino[1]*60))))*X
X = np.log10(X)
Y = np.log10(Y)
# Plot the surface.
To set the ticks I've added:
zticks = [ 1e3, 1e2, 1e1]
ax.set_xticks(np.log10(zticks))
ax.set_xticklabels(zticks)

Zoom out in Matplotlib

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()

Python colorbar scale

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):

Categories

Resources