Issue
I would like to have multi-line text left-aligned inside a box that is right-aligned, or rather whose position is defined in terms of its right edge.
That is, I'd like the text "the other." in the following to be left-aligned:
import matplotlib.pyplot as plt
def lowerrighttext(ax,text, size=12, alpha = 0.5, color='k', dx=1.0/20, dy=1.0/20):
return ax.annotate(text, xy=(1-dx, dy), xycoords = 'axes fraction',
ha='right',va='bottom', color=color,
size=size, bbox=dict(boxstyle="round", fc="w",
ec="0.5", alpha=alpha) )
fig,ax=plt.subplots(1)
ax.plot([0,1],[0,1])
lowerrighttext(ax,'One line is longer than\nthe other.')
plt.show()
If I were to specify ha='left'
it would apply to the text box, not just the text:
Solution
It looks like the trick is the multialignment
or ma
named argument:
import matplotlib.pyplot as plt
def lowerrighttext(ax,text, size=12, alpha = 0.5, color='k', dx=1.0/20, dy=1.0/20):
return ax.annotate(text, xy=(1-dx, dy), xycoords = 'axes fraction',
ha='right',va='bottom',ma='left',color=color, # Here
size=size, bbox=dict(boxstyle="round", fc="w",
ec="0.5", alpha=alpha))
fig,ax=plt.subplots(1)
ax.plot([0,1],[0,1])
lowerrighttext(ax,'One line is longer than\nthe other.')
plt.show()
Produces:
(Although it still seems a little off, not equidistant from the bottom and right sides, but maybe that's an effect of the figure being wider than it is tall.)
Answered By - jedwards
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.