2015-10-06 12 views
6

Vorrei assegnare per l'asse x in fullplotlib la data completa con il tempo ma con la scala automatica potrei ottenere solo le ore o le date ma non entrambe. Codice seguente:Come mostrare la data e l'ora sull'asse x in matplotlib

import matplotlib.pyplot as plt 
import pandas as pd 

times = pd.date_range('2015-10-06', periods=500, freq='10min') 

fig, ax = plt.subplots(1) 
fig.autofmt_xdate() 
plt.plot(times, range(times.size)) 
plt.show() 

E su asse x ottengo solo volte senza alcuna data quindi è difficile per le misurazioni distinte.

Penso che sia un'opzione in matplotlib in matplotlib.dates.AutoDateFormatter ma non ho trovato nessuno che potesse permettermi di cambiare quella scala automatica.

enter image description here

risposta

12

Si può fare questo con un matplotlib.dates.DateFormatter, che prende una stringa di formato strftime come argomento. Per ottenere un formato day-month-year hour:minute, è possibile utilizzare %d-%m-%y %H:%M:

import matplotlib.pyplot as plt 
import pandas as pd 
import matplotlib.dates as mdates 

times = pd.date_range('2015-10-06', periods=500, freq='10min') 

fig, ax = plt.subplots(1) 
fig.autofmt_xdate() 
plt.plot(times, range(times.size)) 

xfmt = mdates.DateFormatter('%d-%m-%y %H:%M') 
ax.xaxis.set_major_formatter(xfmt) 

plt.show() 

enter image description here

Problemi correlati