Scaled color map with facecolors with mplot3d

I have a simple task that should have a simple solution, but I tried several days already. I try to be specific.

  • I am trying to build a surface using matplotlib mplot3d and plot_surface. When I draw the surface of the "z" dataset and try to scale the color map to a certain maximum value, I change the "vmax" property to this value. This works great.

  • When I try to build the surface of one dataset (z) and use the facial flowers of the second dataset (fc), this also works fine.

  • When I want to scale a color copy of facecolors, the vmax property is overridden by the values ​​of facecolors. Therefore, Vmax has no effect (attempt 1). The lines also disappeared, but this is another problem.

  • Also, an attempt to change the values ​​of the facecolor (fc) dataset did not have the desired effect (attempt2).

I am trying to get a figure with a scaled color palette (as shown in the figure below), but it scales to facecolors, not z-values.

Below is the code I have and the results look like this:

enter image description here

Does anyone know what I'm missing here? Any thoughts 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)') 
+6
source share
1 answer

One dirty solution should be to rescale the cropped face masks so that the maximum is equal to the maximum of your height map (in addition to what you assumed 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 mean the result you are looking for?

+3
source

Source: https://habr.com/ru/post/920407/


All Articles