pandas 制作熊猫系列的直方图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/53055708/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Make a histogram of a pandas series
提问by Emma
I want to make a histogram of a pandas series (prior_fails) but I keep getting the following ValueError:
我想制作Pandas系列的直方图(prior_fails),但我不断收到以下 ValueError:
ValueError: view limit minimum -36814.8560105 is less than 1 and is an
invalid Matplotlib date value. This often happens if you pass a non-datetime
value to an axis that has datetime units
This is how I am calling the histogram
这就是我调用直方图的方式
plt.hist(prior_fails)
plt.ylabel('Frequency')
plt.xlabel('Days of Failure (%)')
prior fails is a series with the following index :
先前失败是具有以下索引的系列:
prior_fails.index
Out[85]:
Index([u'prior110', u'prior113', u'prior118', u'prior141', u'prior144',
u'prior16', u'prior217', u'prior223', u'prior245', u'prior29',
u'prior352', u'prior360', u'prior370', u'prior438', u'prior55',
u'prior59', u'prior60', u'prior68', u'prior74', u'prior88'],
dtype='object')
And content:
和内容:
prior_fails
Out[86]:
prior110 13.962170
prior113 10.861125
prior118 21.304131
prior141 11.309109
prior144 11.363863
prior16 14.479841
prior217 10.403186
prior223 14.201095
prior245 7.974116
prior29 17.401692
prior352 9.860627
prior360 12.339472
prior370 16.207068
prior438 16.381284
prior55 20.587357
prior59 10.452962
prior60 15.828771
prior68 16.769537
prior74 16.918865
prior88 9.805874
dtype: float64
Any help would be greatly appreciated. I am fairly new to python. Thanks!
任何帮助将不胜感激。我对python相当陌生。谢谢!
回答by Kristada673
Well, I am not getting any error when plotting the histogram with either matplotlib or pandas (which uses matplotlib itself to plot).
好吧,在使用 matplotlib 或 pandas(使用 matplotlib 本身进行绘图)绘制直方图时,我没有收到任何错误。
import pandas as pd
data = {'ind': ['prior110', 'prior113', 'prior118', 'prior141', 'prior144', 'prior16', 'prior217', 'prior223', 'prior245', 'prior29', 'prior352', 'prior360', 'prior370', 'prior438', 'prior55', 'prior59', 'prior60', 'prior68', 'prior74', 'prior88'],
'val': [13.96217, 10.861125, 21.304131, 11.309109, 11.363863, 14.479841, 10.403186, 14.201095, 7.974116, 17.401692, 9.860627, 12.339472, 16.207068, 16.381284, 20.587357, 10.452962, 15.828771, 16.769537, 16.918865, 9.805874]}
prior_fails = pd.DataFrame(data, columns=['ind', 'val'])
prior_fails.set_index('ind', inplace=True)
prior_fails
# with pandas
prior_fails.hist()
plt.ylabel('Frequency')
plt.xlabel('Days of Failure (%)')
plt.title('Histogram')
# with matplotlib
import matplotlib.pyplot as plt
plt.hist(prior_fails.val)
plt.ylabel('Frequency')
plt.xlabel('Days of Failure (%)')
If you're still getting the error, perhaps you could try %matplotlib inline
just before plotting. This will change the backend that matplotlib uses to inline
. Sometimes the default backend might get broken or corrupted for whatever reason, so you could try changing the backends to see if that was causing the issue. There are other backends as well, like qt5
, agg
, etc. So, if this also doesn't solve your issue, maybe you could try some of these backends.
如果您仍然遇到错误,也许您可以%matplotlib inline
在绘图之前尝试。这将改变 matplotlib 使用的后端inline
。有时,无论出于何种原因,默认后端可能会损坏或损坏,因此您可以尝试更改后端以查看是否是导致问题的原因。还有其他后端,例如qt5
、agg
等。因此,如果这也不能解决您的问题,也许您可以尝试其中的一些后端。
回答by smj
To make a histogram of a series, I normally call .hist()
directly on the series, which uses matplotlib
behind the scenes:
为了制作系列的直方图,我通常.hist()
直接调用系列,它matplotlib
在幕后使用:
import pandas as pd
import numpy as np
data = pd.Series(np.random.randn(1000))
data.hist(bins = 50)
Giving:
给予:
Is this what you are after?
这是你追求的吗?