Filling region between curve and x-axis in Python using Matplotlib

plt.fill assumes that you have a closed shape to fill - interestingly if you add a final 0 to your data you get a much more sensible looking plot.

import numpy as np
import matplotlib.pyplot as plt

y = [0,0,0,0,0,0,0,0,0,0,0,863,969,978,957,764,767,1009,1895,980,791,0]
x = np.arange(len(y))


fig2, ax2 = plt.subplots()
ax2.fill(x, y)

plt.savefig('picForWeb.png')
plt.show()

Results in: Closed Plot

Hope this helps to explain your odd plot.


If you want to use this on a pd.DataFrame use this:

df.abs().interpolate().plot.area(grid=1, linewidth=0.5)

interpolate() is optional.

enter image description here


I usually use the fill_between function for these kinds of plots. Try something like this instead:

import numpy as np
import matplotlib.pyplot as plt

y = [0,0,0,0,0,0,0,0,0,0,0,863,969,978,957,764,767,1009,1895,980,791]
x = np.arange(len(y))

fig, (ax1) = plt.subplots(1,1); 
ax1.fill_between(x, 0, y)
plt.show()

See more examples here.