如何制作 3D 图(X、Y、Z),将 Z 值分配给 X、Y 有序对?
How to make a 3D plot (X, Y, Z), assigning Z values to X,Y ordered pairs?
我正在尝试绘制 3D 图表,其中我的 Z 值将分配给每个 [X,Y] 有序对。例如,这些是我的 X、Y 和 Z 值:
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,]
和Z值,对应以下[X,Y]有序对:
Z = [X[0]Y[0], X[0]Y[1], X[0]Y[2],...., X[5]Y[4], X[5]Y[5]]
谢谢!!
你可以使用 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()
你也可以使用散点图
ax.scatter(xy[:,0],xy[:,1],Z)
对于曲面图
x , y = np.meshgrid(X,Y)
ax.plot_surface(x,y,np.array(Z).reshape(5,5))
plt.show()
这将提供您可以绘制的所需 3d 数组(而不是直接绘制)。
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)
我正在尝试绘制 3D 图表,其中我的 Z 值将分配给每个 [X,Y] 有序对。例如,这些是我的 X、Y 和 Z 值:
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,]
和Z值,对应以下[X,Y]有序对:
Z = [X[0]Y[0], X[0]Y[1], X[0]Y[2],...., X[5]Y[4], X[5]Y[5]]
谢谢!!
你可以使用 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()
你也可以使用散点图
ax.scatter(xy[:,0],xy[:,1],Z)
对于曲面图
x , y = np.meshgrid(X,Y)
ax.plot_surface(x,y,np.array(Z).reshape(5,5))
plt.show()
这将提供您可以绘制的所需 3d 数组(而不是直接绘制)。
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)