Search code examples
python-3.xpandasmatplotlibplotline-plot

Added shared error around Pandas line plot


I have a nice looking line plot, but I want to add shaded areas designating the margin of error above and below my existing line. I have a column with the error already calculated in my pandas data frame, but I am unsure how to add it to my plot.

enter image description here

fig, ax = plt.subplots()
joinedDF.plot(x='date', y='count', figsize = (15, 8), ax = ax)
ax.set_xlabel("Date")
ax.set_ylabel("Count")

Solution

  • Based on the pandas plotting documentation, and assuming your DataFrame joinedDF has the error in a column named err, run this after your current code:

    plt.fill_between(joinedDF['date'], 
                     joinedDF['count'] - joinedDF['err'], 
                     joinedDF['count'] + joinedDF['err'], 
                     color='b', 
                     alpha=0.2);