0

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")
325
  • 594
  • 8
  • 21

1 Answers1

1

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);
Peter Leimbigler
  • 10,775
  • 1
  • 23
  • 37
  • Is it possible to add the shaded area to the legend? @Peter Leimbigler – 325 Jan 05 '22 at 17:30
  • It is indeed :) I don't have a ready-to-go example, but this should help: https://stackoverflow.com/questions/40736920/line-plus-shaded-region-for-error-band-in-matplotlibs-legend – Peter Leimbigler Jan 05 '22 at 17:36