I have some x,y data stored as dictionaries with the key as x and the value as y. I want to graph these on a scatter plot using matplotlib, with each dictionary treated as a category. What I've tried so far colors the points how I want but labels each point separately instead of as a group, producing dozens of identical labels in the legend at scale. Here's what I have, with some placeholder info:
dict1 = {1:2, 3:4, 5:6}
dict2 = {7:8, 9:10, 11:12}
dict3 = {13:14, 15:16, 17:18}
data = (dict1, dict2, dict3)
colors = ('r', 'g', 'b')
groups = ('First','Second','Third')
for datum, color, group in zip(data, colors, groups):
for i in datum:
x, y = i, datum[i]
plt.scatter(x, y, c=color, label=group)
plt.legend(loc=2)
plt.show()
I've tried moving plt.scatter()
out of the for i in datum
loop, but that only plots one point per group, instead of each key:value pair. How do I get the legend to label these by category instead of by point?