Matplotlib bar semilogy not show full y labels

Question:

How to show y labels on below figure?. Matplotlib cut some y labels.

fig, ax = plt.subplots(1,1,figsize = (8,5))

sumsqrt_data ={'PI-ZN' : PI_ZN, 'PI-CHR' : PI_CHR, 'PI-AMIGO' : PI_AMIGO, 'PI-IMC' : PI_IMC, 'PI-SIMC' : PI_SIMC}

courses = list(['PI-ZN', 'PI-CHR', 'PI-AMIGO', 'PI-IMC', 'PI-SIMC'])
values = list([9975.229678280088, 16002.075925234263, 13022.883821302505, 11142.898237215246, 7633.880494079887])

ax.bar(courses, values)
ax.set_yscale('log')

enter image description here

Asked By: Rariusz

||

Answers:

In this particular case, as the range of y-values is quite limited, a log scale seems to add confusion instead of making the plot easier to interpret.

You can add more ticks to the log-scaled y-axis, e.g. via a MultipleLocator.

Or you can just use the standard linear scale. Optionally, you can reduce the bottoms of the bars. The function ax.bar_label annotates the individual heights.

import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator

fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=(16, 5))

courses = ['PI-ZN', 'PI-CHR', 'PI-AMIGO', 'PI-IMC', 'PI-SIMC']
values = [9975.229678280088, 16002.075925234263, 13022.883821302505, 11142.898237215246, 7633.880494079887]

bars = ax1.bar(courses, values)
ax1.bar_label(bars, fmt='%.1f')
ax1.set_yscale('log')
ax1.yaxis.set_major_locator(MultipleLocator(1000))
ax1.set_title('log scaled y axis')

bars = ax2.bar(courses, values)
ax2.bar_label(bars, fmt='%.1f')
# ax2.set_ylim(ymin=7000) # if needed, cut off part of the bars
ax2.set_title('linear y axis')
plt.tight_layout()
plt.show()

setting more y ticks for log scale

Answered By: JohanC
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.