2010-08-15 11 views
16

Ho un set di dati costituito da coppie data-valore. Voglio tracciarli in un grafico a barre con le date specifiche nell'asse x.Come tracciare i dati in base a date specifiche sull'asse x utilizzando matplotlib

Il mio problema è che matplotlib distribuisce lo xticks nell'intero intervallo di date; e inoltre traccia i dati usando i punti.

Le date sono tutti gli oggetti datetime. Ecco un esempio del set di dati:

data = [(DT.datetime.strptime('2010-02-05', "%Y-%m-%d"), 123), 
     (DT.datetime.strptime('2010-02-19', "%Y-%m-%d"), 678), 
     (DT.datetime.strptime('2010-03-05', "%Y-%m-%d"), 987), 
     (DT.datetime.strptime('2010-03-19', "%Y-%m-%d"), 345)] 

Ecco un esempio di codice eseguibile utilizzando pyplot

import datetime as DT 
from matplotlib import pyplot as plt 

data = [(DT.datetime.strptime('2010-02-05', "%Y-%m-%d"), 123), 
     (DT.datetime.strptime('2010-02-19', "%Y-%m-%d"), 678), 
     (DT.datetime.strptime('2010-03-05', "%Y-%m-%d"), 987), 
     (DT.datetime.strptime('2010-03-19', "%Y-%m-%d"), 345)] 

x = [date for (date, value) in data] 
y = [value for (date, value) in data] 

fig = plt.figure() 

graph = fig.add_subplot(111) 
graph.plot_date(x,y) 

plt.show() 

DOMANDA SOMMARIO:
La mia situazione è più simile Ho un Axes esempio pronto (riferimento da nel codice sopra) e voglio fare quanto segue:

  1. Fare in modo che xticks corrisponda ai valori di data esatti. Ho sentito parlare di matplotlib.dates.DateLocator ma non ho idea di come crearne uno e quindi associarlo a uno specifico oggetto Axes.
  2. Ottenere un controllo più rigido il tipo di grafico utilizzato (barre, a linee, punti, ecc)
+0

Solo un consiglio: dato che la tua domanda è davvero puramente circa matplotlib e non ha nulla di specifico per wxWidgets, sarebbe potrebbe rendere le cose più facili se si modifica il tuo esempio per usare le funzioni da 'matplotlib.pyplot' e lasciare la roba wx fuori da esso. –

+0

@David: corretto. Grazie, ho capito che ci potrebbero essere più persone che possono leggere 'matplotlib' +' pyplot' meglio di 'matplotlib' +' wx' – Kit

risposta

28

Quello che stai facendo è abbastanza semplice che è più facile usando solo la trama, piuttosto che plot_date. plot_date è ottimo per i casi più complessi, ma l'impostazione di ciò che ti serve può essere facilmente eseguita senza di essa.

per esempio, in base alla esempio di cui sopra:

import datetime as DT 
from matplotlib import pyplot as plt 
from matplotlib.dates import date2num 

data = [(DT.datetime.strptime('2010-02-05', "%Y-%m-%d"), 123), 
     (DT.datetime.strptime('2010-02-19', "%Y-%m-%d"), 678), 
     (DT.datetime.strptime('2010-03-05', "%Y-%m-%d"), 987), 
     (DT.datetime.strptime('2010-03-19', "%Y-%m-%d"), 345)] 

x = [date2num(date) for (date, value) in data] 
y = [value for (date, value) in data] 

fig = plt.figure() 

graph = fig.add_subplot(111) 

# Plot the data as a red line with round markers 
graph.plot(x,y,'r-o') 

# Set the xtick locations to correspond to just the dates you entered. 
graph.set_xticks(x) 

# Set the xtick labels to correspond to just the dates you entered. 
graph.set_xticklabels(
     [date.strftime("%Y-%m-%d") for (date, value) in data] 
     ) 

plt.show() 

Se preferisci un grafico a barre, basta usare plt.bar(). Per capire come impostare gli stili di linea e pennarello, vedi plt.plot() Plot with date labels at marker locations http://www.geology.wisc.edu/~jkington/matplotlib_date_labels.png

+0

+1 Questo è eccellente. Grazie! – Kit

Problemi correlati