Skip to content

Autoscaling on log axis with negative data results in arbitrary limits. #10782

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ImportanceOfBeingErnest opened this issue Mar 14, 2018 · 7 comments

Comments

@ImportanceOfBeingErnest
Copy link
Member

Bug report

Bug summary

Plotting negative values on a log scale is possible. However it changes the axis limits in a seemingly arbitrary way.

Code for reproduction

The following shows three scatter plots. The x values
(1) are all positive
(2) contain one negative value
(3) have the negative value filtered out

import numpy as np; np.random.seed(1)
import matplotlib.pyplot as plt

x = np.random.rayleigh(100,400)
y = np.random.rand(len(x))

fig, (ax,ax2, ax3) = plt.subplots(nrows=3)

ax.scatter(x,y)
ax.set_xscale("log")
ax.set_title("Scatter of all-positive values")

ax2.scatter(x-2,y)
ax2.set_xscale("log")
ax2.set_title("Scatter with one negative value")

cond = x-2 > 0
ax3.scatter((x-2)[cond],y[cond])
ax3.set_xscale("log")
ax3.set_title("Expected scatter with one negative value")

plt.tight_layout()
plt.show()

Actual outcome

The second subplot, where the data contains a negative value, shows only part of the valid data, because the axis is autoscaled to some seemingly arbitrary region.

image

Expected outcome

The third subplot would be the expected outcome with the axis autoscaled to show all points.

One could also argue that plotting negative data on a log scale should not be supported at all, and that it should be the user's responsibility to make sure the data is positive. In that case however it would make sense to issue a warning instead of just plotting something arbitrary.

Matplotlib version

  • Operating system: Windows 8.1
  • Matplotlib version: 2.2
  • Matplotlib backend: any
  • Python version: 2.7.10
@jklymak
Copy link
Member

jklymak commented Mar 14, 2018

What happens if you set the scale before calling scatter? Loglog etc check for negative I think

@ImportanceOfBeingErnest
Copy link
Member Author

Good point, but setting the scale before the plotting does not make any difference, i.e.

ax2.set_xscale("log")
ax2.scatter(x-2,y)

results in the same plot as above.

@dstansby
Copy link
Member

dstansby commented Apr 3, 2018

I think this might be a duplicate of #7733

EDIT it isn't.

@ImportanceOfBeingErnest
Copy link
Member Author

@dstansby Not sure; the other post seems to give the correct result (0 not being in the plot, but the axis limits are correct and the plot shows all points.). Setting bottom=0 may not make much sense for a log axis, so the warning is expected?

Here, we have no warning and limits are just arbitrarily shifted.

@dstansby
Copy link
Member

dstansby commented Apr 3, 2018

I still slightly suspect that they might have a common cause, but I'm not sure. I'll try and dig into this one to see what's happening.

@github-actions
Copy link

github-actions bot commented May 2, 2023

This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!

@github-actions github-actions bot added the status: inactive Marked by the “Stale” Github Action label May 2, 2023
@QuLogic
Copy link
Member

QuLogic commented May 2, 2023

This was fixed by #18642.

@QuLogic QuLogic closed this as completed May 2, 2023
@QuLogic QuLogic added this to the v3.4.0 milestone May 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants