Python3 从字典创建 3-D 图(值 : Tuple)

标签 python python-3.x matplotlib data-science

我正在尝试从字典创建 3-D 图,但遇到了症结所在。 Z 访问将是键 (MSE),X 和 Y 轴将是元组的第一个和第二个值,例如在下面示例数据集的第一个示例中,X 将为 2,Y 将为 5:

80178.37739073468: (2, 5),
81623.18006660603: (13, 14),
82583.3021359235: (8, 16),
83491.34222215343: (9, 8),
83724.73005402873: (8, 14),
83856.2891246289: (7, 8),
83984.92825308126: (6, 5),
84314.30519882984: (13, 16),
84577.4110193269: (4, 11),
86338.86146117302: (6, 20)

我找到了使用列表来完成此操作的示例代码,但这仅适用于二维绘图。

最佳答案

如果我没理解错的话,你可以按照下面的方式做。

假设您的字典如下所示:

mydict = {80178.37739073468: (2, 5),
81623.18006660603: (13, 14),
82583.3021359235: (8, 16),
83491.34222215343: (9, 8),
83724.73005402873: (8, 14),
83856.2891246289: (7, 8),
83984.92825308126: (6, 5),
84314.30519882984: (13, 16),
84577.4110193269: (4, 11),
86338.86146117302: (6, 20)}

下面是绘制代码:

from mpl_toolkits.mplot3d import Axes3D
# Get your x, y and z variables
z = list(mydict.keys())
# unpack the dictionary values into two variables, x and y
x,y = zip(*mydict.values())
# plot
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.scatter(x, y, z)

enter image description here

有用的文档:matplotlib's Axes3d

关于Python3 从字典创建 3-D 图(值 : Tuple),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50454098/

相关文章:

Python Matplotlib 在被要求绘制第二张图表时挂起(在关闭第一个图表窗口后)

jquery - 如何在Python中将HTML字符串解析为HTML DOM元素?

python - Django错误: TypeError: int() argument must be a string or a number,而不是 'BuildsTable'

python - 如何按顺序获取数字的起点并处理错误?

python - 需要了解如何将数据分成同一图表上的两条单独的线

python - 在 Windows 64 位 python 3.5 上安装 matplotlib

python - 我可以打印出类的实例名称吗?

python - 谷歌应用引擎、Python 和 IPython

Python3如何安装.ttf字体文件?

python - 选择字符串插值中的小数点位数