的文件將幫助,可以在這裏找到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()
這給下面的圖片:
您將需要複製這個以做y軸
告訴我們你嘗試過什麼到目前爲止相同 –