Python 在 matplotlib 中以 3d 形式绘制 imshow() 图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30464117/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Plotting a imshow() image in 3d in matplotlib
提问by Raj
How to plot a imshow()
image in 3d axes? I was trying with this post. In that post, the surface plot looks same as imshow()
plot but actually they are not. To demonstrate, here I took different data:
如何imshow()
在 3d 轴中绘制图像?我正在尝试这篇文章。在那篇文章中,表面图看起来与imshow()
图相同,但实际上并非如此。为了演示,这里我采用了不同的数据:
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
# create a 21 x 21 vertex mesh
xx, yy = np.meshgrid(np.linspace(0,1,21), np.linspace(0,1,21))
# create vertices for a rotated mesh (3D rotation matrix)
X = xx
Y = yy
Z = 10*np.ones(X.shape)
# create some dummy data (20 x 20) for the image
data = np.cos(xx) * np.cos(xx) + np.sin(yy) * np.sin(yy)
# create the figure
fig = plt.figure()
# show the reference image
ax1 = fig.add_subplot(121)
ax1.imshow(data, cmap=plt.cm.BrBG, interpolation='nearest', origin='lower', extent=[0,1,0,1])
# show the 3D rotated projection
ax2 = fig.add_subplot(122, projection='3d')
ax2.plot_surface(X, Y, Z, rstride=1, cstride=1, facecolors=plt.cm.BrBG(data), shade=False)
Here are my plots:
这是我的情节:
采纳答案by Ed Smith
I think your error in the 3D vs 2D surface colour is due to data normalisation in the surface colours. If you normalise the data passed to plot_surface
facecolor with, facecolors=plt.cm.BrBG(data/data.max())
the results are closer to what you'd expect.
我认为您在 3D 与 2D 表面颜色中的错误是由于表面颜色的数据标准化。如果您将传递给plot_surface
facecolor的数据标准化, facecolors=plt.cm.BrBG(data/data.max())
则结果更接近您的预期。
If you simply want a slice normal to a coordinate axis, instead of using imshow
, you could use contourf
, which is supported in 3D as of matplotlib 1.1.0,
如果你只是想要一个垂直于坐标轴的切片,而不是使用imshow
,你可以使用contourf
,它在 matplotlib 1.1.0 的 3D 中支持,
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
from matplotlib import cm
# create a 21 x 21 vertex mesh
xx, yy = np.meshgrid(np.linspace(0,1,21), np.linspace(0,1,21))
# create vertices for a rotated mesh (3D rotation matrix)
X = xx
Y = yy
Z = 10*np.ones(X.shape)
# create some dummy data (20 x 20) for the image
data = np.cos(xx) * np.cos(xx) + np.sin(yy) * np.sin(yy)
# create the figure
fig = plt.figure()
# show the reference image
ax1 = fig.add_subplot(121)
ax1.imshow(data, cmap=plt.cm.BrBG, interpolation='nearest', origin='lower', extent=[0,1,0,1])
# show the 3D rotated projection
ax2 = fig.add_subplot(122, projection='3d')
cset = ax2.contourf(X, Y, data, 100, zdir='z', offset=0.5, cmap=cm.BrBG)
ax2.set_zlim((0.,1.))
plt.colorbar(cset)
plt.show()
This code results in this image:
此代码导致此图像:
Although this won't work for a slice at an arbitrary position in 3D where the imshow solutionis better.
尽管这不适用于 imshow解决方案更好的3D 中任意位置的切片。