2

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.

2

1 Answer 1

6

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.