Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change text justification in Matplotlib labels?

I have made a graph in Matplotlib and want to change the justification of my rotated y-axis label.

In this code example, the text "This is an example plot" should be justified left.

import matplotlib.pyplot as plt
figure, axes = plt.subplots()
axes.plot([0, 1], [0, 1])
axes.set_ylabel('This is an\nexample plot', rotation=0)
axes.yaxis.set_label_coords(0.1, 0.9)
plt.show()

plot figure with a label with centered text

like image 361
Michael Gomes Vieira Avatar asked Mar 08 '26 01:03

Michael Gomes Vieira


1 Answers

Matplotlib text commands, such as for labels and annotations, support a number of keyword arguments that affect alignment, orientation, spacing, font, etc. The full list of text properties can be found in the documentation. In the above example

axes.set_ylabel('This is an\nexample plot', rotation=0, horizontalalignment='left')

would do the trick.

As noted in the other answer, this particular example is an abuse of the y-axis label. But the general principle is the same for all kinds of Matplotlib labels.

like image 98
john-hen Avatar answered Mar 09 '26 13:03

john-hen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!