Matplotlib: imshow in 3d story

In the chart below, taken from the matplotlib gallery, contourf is used to create a 2d chart under 3d. My question is, can imshow be used to do the same? I would like the colors on the 2nd plot to be smoother.

Creating a 2d graph seems possible because contourf accepts the zdir argument while I was watching, but imshow did not. This suggests that this is impossible, but why not? pcolor will also do its job, is this possible with this?

+7
source share
2 answers

Just specify the levels = parameter for the path, for example.

from mpl_toolkits.mplot3d import axes3d import matplotlib.pyplot as plt,numpy as np plt.clf() fig = plt.figure(1) ax = fig.gca(projection='3d') X, Y, Z = axes3d.get_test_data(0.05) ax.plot_surface(X, Y, Z, rstride=8, cstride=8, alpha=0.3) cset = ax.contourf(X, Y, Z, zdir='z', offset=-100, levels=np.linspace(-100,100,1200),cmap=plt.cm.jet) cset = ax.contourf(X, Y, Z, zdir='x', offset=-40, cmap=plt.cm.jet) cset = ax.contourf(X, Y, Z, zdir='y', offset=40, cmap=plt.cm.jet) ax.set_xlabel('X') ax.set_xlim(-40, 40) ax.set_ylabel('Y') ax.set_ylim(-40, 40) ax.set_zlabel('Z') ax.set_zlim(-100, 100) plt.show() 

The image

+11
source

A bit longer code, then sega_sai answer, but faster and for my experience much better for more complex surfaces.

Use plot_surface to build a flat surface where you want, and facecolors to color it with the values ​​you want

You may need to make your data smoother with scipy zoom.

 from mpl_toolkits.mplot3d import axes3d import matplotlib.pyplot as plt,numpy as np plt.clf() fig = plt.figure(1) ax = fig.gca(projection='3d') X, Y, Z = axes3d.get_test_data(0.05) ax.plot_surface(X, Y, Z, rstride=8, cstride=8, alpha=0.3) cset = ax.contourf(X, Y, Z, zdir='x', offset=-40, cmap=plt.cm.jet) cset = ax.contourf(X, Y, Z, zdir='y', offset=40, cmap=plt.cm.jet) ### strating here: # normalize Z to [0..1] Z=ZZ.min() Z=Z/Z.max() #use zoom to make your data smoother from scipy.ndimage.interpolation import zoom #make data 5 times smoother X=zoom(X,5) Y=zoom(Y,5) Z=zoom(Z,5) #draw a surface at -100, using the facecolors command to color it with the values of Z cset = ax.plot_surface(X, Y, np.zeros_like(Z)-100,facecolors=plt.cm.jet(Z),shade=False) ax.set_xlabel('X') ax.set_xlim(-40, 40) ax.set_ylabel('Y') ax.set_ylim(-40, 40) ax.set_zlabel('Z') ax.set_zlim(-100, 100) plt.show() 

ready image

It also makes it difficult to create a color bar so that:

 cb = plt.cm.ScalarMappable(cmap=plt.cm.jet) cb.set_array(Z) plt.colorbar(cb) plt.show() 
+3
source

All Articles