Skip to content
Advertisement

Shading between two lines with Matplotlib

I want to use axvspan() function to visualize a DataFrame that I obtained using Pandas DataReader.But when I use the the following codes, I saw an error and there is no shading in subplots. What should I do? Thank you.

JavaScript

Advertisement

Answer

Try looping over all Subplots and adding axvspan to the specific AxesSubplot instead:

JavaScript

Some slight code reduction using the ylabel and xlabel kwargs of plot and also setting subplot titles from df.columns instead of manually.

plot


Adding legend for axvspan. The simplest approach is to add a label to each axvspan and make the legend at the end of each iteration:

JavaScript

plot 2 with axvspan legend on all subplots


Alternatively a single legend can be made for just the Crises:

JavaScript

plot 3 single legend for Crises

^ Single legend is here in the lower left.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement