Note

Go to the endto download the full example code.

Align tick labels#

By default, tick labels are aligned towards the axis. This means the set ofy tick labels appear right-aligned. Because the alignment reference pointis on the axis, left-aligned tick labels would overlap the plotting area.To achieve a good-looking left-alignment, you have to additionally increasethe padding.

align ticklabels
importmatplotlib.pyplotaspltpopulation={"Sydney":5.2,"Mexico City":8.8,"São Paulo":12.2,"Istanbul":15.9,"Lagos":15.9,"Shanghai":21.9,}fig,ax=plt.subplots(layout="constrained")ax.barh(population.keys(),population.values())ax.set_xlabel('Population (in millions)')# left-align all ticklabelsforticklabelinax.get_yticklabels():ticklabel.set_horizontalalignment("left")# increase paddingax.tick_params("y",pad=70)

Gallery generated by Sphinx-Gallery