Python 3; MatplotLib;方框图错误

时间:2017-03-25 11:38:57

标签: python-3.x pandas matplotlib

我是python / pandas的新手,并尝试使用iris数据集创建一个boxplot。

这是我的代码。:

import pandas as pd
iris_filename = '/Users/pro/Documents/Code/Data Science/Iris/IRIS.csv'
    iris = pd.read_csv(iris_filename, header = None, 
names= ['sepal_lenght','sepal_width','petal_lenght','petal_width','target'])
plt.boxplot(iris)

我收到此错误:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-20-e190e88674b0> in <module>()
----> 1 plt.boxplot(iris)

/anaconda/lib/python3.5/site-packages/matplotlib/pyplot.py in boxplot(x, notch, sym, vert, whis, positions, widths, patch_artist, bootstrap, usermedians, conf_intervals, meanline, showmeans, showcaps, showbox, showfliers, boxprops, labels, flierprops, medianprops, meanprops, capprops, whiskerprops, manage_xticks, autorange, zorder, hold, data)
   2784                          whiskerprops=whiskerprops,
   2785                          manage_xticks=manage_xticks, autorange=autorange,
-> 2786                          zorder=zorder, data=data)
   2787     finally:
   2788         ax._hold = washold

/anaconda/lib/python3.5/site-packages/matplotlib/__init__.py in inner(ax, *args, **kwargs)
   1890                     warnings.warn(msg % (label_namer, func.__name__),
   1891                                   RuntimeWarning, stacklevel=2)
-> 1892             return func(ax, *args, **kwargs)
   1893         pre_doc = inner.__doc__
   1894         if pre_doc is None:

/anaconda/lib/python3.5/site-packages/matplotlib/axes/_axes.py in boxplot(self, x, notch, sym, vert, whis, positions, widths, patch_artist, bootstrap, usermedians, conf_intervals, meanline, showmeans, showcaps, showbox, showfliers, boxprops, labels, flierprops, medianprops, meanprops, capprops, whiskerprops, manage_xticks, autorange, zorder)
   3266             bootstrap = rcParams['boxplot.bootstrap']
   3267         bxpstats = cbook.boxplot_stats(x, whis=whis, bootstrap=bootstrap,
-> 3268                                        labels=labels, autorange=autorange)
   3269         if notch is None:
   3270             notch = rcParams['boxplot.notch']

/anaconda/lib/python3.5/site-packages/matplotlib/cbook.py in boxplot_stats(X, whis, bootstrap, labels, autorange)
   1984 
   1985     # convert X to a list of lists
-> 1986     X = _reshape_2D(X)
   1987 
   1988     ncols = len(X)

/anaconda/lib/python3.5/site-packages/matplotlib/cbook.py in _reshape_2D(X)
   2245                 X = [X.ravel()]
   2246             else:
-> 2247                 X = [X[:, i] for i in xrange(ncols)]
   2248         else:
   2249             raise ValueError("input `X` must have 2 or fewer dimensions")

/anaconda/lib/python3.5/site-packages/matplotlib/cbook.py in <listcomp>(.0)
   2245                 X = [X.ravel()]
   2246             else:
-> 2247                 X = [X[:, i] for i in xrange(ncols)]
   2248         else:
   2249             raise ValueError("input `X` must have 2 or fewer dimensions")

/anaconda/lib/python3.5/site-packages/pandas/core/frame.py in __getitem__(self, key)
   2057             return self._getitem_multilevel(key)
   2058         else:
-> 2059             return self._getitem_column(key)
   2060 
   2061     def _getitem_column(self, key):

/anaconda/lib/python3.5/site-packages/pandas/core/frame.py in _getitem_column(self, key)
   2064         # get column
   2065         if self.columns.is_unique:
-> 2066             return self._get_item_cache(key)
   2067 
   2068         # duplicate columns & possible reduce dimensionality

/anaconda/lib/python3.5/site-packages/pandas/core/generic.py in _get_item_cache(self, item)
   1382         """Return the cached item, item represents a label indexer."""
   1383         cache = self._item_cache
-> 1384         res = cache.get(item)
   1385         if res is None:
   1386             values = self._data.get(item)

TypeError: unhashable type: 'slice'

我在网上搜索了这个,似乎无法找到这个问题的答案。我将不胜感激任何帮助。

1 个答案:

答案 0 :(得分:1)

您正在调用matplotlib来封装DataFrame虹膜...因为您已经使用Pandas导入.csv,您还应该将其用于绘图:

iris.boxplot()

Pandas boxplot api