Wednesday 15 May 2013

matplotlib - Map colors in colorbar in Python -



matplotlib - Map colors in colorbar in Python -

i using code below generate 3d surface plots. color represent value of radius. how draw corresponding colorbar?

i believe problem using facecolors while making surface plot. need create colorbar according facecolors.

i understand have map these colors colorbar, it's don't know how.

import numpy np import matplotlib.pyplot plt def sph2cart(theta, phi, radius): x_cart = radius * np.sin(theta) * np.cos(phi) y_cart = radius * np.sin(theta) * np.sin(phi) z_cart = radius * np.cos(theta) homecoming np.array([x_cart, y_cart, z_cart]) def antenna_plot_3d(mod_f, t, p): [x, y, z] = sph2cart(t, p, mod_f) fig = plt.figure() ax = fig.add_subplot(1, 1, 1, projection='3d') p_surf = ax.plot_surface(x, y, z, rstride=1, cstride=1, linewidth=0, antialiased=true, facecolors=cm.jet(np.sqrt(x*x + y*y + z*z))) ax.set_zlabel('z axis', fontsize=15) ax.set_xlabel('x axis', fontsize=15) ax.set_ylabel('y axis', fontsize=15) plt.savefig('3d_pattern.png') plt.show() x = 300 t = np.linspace(0, np.pi, num=x) p = np.linspace(0,2*np.pi, num=x) [t,p] = np.meshgrid(t,p) mod_f = np.sin(t) * np.cos(p) antenna_plot_3d(mod_f, t, p)

i want surface colored , colors should represent radius.

the next did task

fig = plt.figure() ax = fig.add_subplot(1, 1, 1, projection='3d') p_surf=ax.plot_surface(x,y,z,rstride=1,cstride=1,linewidth=0,antialiased=true,facecolors=cm.jet(np.sqrt(x*x + y*y + z*z))) m = cm.scalarmappable(cmap=cm.jet) m.set_array(x*x + y*y + z*z) plt.colorbar(m) plt.show()

python matplotlib

No comments:

Post a Comment