matplotlib / matplotlib

matplotlib: plotting with Python
https://matplotlib.org/stable/
20.17k stars 7.61k forks source link

[Bug]: symlog scale generates false warning when mouse is moved #26118

Closed jxjo closed 1 year ago

jxjo commented 1 year ago

Bug summary

When using symlog scale for an axis, a false warning is initially generated - independant of the data to plot - as soon as the mouse is moved over the axes.

Code for reproduction

import matplotlib.pyplot as plt

ax = plt.figure().add_subplot()
ax.set_yscale('symlog')

plt.show()

Actual outcome

UserWarning: All values for SymLogScale are below linthresh, making it effectively linear. You likely should lower the value of linthresh.

Expected outcome

"Nothing"

Additional information

... maybe related to TkAgg?

Operating system

Windows

Matplotlib Version

3.7.0

Matplotlib Backend

TkAgg

Python version

3.11.2

Jupyter version

No response

Installation

pip

tacaswell commented 1 year ago

I do not think this warning is erroneous and I see it as soon as the window shows (not on mouse motion). It is warning you that given the current data limits symlog is not doing anything for you (because you are fully in the linear range).

If you do:

import matplotlib.pyplot as plt

ax = plt.figure().add_subplot()
ax.set_yscale('symlog')
ax.set_ylim(0, 1000)
plt.show()

you will not see the warning.

tacaswell commented 1 year ago

I was fooled by the default limits triggering the warning.

I can reproduce this on mouse motion.

ksunden commented 1 year ago

https://github.com/matplotlib/matplotlib/blob/475612ef50a7d4881eebaa0ebe5b410b7b202b28/lib/matplotlib/scale.py#L396

ndarray.all() is true for an empty ndarray, and thus this should probably be changed to

if len(abs_a)>0 and ...:

or short circuit and return earlier, before even calling abs.

github-actions[bot] commented 1 year ago

Good first issue - notes for new contributors

This issue is suited to new contributors because it does not require understanding of the Matplotlib internals. To get started, please see our contributing guide.

We do not assign issues. Check the Development section in the sidebar for linked pull requests (PRs). If there are none, feel free to start working on it. If there is an open PR, please collaborate on the work by reviewing it rather than duplicating it in a competing PR.

If something is unclear, please reach out on any of our communication channels.

jxjo commented 1 year ago

... yep - the question is, "who" sends these false empty values as the warning is also raised if valid data is plotted...

jklymak commented 1 year ago

I'm not a fan of having a warning here at all. People who don't understand symlog sometimes get confused, but I don't think that means we should spam potentially valid usage.

ksunden commented 1 year ago

Looks like it is actually in format_coord (specifically in this example format_xdata, which is odd, considering the y value is the one with the scale set...)

This is the method which gives the text which displays in the toolbar. It probably gets a single value, or a small number of values for neighboring pixels to determine what level of precision to display... at which point yeah...

I think I agree with @jklymak that we should not be warning here... perhaps keep a warning on setting limits (potentially even auto limits, but I'm tempted to say only when explicitly setting limits), rather than just when transforming, which is not the right place for a warning...

I'll remove the Good first issue label at least while we solidify the path forward... (I originally thought this was a simple 1-3 line change, but now see it is a bit more complicated)

ksunden commented 1 year ago

Actually, it is in the inverted symlog transform (and only the inverted transform)..., it was originally implemented for mpl 3.7.0 in #24555, but the idea stems from the issue #24550.

I don't know that there is a much better hook if we do want a warning somewhere, but it does feel odd/limiting to warn whenever points in only the linear region are passed through the inverse transform...

It is equally useless to use symlog (compared to just log) if all your points are in the positive side of the log portion of symlog, but there is no warning then (and in fact if you set ylim to (100, 1000) before showing there is no warning for the example...)... It is less unexpected as you setting to a log behavior and getting a log behavior, but still.

Perhaps @timhoffm can weigh in here, as author of the issue requesting this warning.

jxjo commented 1 year ago

... from my point of view, the warning itself is not the "problem". Here is a valid example throwing the same warning, when mouse is moved:

    import matplotlib.pyplot as plt
    ax = plt.figure().add_subplot()

    ax.set_yscale('symlog')
    x = np.linspace(-100,100,200)
    ax.plot(x, x)

    plt.show()

I think, there are 2 issues. The bug in transform_non_affine, which should be like ...

... and the bug in the mouse coordinate driver, which sends obviously (see above @ksunden ) None or length==0 values.


Regarding the warning itself:

If I have one diagram which I use for different datasets - and all points of one dataset are within the linear threshold, then this is a valid case - no reason to warn.

Or in common: The warning is wrong ;-) and should be removed again.


For completeness: As a workaround the warning can be suppressed for example with

    import warnings
    warnings.filterwarnings("ignore", message = "All values for SymLogScale")
anntzer commented 1 year ago

FWIW I would agree with removing the warning.

tacaswell commented 1 year ago

In further inspection, it looks like this is warning on the inverse (not the forward) transform that happens to also be called in the case that is tested (as part of sorting out how much the margin should be).

Even if we move this to the correct transform, there are still too many places where we want to pass data just in range (e.g. we call the forward transform on mouse motion to estimate the pixel-to-pixel data range and ensure that the values printed in the toolbar are reasonably precise)

I am going to open a PR to revert #24555 and reopen #24550.