简体   繁体   English

给定 x,y,z 坐标的数据,我将如何在 Matplotlib 中制作 3D 表面 plot?

[英]How would I make a 3D surface plot in Matplotlib given this data of x,y,z coordinates?

I have data in this format:我有这种格式的数据:

from itertools import permutations
import random

values = [val for val in range(5)]
xy_coordinate_pairs = list(permutations(values, 2))
z_values = [random.randint(0,20) for val in range(len(xy_coordinate_pairs))]
data = dict(zip(xy_coordinate_pairs,z_values))

{(0, 1): 4,
 (0, 2): 20,
 (0, 3): 16,
 (0, 4): 12,
 (1, 0): 6,
 (1, 2): 3,
 (1, 3): 6,
 (1, 4): 16,
 (2, 0): 19,
 (2, 1): 17,
 (2, 3): 17,
 (2, 4): 11,
 (3, 0): 18,
 (3, 1): 13,
 (3, 2): 11,
 (3, 4): 20,
 (4, 0): 20,
 (4, 1): 19,
 (4, 2): 6,
 (4, 3): 0}

And I'd like to plot this as a 3d surface plot, where the x and y coordinates are the first and second value respectively in the key tuples, and the z (height of surface plot) are the dictionary values.我想将 plot 这个作为 3d 表面 plot,其中 x 和 y 坐标分别是表面图和字典中的第一个和第二个值。 How would I go about doing this?我将如何 go 这样做?

Thanks so much and have a great day.非常感谢,祝您有美好的一天。

I am not sure why you are creating tuples & dictionaries.我不确定你为什么要创建元组和字典。 Assuming you have three different arrays for x, y and z axis:假设 x、y 和 z 轴有三个不同的 arrays:

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

x = [val for val in range(10)]
y = [val * val for val in range(10)]
z = [val * val * val for val in range(10)]

All you have to do is:你所要做的就是:

fig = plt.figure()
ax = Axes3D(fig)
surf = ax.plot_trisurf(x, y, z, linewidth=0.1)
fig.colorbar(surf, shrink=0.5, aspect=5)
plt.show()

End result below:最终结果如下:

在此处输入图像描述

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

相关问题 如何用matplotlib制作3d曲面plot,Z不是由func计算的? - How to make a 3d surface plot with matplotlib, Z is not calculated by func? 如何在3D曲面图中绘制x,y,z的Numpy数组? - How can I plot a numpy array of x, y, z in 3D surface plot? 参数化3D曲面图,颜色取决于(x,y,z) - Parametric 3D Surface Plot with color depending on (x,y,z) 如何在python中使用matplotlib制作3D数据表面图 - How to make a 3D data surface plot using matplotlib in python 当Z是Python中的列表列表时,如何用X,Y,Z绘制3D曲面? - How to plot 3D surface with X, Y, Z when Z is a list of list in Python? 使用 Matplotlib a*y + b*x + c 绘制 3D 曲面 - Plot 3D surface with Matplotlib a*y + b*x + c Matplotlib在给定csv数据的情况下创建表面图(x,y,z,颜色) - 得到错误的颜色 - Matplotlib create surface plot (x,y,z,color) given csv data - getting wrong colors 如何在mplot3d上绘制表面以获取(x,y,z)=(0,0,1),(0,0,2)之类的数据 - How can I plot surface on mplot3d for data like (x,y,z)=(0,0,1),(0,0,2) 在python中从{x,y,z} - 散射数据绘制3D表面 - Plot a 3D surface from {x,y,z}-scatter data in python 如何制作 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?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM