【发布时间】:2019-06-25 01:16:34
【问题描述】:
我使用的是美联储提供的月度数据。但是,我只想绘制 10 年的数据,所以我将 .tail() 的 10 x 12 个月 = 120 用于月度数据,将 10 x 4 季度用于季度。我的困境是当我绘制数据框时,它在 x 轴上绘制每个月,而我只想每个图每年有 1 个刻度。
# 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()
.csv 数据示例:
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
【问题讨论】:
-
不要绘制字符串。先转换为日期。
-
查看 matplotlib 文档。它提供了一个example 用于设置随时间变化的图的刻度和子刻度。
标签: python pandas matplotlib