matplotlib.pyplot散点图传奇从颜色字典 [英] matplotlib.pyplot scatterplot legend from color dictionary

查看:2063
本文介绍了matplotlib.pyplot散点图传奇从颜色字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用我的 D_id_color 字典为我的散点图做一个传奇。如何根据这些值与实际颜色创建图例?

I'm trying to make a legend with my D_id_color dictionary for my scatterplot. How can I create a legend based on these values with the actual color?

#!/usr/bin/python
import matplotlib.pyplot as plt
from 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 for 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 the legend to look like this but instead of color name, it would have the actual color.

A orchid
C grey
B darkcyan
E turquoise
D dodgerblue
F darkviolet


推荐答案

达成此目标的一种方法:

One way to 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 for color in D_id_color.values()[:len(x_coordinates)]]
plt.scatter(x_coordinates,y_coordinates, s = size_map, c = color_map)

# The following two lines generate custom fake lines that will be used as legend entries:
markers = [plt.Line2D([0,0],[0,0],color=color, marker='o', linestyle='') for color in D_id_color.values()]
plt.legend(markers, D_id_color.keys(), numpoints=1)

plt.show()

这将产生:

这篇关于matplotlib.pyplot散点图传奇从颜色字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆