Reputation: 141
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 e.g. 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.
Upvotes: 4
Views: 422
Reputation: 51395
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
Upvotes: 4