2017-03-13 260 views
0

在Python中,我怎樣才能讓'reported'酒吧綠色和'UNREPORTED'酒吧紅色? 我想給我的圖表中的每個報告和未報告的欄添加不同的顏色。Python: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, _ = 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) 
+0

的[Matplotlib改變欄的顏色,如果滿足條件]可能的複製(http://stackoverflow.com/questions/34898146/matplotlib-change-the-bar-color-if-a-condition-是-MET) – tom

回答

0

您需要的顏色,而不是僅僅1顏色列表或元組傳遞給plt.bar。你可以通過創建一個顏色詞典來完成,然後建立顏色列表。

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]) 
1

您可以迭代欄並檢查給定索引是否爲'UNREPORTED'。如果是這種情況,請使用set_color對欄進行着色。

enter image description here

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()