matplotlib set yaxis label size
Asked Answered
T

2

97

How can I change the size of only the yaxis label? Right now, I change the size of all labels using

pylab.rc('font', family='serif', size=40)

but in my case, I would like to make the y-axis label larger than the x-axis. However, I'd like to leave the tick labels alone.

I've tried, for example:

pylab.gca().get_ylabel().set_fontsize(60)

but I only get:

AttributeError: 'str' object has no attribute 'set_fontsize'

So, obviously that doesn't work. I've seen lots of stuff for tick sizes, but nothing for the axis labels themselves.

Tonytonya answered 1/5, 2012 at 21:39 Comment(0)
E
158

If you are using the 'pylab' for interactive plotting you can set the labelsize at creation time with pylab.ylabel('Example', fontsize=40).

If you use pyplot programmatically you can either set the fontsize on creation with ax.set_ylabel('Example', fontsize=40) or afterwards with ax.yaxis.label.set_size(40).

Extrabold answered 1/5, 2012 at 21:51 Comment(0)
N
0

ax.get_ylabel() returns a string, which doesn't define a set_fontsize method but ax.yaxis.get_label() returns a matplotlib.text.Text object, which does; so the following can be used to change fontsize of ylabel as well.

ax.yaxis.get_label().set_fontsize(40)

You can also change the position of the ylabel by calling set() on Text object:

ax.yaxis.get_label().set(fontsize=40, position=[0.5,0.7])
Nameless answered 4/3, 2023 at 1:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.