Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print Y axis label horizontally in a matplotlib / pylab chart?

I'm creating very simple charts with matplotlib / pylab Python module. The letter "y" that labels the Y axis is on its side. You would expect this if the label was longer, such as a word, so as not to extend the outside of the graph to the left too much. But for a one letter label, this doesn't make sense, the label should be upright. My searches have come up blank. How can I print the "y" horizontally?

like image 858
Karl D Avatar asked Dec 27 '14 21:12

Karl D


People also ask

How do I rotate Y label in matplotlib?

Rotate Y-Axis Tick Labels in Matplotlib Firstly, you can change it on the Figure-level with plt. yticks() , or on the Axes-lebel by using tick. set_rotation() or by manipulating the ax.

How do I change the position of a label in matplotlib?

With matplotlib version 3.3. 0, the matplotlib functions set_xlabel and set_ylabel have a new parameter “loc” that can help adjust the positions of axis labels. For the x-axis label, it supports the values 'left', 'center', or 'right' to place the label towards left/center/right.


2 Answers

It is very simple. After plotting the label, you can simply change the rotation:

import matplotlib.pyplot as plt  plt.ion() plt.plot([1, 2, 3])  plt.ylabel("y", rotation=0) # or # h = plt.ylabel("y") # h.set_rotation(0)  plt.draw() 
like image 74
Jens Munk Avatar answered Sep 23 '22 06:09

Jens Munk


Expanding on the accepted answer, when we work with a particular axes object ax:

ax.set_ylabel('abc', rotation=0, fontsize=20, labelpad=20) 

Note that often the labelpad will need to be adjusted manually too — otherwise the "abc" will intrude onto the plot.

From brief experiments I'm guessing that labelpad is the offset between the bounding box of the tick labels and the y-label's centre. (So, not quite the padding the name implies — it would have been more intuitive if this was the gap to the label's bounding box instead.)

like image 21
Evgeni Sergeev Avatar answered Sep 24 '22 06:09

Evgeni Sergeev