简体   繁体   English

颜色条按示例matplotlib

[英]Color bars by sample matplotlib

I am trying to color code a bar plot I have by values in a list. 我正在尝试按列表中的值对条形图进行颜色编码。 I am plotting something like this but I can't figure out how to color it: 我正在绘制类似这样的东西,但我不知道如何给它上色:

x=[1,2,3]
y=[1,2,3]
type=[big,small,big]

p1=plt.bar(x,y)
plt.colorbar(type)

So what I ended up doing was creating a dictionary with defined colors for each of my parameters and then just converted this when plotting like so: 因此,我最终要做的是为每个参数创建一个具有定义颜色的字典,然后在绘制时将其转换为:

colors = {'C':'b', 'T':'r', 'G':'g', 'A':'y'}
wts.append(colors[x[3]])
p1 = plt.bar(loci, vafs, color=wts)

You may create a dictionary mapping values from the typ list to colors. 您可以创建一个字典,将值从类型列表映射到颜色。 Then using this dictionary you can create a list of colors to supply to the bar 's color argument. 然后,使用此字典,您可以创建颜色列表以提供给barcolor参数。

In order to create a legend, the same dictionary can be used to set the color of legend handles. 为了创建图例,可以使用相同的词典设置图例手柄的颜色。

import matplotlib.pyplot as plt

x=[1,2,3]
y=[1,2,3]
typ=["big","small","big"]
color = {"big" : "indigo", "small" : "mediumvioletred"}
p1 = plt.bar(x,y, color=[color[t] for t in typ])

handles = [plt.Rectangle((0,0),1,1, color=color[c]) for c in set(typ)]
plt.legend(handles=handles, labels=set(typ))

plt.show()

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM