Reputation: 10650
I would like to move my axis label Number of States
to the left, so that it is actually over the numbers i have. Other similar questions/answers have suggested using labelpad
, but this shifts the text up or down, not left/right. How can i move my title to the right?
i've also tried the horizontalalignment
kwarg, which a. seems to have the right
and left
alignments reversed, and also does not move the title far enough, nor offer any actual control on where exactly it goes.
i see that i can set the _x
and _y
properties of the Text
instance, using set_[xy]()
, but it seems a bit hacky. Is there a convenient way i can set the location of hte title relative to a value on the xaxis?
Upvotes: 0
Views: 136
Reputation: 20831
You can grap the title object and set its respective position property to whatever you like. A simple example could be:
%matplotlib inline
import matplotlib.pyplot as plt
plt.plot([1,2,3])
ti=plt.title('foo')
ti.set_position((0.2,1))
which creates a plot like
Note that the position is set in relative coordinates.
The position argument suggested by @ThePredator (loc : {‘center’, ‘left’, ‘right’}, see docs also works in a similar fashion.
To set the position of the text in the data coordinate system instead of the axis coordinates, a simple transformation can be used. For details have a look at the Transformation Documentation. A minimal example could look like:
%matplotlib inline
import matplotlib.pyplot as plt
f,ax = plt.subplots(1)
ax.plot([1,2,3])
ti=ax.set_title('foo')
ti.set_position(ax.transLimits.transform((0.5,3)))
This places the title centered at (0.5, 3) as shown in the following plot
Upvotes: 2