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
  Date Time Series Help...Please spra8560 2 312 Feb-01-2024, 01:38 PM
Last Post: spra8560
  Plotting by Time, error mansoorahs 1 706 May-16-2023, 09:46 AM
Last Post: Larz60+
  Print names in x-axis of a time-series values hobbyist 4 1,177 Apr-22-2023, 09:29 PM
Last Post: deanhystad
  Non-blocking real-time plotting slow_rider 5 3,453 Jan-07-2023, 09:47 PM
Last Post: woooee
  Time series JasminQuinn 0 1,005 Apr-22-2022, 10:33 PM
Last Post: JasminQuinn
  time setup for realtime plotting of serial datas at high sampling rate alice93 6 3,647 Jan-07-2022, 05:41 PM
Last Post: deanhystad
  How to read rainfall time series and insert missing data points MadsM 4 2,123 Jan-06-2022, 10:39 AM
Last Post: amdi40
  Getting the hourly average of a time series dataset Raskou07 10 12,315 Dec-15-2020, 12:51 PM
Last Post: palladium
  Tableau Time Series Prediction using Python Integration tobimarsh43 0 1,888 Jul-24-2020, 10:38 AM
Last Post: tobimarsh43
  Bode plot from time series experiment data discus 4 7,209 Jun-20-2020, 07:46 AM
Last Post: discus

Forum Jump:

User Panel Messages

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