Seite 1 von 1

Datum Matplotlib

Verfasst: Donnerstag 3. November 2022, 13:49
von st_baum
Ich möchte das Format der Datumsachse in einem Matplotlib Chart anpassen. Hier der DF

Code: Alles auswählen

period = pd.date_range('2017-01-01', periods=14, freq='m')
value = np.random.randint(5, size=14)
d = {'period':period,'value':value}
df = pd.DataFrame(d)
df['period'] = pd.to_datetime(df.period)
Chart:

Code: Alles auswählen

fig,ax = plt.subplots(figsize=(14,10), dpi = 60)
df.plot(kind = 'line', x = 'period',  y = 'value', ax = ax)
Nun passe ich das Datumsformat an:

Code: Alles auswählen

fig,ax = plt.subplots(figsize=(14,10), dpi = 60)
df.plot(kind = 'line', x = 'period',  y = 'value', ax = ax)
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m'))

ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m')) sorgt dafür, dass auf der x-Achse falsche Werte sind.

Könnt ihr helfen?