简体   繁体   中英

How do I plot 3D collated data as a surface in Python?

So I have two independent variables, x and y, and a set of collated dependent variables. For the y value of 1000 for example, there are several values of Z depending on what X is. I tried adapting the code below for my purposes, but I almost always end up with a line for my data (which is stored in a.csv file.)

Does my data for Z need to be in a specific flattened array format for it to work? When I try something like that, it always gives me the diagonal values of the csv file, and not a surface.

ax = plt.axes(projection='3d')

# Data for a three-dimensional line
zline = np.linspace(0, 15, 1000)
xline = np.sin(zline)
yline = np.cos(zline)
ax.plot3D(xline, yline, zline, 'gray')

# Data for three-dimensional scattered points
zdata = 15 * np.random.random(100)
xdata = np.sin(zdata) + 0.1 * np.random.randn(100)
ydata = np.cos(zdata) + 0.1 * np.random.randn(100)
ax.scatter3D(xdata, ydata, zdata, c=zdata, cmap='Greens');

To plot a 3D surface, you need to use the ax.plot_surface method, as shown below.

ax = plt.axes(projection='3d')

xpoints = np.linspace(0,5,100)
ypoints = np.linspace(0,5,100)
X, Y = np.meshgrid(x,y)
Z = np.cos(X) + np.sin(Y)

ax.plot_surface(X, Y, Z,  cmap='Greens')

Rather than providing arrays of the coordinates directly, you need to use np.meshgrid to obtain 2D arrays of the coordinates. The array Z should also be 2D, with the same dimensions as X and Y .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM