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

Python3 Create A 3-D Plot From A Dictionary (Value: Tuple)

我正在尝试从字典创建 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)

有用的文档:matplotlib's Axes3d