[英]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.