python - matplotlib.pyplot scatterplot legend from color dictionary -
i'm trying make legend d_id_color
dictionary scatterplot. how can create legend based on these values actual color?
#!/usr/bin/python import matplotlib.pyplot plt matplotlib import colors d_id_color = {'a': u'orchid', 'b': u'darkcyan', 'c': u'grey', 'd': u'dodgerblue', 'e': u'turquoise', 'f': u'darkviolet'} x_coordinates = [1,2,3,4,5] y_coordinates = [3,3,3,3,3] size_map = [50,100,200,400,800] color_map = [color color in d_id_color.values()[:len(x_coordinates)]] plt.scatter(x_coordinates,y_coordinates, s = size_map, c = color_map) plt.show()
i want legend instead of color name, have actual color.
a orchid c grey b darkcyan e turquoise d dodgerblue f darkviolet
one way achieve this:
d_id_color = {'a': u'orchid', 'b': u'darkcyan', 'c': u'grey', 'd': u'dodgerblue', 'e': u'turquoise', 'f': u'darkviolet'} x_coordinates = [1,2,3,4,5,6] # added missing datapoint y_coordinates = [3,3,3,3,3,3] # added missing datapoint size_map = [50,100,200,400,800,1200] # added missing datapoint color_map = [color color in d_id_color.values()[:len(x_coordinates)]] plt.scatter(x_coordinates,y_coordinates, s = size_map, c = color_map) # following 2 lines generate custom fake lines used legend entries: markers = [plt.line2d([0,0],[0,0],color=color, marker='o', linestyle='') color in d_id_color.values()] plt.legend(markers, d_id_color.keys(), numpoints=1) plt.show()
this yield:
Comments
Post a Comment