简体   繁体   English

Python:Plt条形图-不同的颜色

[英]Python: Plt bar plot - different colors

In Python, how can I make the 'reported' bars green, and 'UNREPORTED' bars red? 在Python中,如何将'reported''UNREPORTED'绿色,将'reported''UNREPORTED'红色? I want to give different color to each of the reported and UNREPORTED bars in my graph. 我想为图表中的每个报告栏和未报告栏赋予不同的颜色。

new = (('AXIN', 37, 'reported'),
     ('LGR', 30, 'UNREPORTED'),
     ('NKD', 24, 'reported'),
     ('TNFRSF', 23, 'reported'),
     ('CCND', 19, 'reported'),
     ('APCDD', 18, 'reported'),
     ('TRD', 16, 'reported'),
     ('TOX', 15, 'UNREPORTED'), 
     ('LEF', 15, 'reported'),
     ('MME', 13, 'reported'))

#sort them as most common gene comes first
new = sorted(new, key=lambda score: score[1], reverse=True) 
#X, Y zip of the tuple new are for plt.bar 
X, Y, _ = zip(*new)    

import seaborn as sns
sns.set()
import matplotlib.pyplot as plt 
%matplotlib inline

plt.figure(figsize = (20, 10))

mytitle = "Most common genes coexpressed with {gene1}, {gene2}, {gene3}, {gene4}".format(
    gene1="Axin2", gene2="Lef", gene3="Nkd1", gene4="Lgr5")
plt.title(mytitle, fontsize=40)

plt.ylabel('Number of same gene encounters across studies', fontsize=20)
ax = plt.bar(range(len(X)), Y, 0.6, tick_label = X, color="green") 
ax = plt.xticks(rotation=90)

new = tuple(new)

You can iterate over the bars and check if for the given index, the report is 'UNREPORTED'. 您可以遍历栏并检查给定索引的报告是否为“未报告”。 If this is the case, colorize the bar using set_color . 如果是这种情况,请使用set_color为栏着色。

在此处输入图片说明

import seaborn as sns
import matplotlib.pyplot as plt 

new = (('AXIN', 37, 'reported'),
     ('LGR', 30, 'UNREPORTED'),
     ('NKD', 24, 'reported'),
     ('TNFRSF', 23, 'reported'),
     ('CCND', 19, 'reported'),
     ('APCDD', 18, 'reported'),
     ('TRD', 16, 'reported'),
     ('TOX', 15, 'UNREPORTED'), 
     ('LEF', 15, 'reported'),
     ('MME', 13, 'reported'))

#sort them as most common gene comes first
new = sorted(new, key=lambda score: score[1], reverse=True) 
#X, Y zip of the tuple new are for plt.bar 
X, Y, rep = zip(*new)    

plt.figure(figsize = (8, 6))

mytitle = "Most common genes coexpressed with {gene1}, {gene2}, {gene3}, {gene4}".format(
    gene1="Axin2", gene2="Lef", gene3="Nkd1", gene4="Lgr5")
plt.title(mytitle)

plt.ylabel('Number of same gene encounters across studies')
bars = plt.bar(range(len(X)), Y, 0.6, tick_label = X, color="green") 
plt.xticks(rotation=90)

for i, bar in enumerate(bars):
    if rep[i] == 'UNREPORTED':
        bar.set_color("red")

plt.show()

You need to pass a list or tuple of colors instead of just 1 color to plt.bar . 您需要将颜色列表或元组传递给plt.bar而不是仅传递一种颜色。 You can do so by creating a color dictionary, then building the list of color. 您可以通过创建颜色字典,然后构建颜色列表来实现。

new = sorted(new, key=lambda score: score[1], reverse=True) 
# save the reporting type as R
X, Y, R = zip(*new)    

# create color dictionary
color_dict = {'reported':'green', 'UNREPORTED':'red'}

plt.figure(figsize = (20, 10))
mytitle = "Most common genes coexpressed with {gene1}, {gene2}, {gene3}, {gene4}".format(
    gene1="Axin2", gene2="Lef", gene3="Nkd1", gene4="Lgr5")
plt.title(mytitle, fontsize=40)

plt.ylabel('Number of same gene encounters across studies', fontsize=20)
# build the colors from the color dictionary
ax = plt.bar(range(len(X)), Y, 0.6, tick_label = X, color=[color_dict[r] for r in R])

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

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