简体   繁体   English

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

[英]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.我正在尝试 plot 3D 图表,其中我的 Z 值将分配给每个 [X,Y] 有序对。 For example, these are my X, Y and Z values:例如,这些是我的 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,]

And the Z values, correspond to the following [X,Y] ordered pair: Z 值对应于以下 [X,Y] 有序对:

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你可以使用 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用于表面 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.这将提供所需的 3d 阵列(而不是直接绘制它),您可以使用 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 给定 x,y,z 坐标的数据,我将如何在 Matplotlib 中制作 3D 表面 plot? - How would I make a 3D surface plot in Matplotlib given this data of x,y,z coordinates? 如何绘制来自3个不同列表(x,y和z)的3d直方图,其中z是所有重复的x,y坐标的平均值 - How to plot a 3d histogram from 3 different lists (x, y and z), where z is the mean of all repeated x,y coordinates 当Z是Python中的列表列表时,如何用X,Y,Z绘制3D曲面? - How to plot 3D surface with X, Y, Z when Z is a list of list in Python? 参数化3D曲面图,颜色取决于(x,y,z) - Parametric 3D Surface Plot with color depending on (x,y,z) Python读取X和Y值并分配给Z特定的XY对 - Python reading X and Y values and assigning to Z specific X Y pairs 如何在3D曲面图中绘制x,y,z的Numpy数组? - How can I plot a numpy array of x, y, z in 3D surface plot? 来自 x、y、z 值的 matplotlib 2D 图 - matplotlib 2D plot from x,y,z values 如何从 Plotly 中的 X、Y、Z 数组绘制 3D 表面? - How to plot a 3D surface from X,Y,Z array in Plotly? JavaScript Plotly.js 如何使用 x、y 和 z 坐标创建 3D 表面 plot? - JavaScript Plotly.js How to create a 3D surface plot using x, y, and z coordinates? 3d将x,y,z坐标转换为3d numpy数组 - 3d coordinates x,y,z to 3d numpy array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM