Skip to content

fill_between incorrect with log y-axis and value 0 #8623

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
cdeil opened this issue May 15, 2017 · 11 comments
Closed

fill_between incorrect with log y-axis and value 0 #8623

cdeil opened this issue May 15, 2017 · 11 comments

Comments

@cdeil
Copy link
Contributor

cdeil commented May 15, 2017

fill_between doesn't give the result I expect if using a log y-axis and having a value of 0 in the y1 array.

Example:

%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt

x = np.array([1, 2, 3, 4, 5])
y1 = np.array([9, 2, 8, 3, 0])
y2 = np.array([10, 10, 10, 10, 10])

plt.plot(x, y1, lw=3, color='green')
plt.plot(x, y2, lw=3, color='red')
plt.fill_between(x, y1, y2)
plt.loglog()

Actual outcome:

The polygon created by fill_between somehow connects the last and first point, resulting in a completely incorrect patch:

image

Expected outcome

I was expecting / hoping that the band would be filled correctly and simply clipped (i.e. the result I get when changing the last y1 value from 0 to e.g. 1e-10). In the plot, the blue fill should be between the red and green line.

Matplotlib version

This is with Matplotlib 2.0.0.
(on Macports, but I don't think that matters)

@dstansby dstansby added this to the 2.1 (next point release) milestone May 15, 2017
@dstansby
Copy link
Member

Confirmed on master, thanks for the code sample.

@dstansby
Copy link
Member

Slightly abstractly, y=0 is mapped to x = -infinity on a log scale, so I would expect the fill between to be clipped at the vertical line x=4 in the above example, and for the x=5, y=0 point to be ignored.

@cdeil
Copy link
Contributor Author

cdeil commented May 15, 2017

@dstansby - Thanks for looking into the issue!

When creating the issue I forgot to mention #7371 .
I don't quite follow the discussion there, but it might be relevant to decide what to do here.
For me, any solution that doesn't have this issue that the last y1 point connects to the first y2 point would be fine.

Also, if someone else has this issue, I wanted to mention that it's possible to work around the issue using where and filtering out the non-positive y values:

where = (y_hi > 0) & (y_lo > 0) & (other possible selections you might have)
ax.fill_between(energy.value, y_lo.value, y_hi.value, where=where)

This is what I'm doing for now.

@ghost
Copy link

ghost commented May 30, 2017

@cdeil and @dstansby I think this issue was already dealt with using the current latest code in master. You just have to call plt.loglog(nonposx='clip', nonposy='clip') (rather than the default plt.loglog() with no **kwargs). At least, it works on my machine. I didn't try it out, but I don't think you need nonposx = 'clip' if there's no 0 in your x-axis.
figure_1

@cdeil
Copy link
Contributor Author

cdeil commented Jul 3, 2017

@Tuan333 - Yes, indeed nonposy='clip' resolves the issue, thanks for pointing it out.

The main remaining issue is that we have to teach our users about it, because if they call plt.loglog without that option after calling the plotting functions from our package, they will still see that issue.

Close this issue?
Or is there anything that can be improved on the matplotlib side (e.g. change default behaviour)?

@anntzer
Copy link
Contributor

anntzer commented Jul 3, 2017

Probably related to #8537?

@dstansby
Copy link
Member

dstansby commented Jul 3, 2017 via email

@tacaswell
Copy link
Member

@dstansby Won't #8836 make this worse (by changing more things to mask rather than clip)?

@dstansby
Copy link
Member

dstansby commented Jul 6, 2017

I don't think it'll make it worse than the image in the original bug report, but it certainly doesn't fix it.

@tacaswell tacaswell modified the milestones: 2.1 (next point release), 2.2 (next next feature release) Sep 24, 2017
@afvincent
Copy link
Contributor

Currently, the MWE results in
mwe_with_master
with Matplotlib from master (2.1.0.post476+g30b58001f). (The issue is still present with Matplotlib 2.1 from conda)

@tacaswell @anntzer
Should we close this issue (didn't #9477 fixed it?)?

@tacaswell
Copy link
Member

I think this is fixed by #9477

@tacaswell tacaswell modified the milestones: v2.2, v2.1.1 Nov 1, 2017
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

5 participants