简体   繁体   中英

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

I'm trying to create a 3-D Plot from a Dictionary and have hit a sticking point. The Z-access will be the key (MSE) and the X and Y axis will be the first and second values of the tuple eg X will be 2 and Y will be 5 in the first example of the example dataset below:

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)

I've found sample code to do it with a list but that's just for a 2-D plot.

If I understand correctly, you can do the following.

Assuming your dictionary looks like:

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)}

Here is the code to plot:

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)

在此输入图像描述

Useful documentation: matplotlib's Axes3d

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM