1

Often times I want to place text at/about a certain point, but I want the text to automatically position itself so that it is not interfering with lines, points, and other objects attached to the axes. I want it to find the best possible spot about the point. Does matplotlib have something built in like this?

Here is an example case:

import matplotlib.pyplot as plt


fig, ax = plt.subplots()
ax.plot([0, 10], [0, 10])  # plot the arbitrary line
ax.scatter(5, 5)
ax.annotate('Hello There', (5, 5), va='top', bbox=dict(facecolor='white', edgecolor='white', pad=0))

ax.grid()
plt.show()

enter image description here

This case is alright, but it would be awesome if matplotlib could add cushion to the location of the point so that it doesn't eat into the dot. Also, what if the line were arbitrary and expected to change. If this is the case, there would be instances where the text is interfering with the line like so: enter image description here

If this feature were to exist, for this case it would change the vertical alignment of the the text to 'bottom' to reduce the interference. Is there anything like that out there?

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Gabe Morris
  • 804
  • 4
  • 21

1 Answers1

0

look at matplotlib.pyplot.annotate xytext xytext It is not a complete answer to your question but it is the best workaround I have found

  • Not quite what I'm looking for. The `xytext` is just the coordinates for the text for when you want the text coordinates to differ from the point coordinates that's being annotated. Not a solution to automatically position in the best place because it is statically being determined. I'm looking for same kind of behavior as `plt.legend(loc='best')`. I don't think what I'm looking for exists though. – Gabe Morris Jul 27 '22 at 01:40
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 28 '22 at 03:53