2017-02-03 1 views
1

그래서 많은 항목이있는 가로 막 대형 차트를 만듭니다. y 축에는 x 축에 레이블/항목이있는 값이 표시됩니다. 그래서 나는 모두 다른 높이의 막대를 많이 얻습니다.축 레이블의 색/글꼴 두께가 목록에 속하는 경우 변경하십시오.

이제 x 축 레이블/항목 중 일부는 다른 것들보다 중요합니다. 그래서 모든 중요한 레이블/항목을 포함하는 목록을 만들었습니다. 내 생각은 이제 목록에 포함 된 레이블/항목의 색상이나 글꼴 두께를 변경 (굵게 표시)하고 싶습니다. 하지만 어떻게해야할지 모르겠습니다.

내가 지금 사용하고 플롯 코드는 다음과 같습니다

indexes 각 라벨/항목의 인덱스입니다
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() 

, 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

관련 문제