Chris Chris - 6 months ago 16
Python Question

Can't access matplotlib title fontsize when not centered

Not sure what's going on here. If I set

loc='left'
or
loc='right'
the
get_fontsize
method no longer points to the non-centered title.

How do I access the fontsize of a left aligned title?

import matplotlib.pyplot as plt

# Set up a blank plot
fig, ax = plt.subplots()

# Set title with fontsize of 30
ax.set_title('test_title', loc='left', fontsize=30)

# fontsize of title is 12.0?
print ax.title.get_fontsize()

12.0

Answer

From docs:

Set one of the three available axes titles. The available titles are positioned above the axes in the center, flush with the left edge, and flush with the right edge.

So, there are 3 different titles. When you execute dir(ax) you can notice there '_left_title' and '_right_title'. So:

fig, ax = plt.subplots()
ax.set_title('test_title', loc='left', fontsize=30)
print ax._left_title.get_fontsize()

30.0
Comments