Phlya / adjustText

A small library for automatically adjustment of text position in matplotlib plots to minimize overlaps.
https://adjusttext.readthedocs.io/
MIT License
1.5k stars 87 forks source link

Fix using correct canvas for drawing and input empty list #141

Closed oscargus closed 1 year ago

oscargus commented 1 year ago

Fixes #140 Fixes #137 (I also had this issue)

The problem was that the way I was using it, from inside a library, for some reason the draw didn't happen with plt.draw(). Now, it should always work when passing ax (and seems to work for me even without it).

Modifying my own code to deal with #137 is simple, but considering that more people seem to be affected, I added a minor fix for that as well.

(Seems like my editor modified another line as well, hope that is OK...)

Phlya commented 1 year ago

This looks great, thank you! Do you know the different between plt.draw() and ax.figure.canvas.draw()?

Also, perhaps raising an error in case of no passed texts would be better than simply returning? Or at least a warning...

oscargus commented 1 year ago

plt.draw does canvas.draw_idle which "Request a widget redraw once control returns to the GUI event loop." So I guess that if it is used outside of a GUI event loop, or if the GUI does not idle before the next step, it may not behave as expected.

I modified it now to use figure.draw_without_rendering, which only does the positioning of things, without the actual figure generation.

One may argue about the empty list case. In my library I only sometimes get texts that I want to apply adjust_text to. So for me it was convenient to just get that list in, empty or not. Not a big deal to add a check in my code, but since the previous behavior was that it did work with empty lists, I would probably opt for that (unless there starts to come in bug reports about nothing happening that originates from people passing in empty lists...).

(Now, I run adjust_text a configurable number of times, so I realize that I still may want to check if it is empty or not for a bit of micro-optimization, but anyway...)

oscargus commented 1 year ago

Oh, and I think that returning 0 is a quite good indication that nothing has happened.

Phlya commented 1 year ago

OK, I am happy to merge this! Thanks a lot. I'll use the figure.draw_without_rendering method in the future... Matplotlib has too many variations on the "draw" idea!