Python 在matplotlib中使用文本注释轴

Python 在matplotlib中使用文本注释轴,python,matplotlib,annotations,Python,Matplotlib,Annotations,我想用文本注释绘图的轴,如示例图表。具体来说,我想用不同的标题(XYZ、ABC、MNO等,以红色显示)注释轴的区域 我使用以下示例生成了图表(绘制条形图): 有人能帮我画这样的线,并沿轴添加文字吗?任何指向示例的指针都值得赞赏。除了用图片描述之外,我不知道如何表达我想在这里做什么 快速阅读文档会有所帮助,可以在此处找到。我使用了文档中描述的注释函数 下面是一段代码,它将执行x轴所需的操作。这段代码的大部分内容取自您在问题中提供链接的示例 N = 5 menMeans = (20, 35, 30,

我想用文本注释绘图的轴,如示例图表。具体来说,我想用不同的标题(XYZ、ABC、MNO等,以红色显示)注释轴的区域

我使用以下示例生成了图表(绘制条形图):

有人能帮我画这样的线,并沿轴添加文字吗?任何指向示例的指针都值得赞赏。除了用图片描述之外,我不知道如何表达我想在这里做什么


快速阅读文档会有所帮助,可以在此处找到。我使用了文档中描述的注释函数

下面是一段代码,它将执行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()
N=5
menMeans=(20,35,30,35,27)
menStd=(2,3,4,1,2)
ind=np.arange(N)#组的x个位置
宽度=0.35#钢筋宽度
图,ax=plt.子批次()
rects1=ax.bar(ind,menMeans,width,color='r',yerr=menStd)
女性平均数=(25,32,34,20,25)
女性TD=(3,5,2,3,3)
rects2=最大条(ind+宽度,女性平均值,宽度,颜色=y',yerr=womenStd)
#为标签、标题和轴标记添加一些文本
ax.set_ylabel(‘分数’)
ax.设置标题(“按组和性别划分的分数”)
最大设置宽度(索引+宽度)
ax.setxticklabel(('G1','G2','G3','G4','G5'))
ax.图例((rects1[0],rects2[0]),('Men','Women'))
#########注释x轴#########
ax.annotate(“”,xy=(0,-2),xytext=(3,-2.09),#从一组坐标到另一组坐标绘制箭头
arrowprops=dict(arrowstyle='',facecolor='red'),#设置箭头样式和颜色
注释_clip=False)#这使箭头位于绘图之外
ax.annotate('xyz',xy=(1.1,-3.8),xytext=(1.3,-3.8),#为所需的文本添加另一个注释
注释(U剪辑=假)
ax.annotation(“”,xy=(3.1,-2),xytext=(5,-2.09),#在轴下重复任意多个箭头
arrowprops=dict(arrowstyle='',facecolor='red'),
注释(U剪辑=假)
ax.注释('abc',xy=(3.6,-3.8),xytext=(3.9,-3.8),
注释(U剪辑=假)
########可以在此处为y轴添加与上面类似的其他注释########
#通过更改上面的坐标,您也可以对y轴重复此操作
def自动标签(rects):
#附加一些文本标签
对于矩形中的矩形:
高度=矩形。获取高度()
ax.text(rect.get_x()+rect.get_width()/2,1.05*高度,
“%d”%int(高度),
ha='center',va='bottom')
自动标签(rects1)
自动标签(rects2)
plt.show()
这将显示下图:


为了对y轴做同样的操作,你需要复制这个,告诉我们你到目前为止都做了些什么嗯,尝试这个,它对文本有效,但我看不到水平轴下的箭头。我需要
自动标签功能吗?
facecolor='red'
是干什么用的?我在那里没有看到任何红色。@CGFoX,在pyplot的较新版本中,箭头样式是用单词而不是标记指定的。看见设置
arrowstyle=“simple”
对我很有用。