Datum Matplotlib

mit matplotlib, NumPy, pandas, SciPy, SymPy und weiteren mathematischen Programmbibliotheken.
Antworten
st_baum
User
Beiträge: 26
Registriert: Sonntag 24. Mai 2020, 13:54

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?
Antworten