Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scaled colormap of facecolors with mplot3d

I have a simple task that should have a simple solution, but I have been trying for days now. I try to be specific.

  • I try to plot a surface using matplotlib's mplot3d and plot_surface. When I plot the surface of a dataset 'z' and try to scale the colormap to a certain maximum value I change the 'vmax' property to this value. That works great.

  • When I try to plot a surface of one dataset (z) and use the facecolors of a second dataset (fc), this also works fine.

  • When I want to scale the colormap of the facecolors, the vmax property is overruled by the facecolors values. Vmax therefore has no effect (attempt1). The lines also disappeared, but that's another issue.

  • Also trying to change the values of the facecolor dataset (fc) did not have the desired effect (attempt2).

I try to get a figure with a scaled colormap (as in the figure 'scaled' below) but scaled to the facecolors, and not the z-values.

The code below is what I have now, and the results look like this:

enter image description here

Does anyone know what I am missing here? Any thoughts are appreciated!

import pylab as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D

plt.ion()

# creating dataset
profile = np.arange(20)**2
z = profile.repeat(20).reshape(20,20)
fc= np.rot90(z.copy())

x = np.arange(z.shape[0])
y = np.arange(z.shape[1])
X, Y = np.meshgrid(x,y)

# plotting
vmax = 100
fig = plt.figure()
ax = fig.add_subplot(1,4,1, projection='3d', azim=210)
ax.plot_surface(X,Y,z, cmap=plt.cm.jet, cstride=1, rstride=1)
ax.set_title('normal')

ax = fig.add_subplot(1,4,2, projection='3d', azim=210)
ax.plot_surface(X,Y,z, cmap=plt.cm.jet, cstride=1, rstride=1, vmax=vmax)
ax.set_title('scaled')

ax = fig.add_subplot(1,4,3, projection='3d', azim=210)
ax.plot_surface(X,Y,z, facecolors=plt.cm.jet(fc), cstride=1, rstride=1, vmax=vmax)
ax.set_title('rotated (attempt1)')

ax = fig.add_subplot(1,4,4, projection='3d', azim=210)
fc[fc> vmax] = vmax
ax.plot_surface(X,Y,z, facecolors=plt.cm.jet(fc), cstride=1, rstride=1)
ax.set_title('rotated (attempt2)')
like image 469
Niels Avatar asked Jul 13 '12 11:07

Niels


1 Answers

One - dirty - solution would be to rescale the clipped facecolors such that the maximum is equal to the maximum of your height map (in addition to basically what you suggested as attempt 2):

ax.plot_surface(X,Y,z, facecolors=plt.cm.jet(np.clip(fc,0,vmax)*np.max(z)/vmax), cstride=1, rstride=1, vmax=vmax)

Does this give the result you are looking for?

like image 118
Jakob S. Avatar answered Oct 02 '22 07:10

Jakob S.