Using a separate function for colormap other than x,y,z for a 3D surface

I'm generating a 3D shape (X1,Y1,Z1) using the code below. A separate function, A5, has been used for a colormap. But as of now I'm only getting a blue surface. The range of values in Z1 is higher than that of A5 - could that be the reason behind the code generating a blue surface? Does it take the limit of Z1 for facecolor? If so how should I use plot_surface for this purpose?

surf = ax.plot_surface(X1,Y1,Z1,rstride=1, cstride=1, linewidth=0, facecolors=plt.cm.jet(A5),antialiased=False)
m = cm.ScalarMappable(cmap=cm.jet)
q=[-col,col]
m.set_array(q)
plt.colorbar(m)

Upvotes: 1

Views: 468

Answers (1)

gboffi
gboffi

Reputation: 25023

import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

# generating data
x, y = np.meshgrid(np.linspace(-1,1,101), np.linspace(-1,1,101))
z = x+y  # z is just a plane
r = np.sqrt(x*x+y*y)
w = 2*np.cos(5*r)**2-np.sin(6*x) # w is a funny trig func

# initializing the colormap machinery
norm = matplotlib.colors.Normalize(vmin=np.min(w),vmax=np.max(w))
c_m = matplotlib.cm.Spectral
s_m = matplotlib.cm.ScalarMappable(cmap=c_m, norm=norm)
s_m.set_array([])

# a figure and a 3d subplot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# the actual plot, note that to create the facecolors array
# I've used the norm function I instantiated above
ax.plot_surface(x, y, x+y, linewidth=0,
                rstride=1, cstride=1,
                facecolors=s_m.to_rgba(w))

# draw the colormap using the ScalarMappable instantiated above and shoe
plt.colorbar(s_m)
plt.show()

enter image description here

Upvotes: 1

Related Questions