2016-03-13 82 views
0

我想用示例圖表中的文本標註圖的座標軸。具體而言,我想用不同的標題(XYZ,ABC,MNO等以紅色顯示)註釋軸的區域。用matplotlib中的文本標註座標軸

我生成使用此示例(繪製條形圖)圖表:http://matplotlib.org/examples/api/barchart_demo.html

誰能幫我得出這樣的線以及沿軸添加文本?任何指向示例的指針也是值得讚賞的。我不知道除了用圖片描述之外,還有什麼其他的表達我想要做的。

Example figure showing the text annotation along X and Y axes (shown using XYZ, MNO, etc.) in red

+2

告訴我們你嘗試過什麼到目前爲止相同 –

回答

1

的文件將幫助,可以在這裏找到http://matplotlib.org/users/annotations_intro.html的快速閱讀。我已經使用了文檔中描述的註釋功能

下面是一段代碼,它可以滿足您對x軸的要求。此代碼的大部分內容來自您在問題中提供鏈接的示例。

N = 5 
menMeans = (20, 35, 30, 35, 27) 
menStd = (2, 3, 4, 1, 2) 
ind = np.arange(N) # the x locations for the groups 
width = 0.35  # the width of the bars 
fig, ax = plt.subplots() 
rects1 = ax.bar(ind, menMeans, width, color='r', yerr=menStd) 
womenMeans = (25, 32, 34, 20, 25) 
womenStd = (3, 5, 2, 3, 3) 
rects2 = ax.bar(ind + width, womenMeans, width, color='y', yerr=womenStd) 

# add some text for labels, title and axes ticks 
ax.set_ylabel('Scores') 
ax.set_title('Scores by group and gender') 
ax.set_xticks(ind + width) 
ax.set_xticklabels(('G1', 'G2', 'G3', 'G4', 'G5')) 
ax.legend((rects1[0], rects2[0]), ('Men', 'Women')) 

######### annotating the x axis ######### 
ax.annotate('', xy=(0, -2),xytext=(3,-2.09),      #draws an arrow from one set of coordinates to the other 
      arrowprops=dict(arrowstyle='<->',facecolor='red'), #sets style of arrow and colour 
      annotation_clip=False)        #This enables the arrow to be outside of the plot 

ax.annotate('xyz',xy=(1.1,-3.8),xytext=(1.3,-3.8),    #Adds another annotation for the text that you want 
      annotation_clip=False) 


ax.annotate('', xy=(3.1, -2),xytext=(5,-2.09),     #Repeating for however many arrows you want under the axes 
      arrowprops=dict(arrowstyle='<->',facecolor='red'), 
      annotation_clip=False) 

ax.annotate('abc',xy=(3.6,-3.8),xytext=(3.9,-3.8), 
      annotation_clip=False) 

######## Can add further annotations for the y-axis here similar to the above ######## 



# by changing the coorinates of the above you can repeat this for the y axis too 
def autolabel(rects): 
    # attach some text labels 
    for rect in rects: 
     height = rect.get_height() 
     ax.text(rect.get_x() + rect.get_width()/2., 1.05*height, 
       '%d' % int(height), 
       ha='center', va='bottom') 
autolabel(rects1) 
autolabel(rects2) 
plt.show() 

這給下面的圖片:

enter image description here

您將需要複製這個以做y軸

相關問題