You can use matplotlib's fill_between
function to shade specific regions of the plot based on a condition. Here's an example of how to shade the regions where the GDP growth rate was negative (i.e. during a recession) in a time series plot:
main.py854 chars30 lines
This code loads some example GDP data (stored in a CSV file), calculates the growth rate of the GDP, and then creates a boolean Series that indicates whether each point is in a recession or not (using the is_recession
function). It then creates a standard time series plot of the GDP, and uses fill_between
to shade the regions where we were in a recession (specified by the in_recession
boolean Series).
You can customize the color and opacity of the shading by changing the color
and alpha
arguments in the fill_between
function.
gistlibby LogSnag