Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (17.6k points)

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.

1 Answer

0 votes
by (41.4k points)

Let’s assume mydictionary = {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)}

You can follow the below code for creating a 3-D plot from a dictionary

from mpl_toolkits.mplot3d import Axes3D 

c = list(mydictionary.keys()) 

# we have to unpack the dictionary values into two variables, b and c 

a,b = zip(*mydictionary.values()) 

# Now plotting 

fig = plt.figure() 

pr = fig.gca(projection='3d') 

pr.scatter(a, b, c)

If you wish to learn more about Python visit this Python Tutorial.

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...