How to make 3D graphics in Python?

This is the MATLAB version for 3D graphics: EDIT: This is the 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)) for 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) # This is the line number 168 plt.xlabel('x') plt.ylabel('y') 

This is the error message I 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 than 0 values to unpack 
+4
source share
2 answers

After setting up the grid grid for X and Y, you need to create a grid for the Z values.

The way I'm doing this in my code:

 # [ (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) # This is the part you want: Z1 = np.zeros(X.shape, float) for (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 a plot that looks like this:

surf

I saved it as a file, but when you call plt.show() , you get an interactive window in which you can change the viewpoint to whatever you want.

+4
source

What happened? You are trying to make a negative number. In other words: AxesSubplot (whatever it is) does not implement a unary operator.

Thus, this code may not be β€œwhat you did,” since you do not even define b in this code, but it does exist and has some kind of user type called AxesSubplot . If you explain what AxesSubplot is, then this will help. Try to include code that actually demonstrates the problem, if possible.

Edit: As DSM indicates, you are overwriting variable b. The problem is that you are stuck in "math mode" and use names without descriptive variables such as "a", "b" and "M". Use longer descriptive names instead.

Instead:

  a = fig.add_subplot(2,2,i) b = fig2.add_subplot(2,2,i) 

make:

  x_subplot = fig.add_subplot(2,2,i) y_subplot = fig2.add_subplot(2,2,i) 

Or something like this (I'm not sure what the variable really is, so just an example).

+4
source

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


All Articles