Ax.fill () in matplotlib filling the wrong area
I am trying to plot a Lorenz curve using matplotlib and I wanted to fill the area below the Lorenz curve, this is how I wrote the code:
import matplotlib.pyplot as plt
import seaborn as sns
sns.set_style("darkgrid")
# I used seaborn for different styles.
x = [0, 0.10304926499801351, 0.20455899880810488, 0.30986789829161698,
0.41132796980532377, 0.51018077075883983, 0.61201330949543098,
0.71223182359952319, 0.81185439014700034, 0.91063269765593957,
1.0]
y = [0, 0.016037735849056604, 0.033018867924528301, 0.063207547169811321,
0.087735849056603782, 0.13962264150943399, 0.20000000000000001,
0.27547169811320754, 0.37264150943396224, 0.53018867924528301,
1.0]
fig, ax = plt.subplots()
ax.fill(x, y, 'b', alpha=0.3)
plt.show()
The result is something like this:
Obviously it doesn't work as expected as I would like to fill the area BELOW the curve.
I tried with some other examples like this:
x = np.linspace(0, 2 * np.pi, 500) y1 = np.sin(x) fig, ax = plt.subplots() ax.fill(x, y1, 'b', alpha=0.3) plt.show()
and they all worked great. I must have missed something in my own conspiracy, but I can't figure it out. Any thoughts?
source to share
I think you want to use fill_between
instead fill
.
fill
fills the area between the points of the curve. Therefore the expected result.
fill_between
fills in the area between the heading and some other curve or constant y
.
Using
ax.fill_between(x, y, y2=0, color='b', alpha=0.3)
you get the area between the zero line y = 0 and the filled curve:
source to share