matlab - How to make 3D plots in Python? -
this matlab version of 3d plotting code: edit: current code:
plt.figure(2) fig_b = axes3d(fig2) xx2 = np.arange(0, l+h_grid*l, h_grid*l) yy2 = np.arange(-b, b+h_grid*b, h_grid*b) x, y = np.meshgrid(xx2, yy2) w = np.zeros((41,21), float) mx = len(xx2)*len(yy2) x = np.reshape(x, (1, mx)) y = np.reshape(y, (1, mx)) w = np.reshape(w, (1, mx)) j in range(0, mx): w[0][j] = np.sin(np.pi*x[0][j]/l) surf = fig_b.plot_surface(x, y, w, rstride=1, cstride=1, cmap=cm.jet, linewidth=0, antialiased=false) # line number 168 plt.xlabel('x') plt.ylabel('y')
this error message get:
traceback (most recent call last): file "nonhomog.py", line 247, in <module> main() file "nonhomog.py", line 245, in main nonhomog(nu) file "nonhomog.py", line 168, in nonhomog surf = fig_b.plot_surface(x, y, w, rstride=1, cstride=1, cmap=cm.jet, linewidth=0, antialiased=false) file "/usr/lib/pymodules/python2.6/mpl_toolkits/mplot3d/axes3d.py", line 618, in plot_surface polyc = art3d.poly3dcollection(polys, *args, **kwargs) file "/usr/lib/pymodules/python2.6/mpl_toolkits/mplot3d/art3d.py", line 290, in __init__ polycollection.__init__(self, verts, *args, **kwargs) file "/usr/lib/pymodules/python2.6/matplotlib/collections.py", line 668, in __init__ self.set_verts(verts, closed) file "/usr/lib/pymodules/python2.6/mpl_toolkits/mplot3d/art3d.py", line 312, in set_verts self.get_vector(verts) file "/usr/lib/pymodules/python2.6/mpl_toolkits/mplot3d/art3d.py", line 305, in get_vector xs, ys, zs = zip(*points) valueerror: need more 0 values unpack
after setting mesh grid x , y, need come grid z values.
the way in code is:
# [ (x1, y1, z1), (x2, y2, z2), ... (xn, yn, zn) ] all_vals = ... # (x1, x2, ... xn) , (y1, y2, ... yn) , (z1, z2, ... zn) all_xvals, all_yvals, all_zvals = zip(*all_vals) fig = plt.figure() ax = axes3d(fig) x, y = np.meshgrid(xvals, yvals) # part want: z1 = np.zeros(x.shape, float) (x, y, z) in all_vals: x = find_in_sorted_list(x, xvals) y = find_in_sorted_list(y, yvals) z1[y,x] = z surf = ax.plot_surface(x, y, z1, rstride=1, cstride=1, cmap=cm.jet, linewidth=0, antialiased=false) plt.xlabel('blur standard deviation') plt.ylabel('jpeg quality') ax.w_zaxis.set_major_locator(linearlocator(10)) ax.w_zaxis.set_major_formatter(formatstrformatter('%.03f')) fig.colorbar(surf, shrink=0.5, aspect=5) plt.show()
this gives me plot looks this:
i've saved file, when call plt.show()
, interactive window can change viewpoint want.
Comments
Post a Comment