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

Time:04-26

I am pulling in data on Japanese GDP and graphing a stacked barchart overlayed with a line. I would like for the x-axis to have only yyyy-mm and no timestamp. I read about a compatability issue with pandas and matplotlib epochs. Is that the issue here? When I try to use matplotlib Dateformatter, the returned dates begin with 1970. How can I fix this?

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()```

CodePudding user response:

Don't use DateFormatter as it is causing trouble, rather change format of the dataframe index using df.index = pd.to_datetime(df.index, format = '%m/%d/%Y').strftime('%Y-%m')

Here is what I did with your gdp.csv file

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()

enter image description here

  • Related