0

I am plotting some points on a line in python using matplotlib, and whenever the point is at/near the boundaries of the plot the annotated text is hard to read due to overlapping axes labels and such (see screenshot below): screenshot of plot

I'm currently using code like this to place my point annotations manually:

# add value text to x, y point
  jt = x_points_to_plot  # a single x-value, in this case
  f = ys_func(x_points_to_plot)  # a single y-value, in this case
  ax.annotate(
    '({}C, {:0.0f}%)'.format(jt, f),  # the string text to add
    xy=(jt + 1, f + 5),  # offset the text from the point manually
    ha='center')

Usually my points are in the middle and look acceptable, like this: screenshot of a plot that looks decent

But I don't want to manually adjust the text for every point, because I have a lot of changing data and it's not where I want to spend my time; instead, I'd love to find a way to accommodate the text so it it easily readable on the plot. Maybe I could expand the plot to contain the new text, or I could move the text to a different place depending on a set of conditions about what might be near the text? I'm not sure...

I think the best answer will be one I can reuse for other projects, robust to points anywhere on the plot, and relatively easy to implement (least amount of custom functions or "hacks" that I would have to recreate for every project). Thanks a ton in advance!

123
  • 595
  • 6
  • 18
  • 1
    You may get some ideas from [Matplotlib overlapping annotations](https://stackoverflow.com/questions/8850142/matplotlib-overlapping-annotations) and [Matplotlib overlapping annotations / text](https://stackoverflow.com/questions/19073683/matplotlib-overlapping-annotations-text) – ImportanceOfBeingErnest Jul 01 '19 at 21:00
  • Yeah the [adjustText](https://github.com/Phlya/adjustText) library on github looks promising, thanks! – 123 Jul 02 '19 at 03:24

0 Answers0