如何制作3D图(X,Y,Z),将Z值分配给X,Y有序对?

问题描述 投票:0回答:1

我正在尝试绘制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]]

谢谢!

python numpy matplotlib multidimensional-array mplot3d
1个回答
0
投票

您可以使用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()

enter image description here

您也可以使用散点图

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

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.