2017-02-03 68 views
1

因此,我创建了一个包含大量条目的条形图。在y轴上,它只是显示x轴上所有标签/入口的值。总而言之,我得到了很多不同高度的酒吧 - 因为它应该是。现在如果轴标签属于列表,则更改轴标签的颜色/字体重量

,一些x轴的标签/条目比其他人更重要。所以我创建了一个包含所有重要标签/条目的列表。我的想法是,现在我想改变列表中包含的那些标签/条目的颜色或字体粗细(使它们为粗体)。但我真的不知道该怎么做。

我现在使用的情节代码只是:

plt.bar(indexes, values, width, color="#3F5D7D", edgecolor="#111111", align='center') 
plt.xticks(indexes, labels, fontsize=10, rotation='vertical') 
plt.xlim([-0.5,indexes.size-0.5]) 
plt.subplots_adjust(left=0.05, bottom=0.20, right=0.95, top=0.95, wspace=0.2, hspace=0.2) 
plt.show() 

其中indexes为每个标签/项的索引,values当然这些值,并在plt.xticks我只是改变了indexeslabels

我那么有标签的列表,我们称之为main_labels = ['important_label1', 'important_label2', 'important_label3'...]等。是的,现在我想它,以便当标签是这个main_labels名单将得到粗体或其它颜色的一部分。

回答

2

您可以遍历main_labels,找到标签在labels列表中的位置并更改相应的标记。

import matplotlib.pyplot as plt 
indexes = [1,2,3,5,6] 
values = [8,6,4,5,3] 
width = 0.8 
labels = ["cow","ox","pig","dear","bird"] 
main_labels = ["ox", "pig", "bird"] 


plt.bar(indexes, values, width, color="#3F5D7D", edgecolor="#111111", align='center') 
plt.xticks(indexes, labels, fontsize=10, rotation='vertical') 
plt.xlim([0.5,max(indexes)+0.5]) 
plt.subplots_adjust(left=0.05, bottom=0.20, right=0.95, top=0.95, wspace=0.2, hspace=0.2) 

ticklabels = [t for t in plt.gca().get_xticklabels()] 
for l in main_labels: 
    i = labels.index(l) 
    ticklabels[i].set_color("red") 
    ticklabels[i].set_fontweight("bold") 

plt.show() 

enter image description here