Matthias Arras
Matthias Arras

Reputation: 740

Padding text in matplotlib.text

I am looking for a reasonable way to pad text for matplotlib.text. I saw the bbox has a nice pad option but it does only pad the box and does not move the text. Also strangely enough bbox={'linestyle':'None'} does not remove the box line, however, other linestyles are working as expected. Any suggestions?

Use case is relative positioning of text on the plotting area. Using coordinates (1,0.5) puts it too close to the plotting boundary line to look good, and somehow I am reluctant to do (0.99,0.5). That would require change if someone changes fontsize etc.

Upvotes: 2

Views: 2515

Answers (1)

Diziet Asahi
Diziet Asahi

Reputation: 40667

One option is to use annotate(), which is more flexible than text. You can easily offset the text from the anchor point by a certain number of points (or pixels).

fig, ax = plt.subplots()
ax.annotate('test', xy=(1,0.5), xytext=(-5,0), xycoords='axes fraction', textcoords='offset points', ha='right')

enter image description here

Upvotes: 5

Related Questions