Matplotlib / Pandas Error Using Histogram

I have a problem creating histograms from pandas series objects, and I cannot understand why this is not working. The code worked fine, but now it is not.

Here is some of my code (specifically the pandas series object that I am trying to make a histogram):

type(dfj2_MARKET1['VSPD2_perc']) 

which outputs the result: pandas.core.series.Series

Here is my build code:

 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') 

Error message:

  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 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. 
+42
python matplotlib pandas histogram
Dec 18 '13 at 11:17
source share
1 answer

This error occurs by the way when you have NaN values ​​in a series. Could this be so?

These NaNs are poorly handled by the hist matplotlib function. For example:

 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') 

throws the same error AttributeError: max must be larger than min in range parameter. One option is, for example, to remove NaN before plotting. This will work:

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

Another option is to use the pandas hist method in your series and provide axes[0] with the ax keyword:

 dfj2_MARKET1['VSPD1_perc'].hist(ax=axes[0], alpha=0.9, color='blue') 
+72
Dec 18 '13 at 11:58
source share



All Articles