簡體   English   中英

生成單元素序列的直方圖時,Seaborn distplot錯誤

[英]Seaborn distplot errors when generating histograms for single-element series

當我使用seaborn的distplot (在0.7.1中)可視化直方圖時,如果輸入系列僅包含單個元素,則會出現錯誤。 換句話說,就像

import numpy as np
import seaborn as sns

num_elements = 1000
sns.distplot(np.random.normal(10, 1, num_elements))

工作num_elements漂亮,但如果我將num_elements設置為1,我會收到以下錯誤:

In [8]: num_elements = 1

In [9]: sns.distplot(np.random.normal(10, 1, num_elements))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-9-35e3f8293582> in <module>()
----> 1 sns.distplot(np.random.normal(10, 1, num_elements))

/usr/local/lib/python2.7/site-packages/seaborn/distributions.pyc in distplot(a, bins, hist, kde, rug, fit, hist_kws, kde_kws, rug_kws, fit_kws, color, vertical, norm_hist, axlabel, label, ax)
    207     if hist:
    208         if bins is None:
--> 209             bins = min(_freedman_diaconis_bins(a), 50)
    210         hist_kws.setdefault("alpha", 0.4)
    211         hist_kws.setdefault("normed", norm_hist)

/usr/local/lib/python2.7/site-packages/seaborn/distributions.pyc in _freedman_diaconis_bins(a)
     28     # From http://stats.stackexchange.com/questions/798/
     29     a = np.asarray(a)
---> 30     h = 2 * iqr(a) / (len(a) ** (1 / 3))
     31     # fall back to sqrt(a) bins if iqr is 0
     32     if h == 0:

TypeError: len() of unsized object

在稍微探討了源代碼之后,我發現通過指定bin的數量可以避免這個錯誤。 但這讓我開啟了另一個:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-10-6e6569de737a> in <module>()
----> 1 sns.distplot(np.random.normal(10, 1, num_elements), bins=10)

/usr/local/lib/python2.7/site-packages/seaborn/distributions.pyc in distplot(a, bins, hist, kde, rug, fit, hist_kws, kde_kws, rug_kws, fit_kws, c\olor, vertical, norm_hist, axlabel, label, ax)
    213         hist_color = hist_kws.pop("color", color)
    214         ax.hist(a, bins, orientation=orientation,
--> 215                 color=hist_color, **hist_kws)
    216         if hist_color != color:
    217             hist_kws["color"] = hist_color

/usr/local/lib/python2.7/site-packages/matplotlib/__init__.pyc in inner(ax, *args, **kwargs)
   1817                     warnings.warn(msg % (label_namer, func.__name__),
   1818                                   RuntimeWarning, stacklevel=2)
-> 1819             return func(ax, *args, **kwargs)
   1820         pre_doc = inner.__doc__
   1821         if pre_doc is None:

/usr/local/lib/python2.7/site-packages/matplotlib/axes/_axes.pyc in hist(self, x, bins, range, normed, weights, cumulative, bottom, histtype, align, orientation, rwidth, log, color, label, stacked, **kwargs)
   5933             x = np.array([[]])
   5934         else:
-> 5935             x = _normalize_input(x, 'x')
   5936         nx = len(x)  # number of datasets
   5937

/usr/local/lib/python2.7/site-packages/matplotlib/axes/_axes.pyc in _normalize_input(inp, ename)
   5875                 else:
   5876                     raise ValueError(
-> 5877                         "{ename} must be 1D or 2D".format(ename=ename))
   5878                 if inp.shape[1] < inp.shape[0]:
   5879                     warnings.warn(

ValueError: x must be 1D or 2D

這里發生了什么? 是否有一些解決方法可以用來強制seaborn為這個單個元素系列繪制直方圖? 對於上下文,我需要這個,因為我在共享軸上繪制多個直方圖,描述不同的條件。 在某些情況下,在這種情況下可能只有一個元素,但我應該只能繪制一個條形圖!

通過復制值,您可以在數組長度為1時欺騙seaborn

rs = np.random.RandomState(0)
num_elements = 1
x = rs.normal(10, 1, num_elements)
sns.distplot(np.r_[x, x], kde=False, norm_hist=True)

seaborn直方圖

或者,如果您不需要seaborn提供的好東西(大多數對單個元素沒有意義),只需使用matplotlib或pandas:

plt.hist(x, bins=1)

在此輸入圖像描述

pd.Series(x).hist(bins=1);

在此輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM