弄清楚了。如果我遍历数据帧每一行的列,则可以建立一个所需标签的列表,这些标签与中矩形的进行匹配
ax.patches。解决方案如下:
labels = []for j in df.columns: for i in df.index: label = str(j)+": " + str(df.loc[i][j]) labels.append(label)patches = ax.patchesfor label, rect in zip(labels, patches): width = rect.get_width() if width > 0: x = rect.get_x() y = rect.get_y() height = rect.get_height() ax.text(x + width/2., y + height/2., label, ha='center', va='center')
现在只需要处理太小的条形标签。



