如何在不使用 matplotlib DateFormatter hh:mm:ss 的情况下格式化 x 轴日期?

How can the x-axis dates be formatted without hh:mm:ss using matplotlib DateFormatter?

我正在提取有关日本 GDP 的数据,并绘制一个叠加有一条线的堆叠条形图。我希望 x 轴只有 yyyy-mm 而没有时间戳。我读到了 pandas 和 matplotlib 时期的兼容性问题。是这里的问题吗?当我尝试使用 matplotlib Dateformatter 时,返回的日期从 1970 年开始。我该如何解决这个问题?

import pandas as pd
import pandas_datareader.data as web
import datetime
import requests
import investpy
import matplotlib.pyplot as plt
import matplotlib.dates as mdates

start1 = '01/01/2013' #dd/mm/yyyy
end1 = '22/04/2022'

# Real GDP growth
# Source: Cabinet Office http://www.esri.cao.go.jp/en/sna/sokuhou/sokuhou_top.html
# Get the data
url = 'https://www.esri.cao.go.jp/jp/sna/data/data_list/sokuhou/files/2021/qe214_2/tables/nritu-jk2142.csv'
url2 = url.replace('nritu','nkiyo')  # URL used for GDP growth by component
url3 = url.replace('nritu-j', 'gaku-m')
url4 = url.replace('nritu', 'gaku')
url5 = url.replace('nritu', 'kgaku')
df = pd.read_csv(url2, header=5, encoding='iso-8859-1').loc[49:]
gdpkeep = {
    'Unnamed: 0': 'date',
    'GDP(Expenditure Approach)': 'GDP',
    'PrivateConsumption': 'Consumption',
    'PrivateResidentialInvestment': 'inv1',
    'Private Non-Resi.Investment': 'inv2',
    'Changein PrivateInventories': 'inv3',
    'GovernmentConsumption': 'gov1',
    'PublicInvestment': 'gov2',
    'Changein PublicInventories': 'gov3',
    'Goods & Services': 'Net Exports'
}
df = df[list(gdpkeep.keys())].dropna()
df.columns = df.columns.to_series().map(gdpkeep)

# Adjust the date column to make each value a consistent format
dts = df['date'].str.split('-').str[0].str.split('/ ')
for dt in dts:
    if len(dt) == 1:
        dt.append(dt[0])
        dt[0] = None
df['year'] = dts.str[0].fillna(method='ffill')
df['month'] = dts.str[1].str.zfill(2)
df['date2'] = df['year'].str.cat(df['month'], sep='-')
df['date'] = pd.to_datetime(df['date2'], format='%Y-%m')

# Sum up various types of investment and government spending
df['Investment'] = df['inv1'] + df['inv2'] + df['inv3']
df['Government Spending'] = df['gov1'] + df['gov2'] + df['gov3']
df = df.set_index('date')[['GDP', 'Consumption', 'Investment', 'Government Spending', 'Net Exports']]
df.to_csv('G:\AutomaticDailyBackup\Python\MacroEconomics\Japan\Data\gdp.csv', header=True)  # csv file created
print(df.tail(8))

# Plot
df['Net Exports'] = df['Net Exports'].astype(float)
ax = df[['Consumption', 'Investment', 'Government Spending', 'Net Exports']]['2013':].plot(label=df.columns, kind='bar', stacked=True, figsize=(10, 10))
ax.plot(range(len(df['2013':])), df['GDP']['2013':], label='Real GDP', marker='o', linestyle='None', color='black')
plt.title('Japan: Real GDP Growth')
plt.legend(frameon=False, loc='upper left')
ax.set_frame_on(False)
ax.set_ylabel('Annual Percent Change')
# dfmt = mdates.DateFormatter("%Y-%m") # proper formatting Year-month
# ax.xaxis.set_major_formatter(dfmt)
plt.savefig('G:\AutomaticDailyBackup\Python\MacroEconomics\Japan\Data\RealGDP.png')
plt.show()```

不要使用 DateFormatter 因为它会引起麻烦,而是使用 df.index = pd.to_datetime(df.index, format = '%m/%d/%Y').strftime('%Y-%m')

更改数据帧索引的格式

这是我对你的 gdp.csv 文件所做的

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import matplotlib.dates as mdates
from matplotlib.dates import DateFormatter
import matplotlib.dates 

df=pd.read_csv("D:\python\gdp.csv").set_index('date')
df.index = pd.to_datetime(df.index, format = '%m/%d/%Y').strftime('%Y-%m')
# Plot

fig, ax = plt.subplots()
df['Net Exports'] = df['Net Exports'].astype(float)

ax = df[['Consumption', 'Investment', 'Government Spending', 'Net Exports']]['2013':].plot(label=df.columns, kind='bar', stacked=True, figsize=(10, 10))
ax.plot(range(len(df['2013':])), df['GDP']['2013':], label='Real GDP', marker='o', linestyle='None', color='black')

plt.legend(frameon=False, loc='upper left')
ax.set_frame_on(False)

plt.savefig(r'D:\python\RealGDP.png')
plt.show()