简体   繁体   English

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

[英]Matplotlib-Imported PNG won't show in 3D plot

I'm trying to use matplotlib to generate 3D figures where the xy plane is an image, and then some 3D tracks are drawn on top (that part works just fine). 我正在尝试使用matplotlib生成3D图形,其中xy平面是一幅图像,然后在顶部绘制一些3D轨迹(该部分效果很好)。 The problem is, even though my imported PNG shows just fine with imshow, and even though I can plot an image on a 3D axis if I just use an example from the cookbook, my image just shows up as a featureless black box. 问题是,即使我导入的PNG与imshow一起显示也很好,并且即使我仅使用菜谱中的示例,也可以在3D轴上绘制图像,但我的图像仍显示为无特征的黑框。 I'm sure I'm missing something small- thanks in advance! 我确定我想念一些小东西-预先感谢!

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

The output comes out as this (edited to include image). 输出结果如下(已编辑以包含图像)。 在此处输入图片说明
PS Running Matplotlib 1.2.1 in Spyder for Python 2.75 PS在Python 2.75的Spyder中运行Matplotlib 1.2.1

Edited to add- I was largely modeling my approach from this post , so if instead of 编辑为添加-我主要是根据本文对我的方法进行建模,所以如果不是

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

I use 我用

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

it works perfectly. 它完美地工作。 I've tried several of my PNG outputs, produced a couple of different ways, and no love. 我已经尝试了几种PNG输出,产生了几种不同的方式,但是没有爱。 And yes, they're greyscale between 0-1. 是的,它们是0-1之间的灰度。

You should use an explicit color array for facecolors. 您应该为facecolors使用显式的颜色数组。 You want something having shape (Nx, Ny, 4) where the "4" dimension holds RGBA values. 您需要某种形状(Nx,Ny,4)的图形,其中“ 4”维包含RGBA值。 Also, get rid of the cmap='gray' in the plot_surface invocation. 另外,在plot_surface调用中摆脱cmap ='gray'。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM