制作熊猫系列的直方图

时间:2018-10-30 00:23:02

标签: python pandas matplotlib histogram series

我想制作一个熊猫系列的直方图(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

这就是我所说的直方图

plt.hist(prior_fails)
plt.ylabel('Frequency')
plt.xlabel('Days of Failure (%)')

先前失败的是具有以下索引的系列:

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

内容:

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

任何帮助将不胜感激。我是python的新手。 谢谢!

2 个答案:

答案 0 :(得分:1)

好吧,使用matplotlib或pandas(使用matplotlib本身进行绘制)绘制直方图时,我不会出现任何错误。

def format_duration(seconds):
    units = [('year', 86400*365), ('day', 86400), ('hour', 3600), ('minute', 60), ('second', 1)]
    secs = seconds
    count = []
    for uname, usecs in units:
        if secs // usecs != 0:
            count.append((secs // usecs, uname))
            secs %= usecs

    words = [str(n) + ' ' + (unit if n == 1 else unit + 's') for n, unit in count]

    if len(words) > 1:
        result = ', '.join(words[:-1]) + ' and ' + words[-1]
    else:
        result = words[0]

    return result


print(format_duration(62))

enter image description here

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

enter image description here

# with pandas
prior_fails.hist()
plt.ylabel('Frequency')
plt.xlabel('Days of Failure (%)')
plt.title('Histogram')

enter image description here

如果仍然出现错误,也许可以在绘图前尝试# with matplotlib import matplotlib.pyplot as plt plt.hist(prior_fails.val) plt.ylabel('Frequency') plt.xlabel('Days of Failure (%)') 。这会将matplotlib使用的后端更改为%matplotlib inline。有时默认后端可能由于任何原因而损坏或损坏,因此您可以尝试更改后端以查看是否是造成此问题的原因。还有其他后端,例如inlineqt5等。因此,如果这也不能解决您的问题,也许您可​​以尝试其中的一些后端。

答案 1 :(得分:0)

要制作序列的直方图,通常我直接在序列上调用.hist(),该序列在幕后使用matplotlib

import pandas as pd
import numpy as np

data = pd.Series(np.random.randn(1000))

data.hist(bins = 50)

给予:

enter image description here

这是你的追求吗?