1

I'm using something like this for bar charts with data labels (actual value of each bar) on the outside of each bar:

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()

The result looks like this:

enter image description here

How can I rotate the data labels at the top of each bar by 90 degrees? I'm not asking about xtick labels.

tdy
  • 36,675
  • 19
  • 86
  • 83
CentAu
  • 10,660
  • 15
  • 59
  • 85

1 Answers1

3

bar_label uses Text under the hood and can accept its parameters, so here we can pass either rotation=90 or rotation='vertical':

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

tdy
  • 36,675
  • 19
  • 86
  • 83