pandas 如何将matplotlib条形图中的数据标签旋转90度?[副本]

dzjeubhm  于 2023-06-20  发布在  其他
关注(0)|答案(1)|浏览(135)

此问题已在此处有答案

How to add value labels on a bar chart(7个回答)
How to plot and annotate a grouped bar chart(1个答案)
11天前关闭
我在条形图中使用了这样的方法,每个条形图的外部都有数据标签(每个条形图的实际值):

import matplotlib.pyplot as plt
import numpy as np

labels = ['G1', 'G2', 'G3', 'G4', 'G5']
men_means = [20, 34, 30, 35, 27]
women_means = [25, 32, 34, 20, 25]

x = np.arange(len(labels))  # the label locations
width = 0.35  # the width of the bars

fig, ax = plt.subplots()
rects1 = ax.bar(x - width/2, men_means, width, label='Men')
rects2 = ax.bar(x + width/2, women_means, width, label='Women')

# Add some text for labels, title and custom x-axis tick labels, etc.
ax.set_ylabel('Scores')
ax.set_title('Scores by group and gender')
ax.set_xticks(x, labels)
ax.legend()

ax.bar_label(rects1, padding=3)
ax.bar_label(rects2, padding=3)

fig.tight_layout()

plt.show()

结果如下:

如何将每个条形图顶部的数据标签旋转90度?我不是在问xtick标签。

qyyhg6bp

qyyhg6bp1#

bar_label在后台使用Text,并且可以接受它的参数,所以这里我们可以传递rotation=90rotation='vertical'

ax.bar_label(rects1, padding=3, rotation=90)
ax.bar_label(rects2, padding=3, rotation=90)

相关问题