从 matplotlib 中的元组列表中绘制 3d 曲面

Posted

技术标签:

【中文标题】从 matplotlib 中的元组列表中绘制 3d 曲面【英文标题】:Plotting a 3d surface from a list of tuples in matplotlib 【发布时间】:2014-02-05 08:46:56 【问题描述】:

我正在尝试从 (X,Y,Z) 元组列表中绘制一个 3d 表面,数据是这样的,我不确定如何将其提供给 matplotlib:

[(60,5,'121'), (61,5,'103'),  (62,5,'14.8'), (63,5,'48.5'),
(64,5,'57.5'), (65,5,'75.7'), (66,5,'89.6'), (67,5,'55.3'),
(68,5,'63.3'), (69,5,'118'),  (70,5,'128'),  (71,5,'105'),
(72,5,'115'),  (73,5,'104'),  (74,5,'134'),  (75,5,'123'),
(76,5,'66.3'), (77,5,'132'),  (78,5,'145'),  (79,5,'115'),
(80,5,'38.2'), (81,5,'10.4'), (82,5,'18.4'), (83,5,'87'),
(84,5,'86.7'), (85,5,'78.9'), (86,5,'89.9'), (87,5,'108'),
(88,5,'57.1'), (89,5,'51.1'), (90,5,'69.1'), (91,5,'59.8'),
(60,6,'48.9'), (61,6,'33.3'), (62,6,'-19.2'),(63,6,'-17.5'),
(64,6,'-6.5'), (65,6,'75.7'), (66,6,'89.6'), (67,6,'55.3'),
(68,6,'99.8'), (69,6,'156'),  (70,6,'141'),  (71,6,'54.1'),
(72,6,'66.1'), (73,6,'98.9'), (74,6,'155'),  (75,6,'146'),
(76,6,'111'),  (77,6,'132'),  (78,6,'145'),  (79,6,'97.3'),
(80,6,'101'),  (81,6,'59.4'), (82,6,'70.4'), (83,6,'142'),
(84,6,'145'),  (85,6,'140'),  (86,6,'56.9'), (87,6,'77.8'),
(88,6,'21.1'), (89,6,'27.1'), (90,6,'48.1'), (91,6,'41.8')]

理想情况下,我希望它看起来像这样:

【问题讨论】:

你可能想看看***.com/questions/12423601/…或***.com/questions/3012783/… 我在这里发布了另一个示例:***.com/a/30539444/3585557。另外,请查看这些相关/相似/重复的帖子:***.com/q/9170838/3585557、***.com/q/12423601/3585557、***.com/q/26074542/3585557、***.com/q/28389606/3585557、***.com/q/29547687/3585557 您的Z 是浮点数的字符串表示形式。 【参考方案1】:

这是一种方法。

from mpl_toolkits.mplot3d import Axes3D
from scipy.interpolate import griddata
import matplotlib.pyplot as plt
import numpy as np

data = [(60, 5, '121'), (61, 5, '103'), (62, 5, '14.8'), (63, 5, '48.5'), (64, 5, '57.5'), (65, 5, '75.7'), (66, 5, '89.6'), (67, 5, '55.3'), (68, 5, '63.3'), (69, 5, '118'), (70, 5, '128'), (71, 5, '105'), (72, 5, '115'), (73, 5, '104'), (74, 5, '134'), (75, 5, '123'), (76, 5, '66.3'), (77, 5, '132'), (78, 5, '145'), (79, 5, '115'), (80, 5, '38.2'), (81, 5, '10.4'), (82, 5, '18.4'), (83, 5, '87'), (84, 5, '86.7'), (85, 5, '78.9'), (86, 5, '89.9'), (87, 5, '108'), (88, 5, '57.1'), (89, 5, '51.1'), (90, 5, '69.1'), (91, 5, '59.8'), (60, 6, '48.9'), (61, 6, '33.3'), (62, 6, '-19.2'), (63, 6, '-17.5'), (64, 6, '-6.5'), (65, 6, '75.7'), (66, 6, '89.6'), (67, 6, '55.3'), (68, 6, '99.8'), (69, 6, '156'), (70, 6, '141'), (71, 6, '54.1'), (72, 6, '66.1'), (73, 6, '98.9'), (74, 6, '155'), (75, 6, '146'), (76, 6, '111'), (77, 6, '132'), (78, 6, '145'), (79, 6, '97.3'), (80, 6, '101'), (81, 6, '59.4'), (82, 6, '70.4'), (83, 6, '142'), (84, 6, '145'), (85, 6, '140'), (86, 6, '56.9'), (87, 6, '77.8'), (88, 6, '21.1'), (89, 6, '27.1'), (90, 6, '48.1'), (91, 6, '41.8')]
x, y, z = zip(*data)
z = map(float, z)
grid_x, grid_y = np.mgrid[min(x):max(x):100j, min(y):max(y):100j]
grid_z = griddata((x, y), z, (grid_x, grid_y), method='cubic')

fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot_surface(grid_x, grid_y, grid_z, cmap=plt.cm.Spectral)
plt.show()

【讨论】:

上述代码 (Python 3.3) 失败并出现以下回溯: 文件 "C:\Python33\PyPostgres\test.py",第 10 行,在 grid_z = griddata((x, y), z, (grid_x, grid_y), method='立方') 文件“C:\Python33\lib\site-packages\scipy\interpolate\ndgriddata.py”,第 210 行,在 griddata rescale=rescale) 文件“interpnd.pyx”,第 840 行,在scipy.interpolate.interpnd.CloughTocher2DInterpolator.__init__ (scipy\interpolate\interpnd.c:9507) 文件“interpnd.pyx”,第 78 行,在 scipy.interpolate.interpnd.NDInterpolatorBase.__init__ (scipy\interpolate\interpnd.c:2372 )文件“interpnd.pyx”,第 120 行,在 scipy.interpolate.interpnd.NDInterpolatorBase._check_init_shape (scipy\interpolate\interpnd.c:3039) IndexError: tuple index out of range @aag 你需要做z = list(map(float, z)),因为map是python3中的生成器。 @M4rtini,感谢有关griddata 的提示。这是一个出色的实现。

以上是关于从 matplotlib 中的元组列表中绘制 3d 曲面的主要内容,如果未能解决你的问题,请参考以下文章

使用 matplotlib 从“列表列表”中绘制 3d 曲面

从列表中的元组中删除空字符串

如何从python中的列表中删除重复的元组?

从列表中的元组替换字符串

Python中的元组(Tuple)

如何从 C# 中的元组列表中获取最接近“a”的 4 个元素?