Skip to content
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

fill_between() not working properly #6650

Closed
avipersin opened this issue Jun 27, 2016 · 4 comments
Closed

fill_between() not working properly #6650

avipersin opened this issue Jun 27, 2016 · 4 comments

Comments

@avipersin
Copy link

avipersin commented Jun 27, 2016

Matplotlib 1.5.1
Python 3.4
Anaconda 4.0.1

import matplotlib.pyplot as plt
import numpy as np

xs = [1950.04, 1950.29, 1950.54, 1950.79, 1951.04, 1951.29, 1951.54, 1951.79, 1952.04, 1952.29, 1952.54, 1952.79, 1953.04, 1953.29, 1953.54, 1953.79, 1954.04, 1954.29, 1954.54, 1954.79, 1955.04, 1955.29, 1955.54, 1955.79, 1956.04, 1956.29, 1956.54, 1956.79, 1957.04, 1957.29, 1957.54, 1957.79, 1958.04, 1958.29, 1958.54]
ys = [-1.46, -1.17, -0.73, -0.7, -0.98, 0.14, 0.36, 0.71, 0.33, 0.44, -0.06, 0.03, 0.41, 0.66, 0.62, 0.62, 0.55, -0.39, -0.62, -0.75, -0.68, -0.71, -0.64, -1.52, -0.97, -0.47, -0.52, -0.57, -0.34, 0.59, 1.02, 1.05, 1.6, 0.87, 0.44]

plt.plot(xs, ys)
plt.fill_between(xs, 0, ys, where=np.asarray(ys) > 0, facecolor='red')
plt.savefig("fill_between.png")
plt.show()

fill_between() is not properly filling along the line when y > 0. The image below illustrates the problem.

fill_between

@avipersin
Copy link
Author

I got around this by:

  • Plotted a line at y=0.
  • Created a list ys1 = [y if y > 0 else 0 for y in ys]
  • plt.plot(xs, ys1, 'k')
  • plt.fill_between(xs, [0] * len(ys1), ys1, facecolor='red')

And because I also wanted to fill in below y=0, I did the opposite for the bottom.

If fill_between() is not supposed to compute zero-crossings then this can be closed.

@afvincent
Copy link
Contributor

afvincent commented Jun 28, 2016

Does passing the argument interpolate=True achieve what you want? I think this kwarg was designed especially for what you describe. With interpolate=True

import matplotlib.pyplot as plt
import numpy as np

xs = [1950.04, 1950.29, 1950.54, 1950.79, 1951.04, 1951.29, 1951.54, 1951.79, 1952.04, 1952.29, 1952.54, 1952.79, 1953.04, 1953.29, 1953.54, 1953.79, 1954.04, 1954.29, 1954.54, 1954.79, 1955.04, 1955.29, 1955.54, 1955.79, 1956.04, 1956.29, 1956.54, 1956.79, 1957.04, 1957.29, 1957.54, 1957.79, 1958.04, 1958.29, 1958.54]
ys = [-1.46, -1.17, -0.73, -0.7, -0.98, 0.14, 0.36, 0.71, 0.33, 0.44, -0.06, 0.03, 0.41, 0.66, 0.62, 0.62, 0.55, -0.39, -0.62, -0.75, -0.68, -0.71, -0.64, -1.52, -0.97, -0.47, -0.52, -0.57, -0.34, 0.59, 1.02, 1.05, 1.6, 0.87, 0.44]

plt.plot(xs, ys)
plt.fill_between(xs, 0, ys, where=np.asarray(ys) > 0, facecolor='Crimson', interpolate=True)
plt.fill_between(xs, 0, ys, where=np.asarray(ys) <= 0, facecolor='OliveDrab', interpolate=True)

plt.savefig("fill_between_w_interpolation.png")
plt.show()

produces
fill_between_w_interpolation

@avipersin
Copy link
Author

@afvincent Yes that works, thanks!

I'll close this issue.

@afvincent
Copy link
Contributor

@avipersin You're welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants