2

I am using monthly data provided from the federal reserve. However, I only want to plot 10 years worth of data, so I took the .tail() of 10 x 12 months = 120 for monthly data and 10 x 4 quarters for quarterly. My dilemma is when I plot the dataframes, it is plotting every single month on the x axis, when I only want to be having 1 tick per year per graph.

# Load in data from .csv files only using the 10 most recent years of data, 120 for monthly 40 for quarterly
federal_funds_df = pd.read_csv("data/FEDFUNDS.csv").tail(120)
CPI_df = pd.read_csv("data/CPIAUCSL.csv").tail(120)
unemployment_df = pd.read_csv("data/UNRATE.csv").tail(120)
real_GDP_df = pd.read_csv("data/GDPC1.csv").tail(40)

# Initialize the plot figure
plt.figure(figsize=(4, 1))
plt.suptitle("U.S. Economic Indicators")

# Effective Federal Funds Rate Plot
plt.subplot(141)
plt.plot(federal_funds_df.DATE, federal_funds_df.FEDFUNDS, label="Federal Funds Rate")
plt.legend(loc='best')

# Consumer Price Index Plot
plt.subplot(142)
plt.plot(CPI_df.DATE, CPI_df.CPIAUCSL, label="Consumer Price Index")
plt.legend(loc='best')

# Civilian Unemployment Rate Plot
plt.subplot(143)
plt.plot(unemployment_df.DATE, unemployment_df.UNRATE, label="Unemployment Rate")
plt.legend(loc='best')

# Real Gross Domestic Product Plot
plt.subplot(144)
plt.plot(real_GDP_df.DATE, real_GDP_df.GDPC1, label="Real GDP")
plt.legend(loc='best')

# Show plots fullscreen
mng = plt.get_current_fig_manager()
mng.window.state('zoomed')
plt.show()

Sample .csv data:

DATE,FEDFUNDS
1954-07-01,0.80
1954-08-01,1.22
1954-09-01,1.06
1954-10-01,0.85
1954-11-01,0.83
1954-12-01,1.28
michaelobr
  • 21
  • 3

1 Answers1

1

You should convert the dates to date time format. It would be easier if you posted a sample of your input data. But something like the following:

pd.to_datetime(df['Date'], format= '%d/%m/%y')

You can then do a groupby of whatever items you want to plot per year. For a more specific answer, please post some of you data.

Also see this post : Groupby month and year

jonboy
  • 415
  • 4
  • 14
  • 45