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
2
  • 1
    Don't plot strings. Convert to dates first. Commented Jun 25, 2019 at 1:20
  • 1
    Have a look at the matplotlib documentation. It provides an example for setting the ticks and subticks of a plot over time. Commented Jun 25, 2019 at 1:32

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.