简体   繁体   中英

How to make a 3D plot (X, Y, Z), assigning Z values to X,Y ordered pairs?

I am trying to plot a 3D chart, where my Z values would be assigned to each [X,Y] ordered pair. For example, these are my X, Y and Z values:

X = [1,2,3,4,5]
Y = [1,2,3,4,5]
Z = [10, 20, 30, 40, 50, 10, 20, 30, 40, 50, 10, 20, 30, 40, 50, 10, 20, 30, 40, 50, 10, 20, 30, 40, 50,]

And the Z values, correspond to the following [X,Y] ordered pair:

Z = [X[0]Y[0], X[0]Y[1], X[0]Y[2],...., X[5]Y[4], X[5]Y[5]]

Thank you!!

you can do it using np.meshgrid

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

X = [1,2,3,4,5]
Y = [1,2,3,4,5]
Z = [10, 20, 30, 40, 50, 10, 20, 30, 40, 50, 10, 20, 30, 40, 50, 10, 20, 30, 40, 50, 10, 20, 30, 40, 50,]

xy = np.array(np.meshgrid(X,Y)).reshape(-1,2)

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot(xy[:,0],xy[:,1],Z)
plt.show()

在此处输入图像描述

you can also use scatter

ax.scatter(xy[:,0],xy[:,1],Z)

在此处输入图像描述

for surface plot

x , y = np.meshgrid(X,Y)
ax.plot_surface(x,y,np.array(Z).reshape(5,5))
plt.show()

在此处输入图像描述

This will give the needed 3d array (instead of plotting it directly) which you can plot.

import numpy as np
X = [1,2,3,4,5]
Y = [1,2,3,4,5]
Z = [10, 20, 30, 40, 50, 10, 20, 30, 40, 50, 10, 20, 30, 40, 50, 10, 20, 30, 40, 50, 10, 20, 30, 40, 50]

XY = np.array([[(x, y) for y in Y] for x in X])

Z = np.array(Z).reshape(XY.shape[0],XY.shape[1], 1)

XYZ = np.concatenate((XY, Z), axis = -1)

print(XYZ)

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