Python 固定条形图matplotlib的标签功能

Python 固定条形图matplotlib的标签功能,python,pandas,matplotlib,bar-chart,subplot,Python,Pandas,Matplotlib,Bar Chart,Subplot,我想创建两个由这两个列表组成的条形图,并构建了它 d_prime = [-0.53, -1.89, 0.76, 1.66] d_prime_2 = [-0.69, 0.23, -0.88, 1.34] plt.figure() fig = plt.gcf() fig.set_size_inches(11, 8) times = ("1400", "2000", "3000", "4000") ypos = np.arange(len(times)) plt.subplot(2,2,1) b

我想创建两个由这两个列表组成的条形图,并构建了它

d_prime = [-0.53, -1.89, 0.76, 1.66]
d_prime_2 = [-0.69, 0.23, -0.88, 1.34]

plt.figure()
fig = plt.gcf()
fig.set_size_inches(11, 8)

times = ("1400", "2000", "3000", "4000")
ypos = np.arange(len(times))

plt.subplot(2,2,1)
bar_1= plt.bar(ypos, d_prime, align='center', alpha=0.5)
plt.title('First half of the D primes of subject {}'.format(i+3))
plt.xticks(x, index, rotation = 0)

plt.legend("D' primes 1st")

plt.subplot(2,2,2)
bar_2 = plt.bar(ypos, d_prime_2, align='center', alpha=0.5, color = 'cyan')
plt.title('Second half of the D primes of subject {}'.format(i+3))
plt.xticks(ypos, times)
plt.legend("D' primes 2nd")

def autolabel(rects):
    ##Attach a text label above each bar in *rects*, displaying its height.
    for rect in rects:
        height = rect.get_height()
        plt.annotate('{}'.format(height),
                    xy=(rect.get_x() + rect.get_width() / 2, height),
                    xytext=(0, 3),  # 3 points vertical offset
                    textcoords="offset points",
                    ha='center', va='bottom')

autolabel(bar_1)
autolabel(bar_2)
plt.show()
我尝试过这样做,但它看起来像:

我想清楚地标记bar_1和bar_2,但它只标记bar_2


你能帮我修复这个功能吗?或者你建议使用什么功能吗?

问题来自第二个子图是活动轴这一事实

一个不太改变代码的快速修复方法是在轴的
autolabel
中添加一个参数:

ax1=plt.子批次(2,2,1)
...
ax2=plt.子批次(2,2,2)
...
def自动标签(ax、rects):
...
ax.注释(…)
自动标签(ax1、bar1)
自动标签(ax2、bar2)
或者只需在显示条形图之前定义函数
autolabel
,并在每个子批语句中调用该函数

def自动标签(rects):
...
...
plt.子地块(1,2,1)
bar_1=。。。
自动标签(bar_1)#在第一个子批次(活动批次)上添加注释
plt.子地块(1,2,2)#现在,当前轴是第二个轴
bar_2=。。。
自动标签(bar_2)#在第二个子批次上添加注释