Matplotlib导入的PNG将不会显示在3D图中

时间:2013-09-23 09:41:03

标签: python 3d matplotlib plot

我正在尝试使用matplotlib生成3D图形,其中xy平面是图像,然后在顶部绘制一些3D轨迹(该部分工作得很好)。问题是,即使我导入的PNG在imshow中显示得很好,即使我可以在3D轴上绘制图像,如果我只是使用菜谱中的一个例子,我的图像就会显示为一个无特征的黑盒子。我确定我错过了一些小事 - 提前谢谢!

import matplotlib as mpl
from mpl_toolkits.mplot3d import Axes3D, art3d
from pylab import ogrid
import matplotlib.pyplot as plt

plt.ioff()
fig = plt.figure()
ay=fig.add_subplot(2,1,1)
rawim=plt.imread(r'G:\Path\myimage.png')
ay.imshow(rawim,cmap='gray')
ax=fig.add_subplot(2,1,2,projection='3d')
x,y= ogrid[0:rawim.shape[0],0:rawim.shape[1]]
ax.plot_surface(x,y,0,rstride=5,cstride=5,facecolors=rawim,cmap='gray')
ax.view_init(elev=45, azim=12)
plt.show()

输出结果为此(编辑为包含图像)。 enter image description here
PS在Spyder中运行Matplotlib 1.2.1 for Python 2.75

编辑添加 - 我主要是从this post建模我的方法,所以如果不是

rawim=plt.imread(r'G:\Path\myimage.png')

我用

from matplotlib.cbook import get_sample_data
fn = get_sample_data("lena.png", asfileobj=False)
rawim=read_png(fn)

它完美无缺。我尝试了几种PNG输出,产生了几种不同的方式,而且没有爱。是的,他们是0-1之间的灰度。

1 个答案:

答案 0 :(得分:0)

您应该为facecolors使用显式颜色数组。 你想要一些具有形状(Nx,Ny,4)的东西,其中“4”尺寸包含RGBA值。 另外,在plot_surface调用中删除cmap ='gray'。

相关问题