Python Forum
Plotting A Time Series With Shaded Recession Bars
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Plotting A Time Series With Shaded Recession Bars
#1
Hello,

I am trying to reconstruct time series graphs from FRED through Python.

I am having trouble however finding a simple way to add shaded bars denoting start and end dates for recessions onto a basic time series plot.

I have loaded data for recession dates from FRED that looks like:
Date Recession
10/1/67 0
1/1/68 0
4/1/68 0
7/1/68 0
10/1/68 0
1/1/69 0
4/1/69 1
7/1/69 1
10/1/69 1
1/1/70 1
4/1/70 1
7/1/70 1
10/1/70 1
Reply
#2
Perhaps an area plot or a bar plot (commented)?

from pandas import read_csv
import matplotlib.pyplot as plt
from io import StringIO

if __name__ == "__main__":
    df = read_csv(StringIO("""Date Recession tsval
10/1/1967 0 .1
1/1/1968 0 .2
4/1/1968 0 .3
7/1/1968 0 .4
10/1/1968 0 .5
1/1/1969 0 .4
4/1/1969 1 .3
7/1/1969 1 .2
10/1/1969 1 .1
1/1/1970 1 .2
4/1/1970 1 .3
7/1/1970 1 .4
10/1/1970 1 .5"""), sep=" ")
    df["nonrecession"] = 1-df.Recession
    # ax=df.plot(x="Date",y=["Recession","nonrecession"],kind="bar", stacked=True)
    ax=df.plot(x="Date",y=["Recession","nonrecession"],kind="area", stacked=True)
    df.plot(x="Date",y="tsval", ax=ax)
    plt.show()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Getting the hourly average of a time series dataset Raskou07 10 1,991 Dec-15-2020, 12:51 PM
Last Post: palladium
  Tableau Time Series Prediction using Python Integration tobimarsh43 0 552 Jul-24-2020, 10:38 AM
Last Post: tobimarsh43
  Bode plot from time series experiment data discus 4 1,733 Jun-20-2020, 07:46 AM
Last Post: discus
  Convert quarterly time series to monthly time series donnertrud 1 1,485 May-22-2020, 10:16 AM
Last Post: pyzyx3qwerty
  Time series manipulation SinPy 0 462 Apr-01-2020, 12:48 PM
Last Post: SinPy
  save in CSV and plotting in real time linkxxx 8 1,591 Sep-01-2019, 12:24 PM
Last Post: buran
  Plotting help! Time is of the essence lordhyperfluxed 6 4,579 Mar-22-2019, 12:50 PM
Last Post: scidam
  Multiply Two Time Series: Not Working brandonfoo 0 831 Feb-27-2019, 05:15 AM
Last Post: brandonfoo
  Working with rain time series dmildem 3 1,497 Nov-28-2018, 11:27 PM
Last Post: Larz60+
  Extract data from rasters to plot time-series zhl 0 1,115 Sep-20-2018, 09:45 PM
Last Post: zhl

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020