The color of the 3D surface in python

I use the following line to build a 3D surface:

surf = ax3.plot_surface(X, Y, Z, rstride=1, cstride=1, alpha=0.5, linewidth=0, cmap=cm.jet,antialiased=True) 

Now the color is going very well, although a little scaly appearance, although beautiful.
But I want to change the surface color of wrt to other data stored in list as:

 m = [104.48, 111.73,109.93,139.95,95.05,150.49,136.96,157.75] 

I have tried:

 norm = cls.Normalize() # Norm to map the 'm' values to [0,1] norm.autoscale(m) cmap = cm.ScalarMappable(norm, 'jet') surf = ax3.plot_surface(X, Y, Z, rstride=5, cstride=5, alpha=0.5, linewidth=0, color=cmap.to_rgba(m), antialiased=True) 

But this causes an error because cmap.to_rgba accepts only 1D arrays. Any suggestions on how I can change the colormap surface would be greatly appreciated.

+3
source share
3 answers

Well, that looks awful, but I think you can adapt it:

 from mpl_toolkits.mplot3d import Axes3D from matplotlib import cm from matplotlib.ticker import LinearLocator, FormatStrFormatter import matplotlib.pyplot as plt import numpy as np fig = plt.figure() ax = fig.gca(projection='3d') X = np.arange(-5, 5, 0.25) Y = np.arange(-5, 5, 0.25) X, Y = np.meshgrid(X, Y) R = np.sqrt(X**2 + Y**2) Z = np.sin(R) my_col = cm.jet(np.random.rand(Z.shape[0],Z.shape[1])) surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, facecolors = my_col, linewidth=0, antialiased=False) ax.set_zlim(-1.01, 1.01) 

I would not use a trickle, but some linear color palette like cubehelix . You can easily fool the eye using the wrong flower card ( one of many posts on this topic)

+4
source

To get the correct colors, use the Z values ​​to select the values ​​from the color map:

 my_col = cm.jet(Z/np.amax(Z)) 

Result:

superficial plot

otherwise using the same code as @Moritz.

 from mpl_toolkits.mplot3d import Axes3D from matplotlib import cm from matplotlib.ticker import LinearLocator, FormatStrFormatter import matplotlib.pyplot as plt import numpy as np fig = plt.figure() ax = fig.add_subplot(111, projection='3d') X = np.arange(-5, 5, 0.25) Y = np.arange(-5, 5, 0.25) X, Y = np.meshgrid(X, Y) R = np.sqrt(X**2 + Y**2) Z = np.sin(R) my_col = cm.jet(Z/np.amax(Z)) surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, facecolors = my_col, linewidth=0, antialiased=False) ax.set_zlim(-1.01, 1.01) plt.show() 
+1
source

I am doing this with some lines in python using PANDAS, the plot is beautiful!

 from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt from matplotlib import cm import numpy as np import pandas as pd from sys import argv file = argv[1] x,y,z = np.loadtxt(file, unpack=True) df = pd.DataFrame({'x': x, 'y': y, 'z': z}) fig = plt.figure() ax = Axes3D(fig) surf = ax.plot_trisurf(df.x, df.y, df.z, cmap=cm.jet, linewidth=0.1) fig.colorbar(surf, shrink=0.5, aspect=5) plt.savefig('teste.pdf') plt.show() 

Collapsing Wave Equations

A little prettier! In my case, I use colormap JET Colormaps Matplotlib , but there are other types of color and quality maps. Take a look at the link earlier.

0
source

All Articles