python - 显示条形图标准偏差的Seaborn错误

时间:2017-07-24 15:02:43

标签: python pandas seaborn

这是我的代码。

我想使用seaborn包添加错误栏作为数据的标准偏差。但是我得到了Type错误。

    import pandas as pd
    %matplotlib inline
    import seaborn as sns
    import matplotlib.pyplot as plt

    example = pd.DataFrame({"value": [2,3,4,5,6,7], "category":[1,2,1,2,1,2]})

    sns.barplot(data= example, x='category', y= 'value', ci = 'sd')

和这样的错误,

    ------------------------------------------------------------------------
    TypeError                                 Traceback (most recent call last)
    <ipython-input-73-d39dbb59d2cc> in <module>()
    ----> 1 sns.barplot(data= example, x='category', y= 'value', ci = 'sd')

    C:\Users\SamSung\Anaconda3\lib\site-packages\seaborn\categorical.py in barplot(x, y, hue, data, order, hue_order, estimator, ci, n_boot, units, orient, color, palette, saturation, errcolor, errwidth, capsize, ax, **kwargs)
       2897                           estimator, ci, n_boot, units,
       2898                           orient, color, palette, saturation,
    -> 2899                           errcolor, errwidth, capsize)
       2900 
       2901     if ax is None:

    C:\Users\SamSung\Anaconda3\lib\site-packages\seaborn\categorical.py in __init__(self, x, y, hue, data, order, hue_order, estimator, ci, n_boot, units, orient, color, palette, saturation, errcolor, errwidth, capsize)
       1543                                  order, hue_order, units)
       1544         self.establish_colors(color, palette, saturation)
    -> 1545         self.estimate_statistic(estimator, ci, n_boot)
       1546 
       1547         self.errcolor = errcolor

    C:\Users\SamSung\Anaconda3\lib\site-packages\seaborn\categorical.py in estimate_statistic(self, estimator, ci, n_boot)
       1450                                       n_boot=n_boot,
       1451                                       units=unit_data)
    -> 1452                     confint.append(utils.ci(boots, ci))
       1453 
       1454             # Option 2: we are grouping by a hue layer

    C:\Users\SamSung\Anaconda3\lib\site-packages\seaborn\utils.py in ci(a, which, axis)
        341 def ci(a, which=95, axis=None):
        342     """Return a percentile range from an array of values."""
    --> 343     p = 50 - which / 2, 50 + which / 2
        344     return percentiles(a, p, axis)
        345 

    TypeError: unsupported operand type(s) for /: 'str' and 'int'

为什么会这样?我该如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

Seaborn在版本0.8.0中添加了使用ci='sd'的功能:

来自“套餐v0.8.0(2017年7月)中的新功能”:

“添加了使用误差线显示标准偏差的能力,而不是通过输入ci =”sd“来显示大多数统计函数中的置信区间。”

参见 - http://seaborn.pydata.org/whatsnew.html

您需要更新Seaborn才能使用它。