2013-12-18 13 views
46

Ho problemi a creare istogrammi da oggetti serie panda e non riesco a capire perché non funzioni. Il codice ha funzionato bene prima ma ora non funziona.Errore Matplotlib/Pandas usando l'istogramma

Qui è un po 'del mio codice (in particolare, una serie oggetto panda Sto cercando di fare un istogramma di):

type(dfj2_MARKET1['VSPD2_perc']) 

che emette il risultato: pandas.core.series.Series

Ecco il mio tracciato codice:

fig, axes = plt.subplots(1, 7, figsize=(30,4)) 
axes[0].hist(dfj2_MARKET1['VSPD1_perc'],alpha=0.9, color='blue') 
axes[0].grid(True) 
axes[0].set_title(MARKET1 + ' 5-40 km/h') 

messaggio di errore:

AttributeError       Traceback (most recent call last) 
    <ipython-input-75-3810c361db30> in <module>() 
     1 fig, axes = plt.subplots(1, 7, figsize=(30,4)) 
     2 
    ----> 3 axes[1].hist(dfj2_MARKET1['VSPD2_perc'],alpha=0.9, color='blue') 
     4 axes[1].grid(True) 
     5 axes[1].set_xlabel('Time spent [%]') 

    C:\Python27\lib\site-packages\matplotlib\axes.pyc in hist(self, x, bins, range, normed,   weights, cumulative, bottom, histtype, align, orientation, rwidth, log, color, label, stacked, **kwargs) 
    8322    # this will automatically overwrite bins, 
    8323    # so that each histogram uses the same bins 
-> 8324    m, bins = np.histogram(x[i], bins, weights=w[i], **hist_kwargs) 
    8325    m = m.astype(float) # causes problems later if it's an int 
    8326    if mlast is None: 

    C:\Python27\lib\site-packages\numpy\lib\function_base.pyc in histogram(a, bins, range,  normed, weights, density) 
    158   if (mn > mx): 
    159    raise AttributeError(
--> 160     'max must be larger than min in range parameter.') 
    161 
    162  if not iterable(bins): 

AttributeError: max must be larger than min in range parameter. 
+0

Hmm, funziona per me. Puoi mostrare il tuo dataframe? –

+0

Hmm, strano quando lo faccio posso effettivamente produrre un istogramma: s = dfj2_MARKET1 ['VSPD1_perc'] s.hist() – jonas

+0

Sì, ma poi stai usando la funzione 'hist' dei panda, e non i matplotlibs. E questo gestisce ad esempio i NaN come previsto. Vedi il mio aggiornamento. – joris

risposta

78

Questo errore si verifica tra l'altro quando si hanno valori NaN nella serie. Potrebbe essere il caso?

Queste NaN non sono gestite correttamente dalla funzione hist di matplotlib. Per esempio:

s = pd.Series([1,2,3,2,2,3,5,2,3,2,np.nan]) 
fig, ax = plt.subplots() 
ax.hist(s, alpha=0.9, color='blue') 

produce lo stesso errore AttributeError: max must be larger than min in range parameter. Una possibilità è ad esempio per rimuovere il Nan prima della stampa. Questo funziona:

ax.hist(s.dropna(), alpha=0.9, color='blue') 

Un'altra opzione è quella di utilizzare i panda hist metodo sul vostra serie e fornendo il axes[0] alla parola ax:

dfj2_MARKET1['VSPD1_perc'].hist(ax=axes[0], alpha=0.9, color='blue') 
+0

Funziona perfetto !!! Grazie mille – jonas

+0

Perfect NaN crea l'errore e l'eccezione panda/pyplot non condivide abbastanza informazioni. Davvero utile – Doogle

+0

Funziona perfettamente! –

Problemi correlati