# Plotting a imshow() image in 3d in matplotlib

Hi,

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:

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.imshow(data, cmap=plt.cm.BrBG, interpolation=‘nearest’, origin=‘lower’, extent=[0,1,0,1])

# show the 3D rotated projection

ax2.plot_surface(X, Y, Z, rstride=1, cstride=1, facecolors=plt.cm.BrBG(data), shade=False)

The plots are here. Is there any other way to solve this issue?

I have posted this question on stackoverflow.

Thanks

Raj

···

##################################################################
Raj Kumar Manna

Complex Fluid & Biological Physics Lab

Ph. No. 8144637401

alternate email: raj@…4669…

####################################################################

The call to imshow() without vmin/vmax arguments will automatically scale
the colormap to cover the entire range of values. Meanwhile, when you did
plt.cm.BrBG(data), it assumed that the vmin/vmax is 0 and 1, respectively.
The min and max of your data is actually 0.292 and 1.708. If you normalize
your data, it should look much more correct.

Cheers!
Ben Root

···

On Tue, May 26, 2015 at 12:36 PM, Raj Kumar Manna <rajphysics.mon@...287...> wrote:

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