3

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). 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. 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). enter image description here
PS Running Matplotlib 1.2.1 in Spyder for Python 2.75

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. And yes, they're greyscale between 0-1.

Community
  • 1
  • 1
  • Could you put a pointer to the corresponding cookbox working example? –  Sep 23 '13 at 10:24
  • It seems to work fine for me. Have you tried using different images? Also, are your image values floats between 0 and 1? – ali_m Sep 23 '13 at 11:17

1 Answers1

0

You should use an explicit color array for facecolors. You want something having shape (Nx, Ny, 4) where the "4" dimension holds RGBA values. Also, get rid of the cmap='gray' in the plot_surface invocation.

ARonz
  • 1
  • 1