如何使用pandas从数据框绘制直方图。

10

我有一个简单的pandas数据框,其中有两列数字。我希望使用matplotlib通过pandas将这些列制成直方图。以下示例无效:

In [6]: pandas.__version__
Out[6]: '0.14.1'

In [7]: df
Out[7]: 
   a   b
0  1  20
1  2  40
2  3  30
3  4  30
4  4   3
5  3   5

In [8]: df.plot(kind="hist")
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-8-4f53176a4683> in <module>()
----> 1 df.plot(kind="hist")

/software/lib/python2.7/site-packages/pandas/tools/plotting.pyc in plot_frame(frame, x, y, subplots, sharex, sharey, use_index, figsize, grid, legend, rot, ax, style, title, xlim, ylim, logx, logy, xticks, yticks, kind, sort_columns, fontsize, secondary_y, **kwds)
   2095         klass = _plot_klass[kind]
   2096     else:
-> 2097         raise ValueError('Invalid chart type given %s' % kind)
   2098 
   2099     if kind in _dataframe_kinds:

ValueError: Invalid chart type given hist

为什么它说图表类型无效?这些列是数字,可以制作成直方图。


尝试重新安装Pandas。这对我起作用了。 - samthebrand
2个回答

16

DataFrame 有自己的 hist 方法:

df =pd.DataFrame({'col1':np.random.randn(100),'col2':np.random.randn(100)})
df.hist(layout=(1,2))   

dataframe的每个有效列绘制一个柱状图。

输入图像描述


2

我不认为“hist”是0.14.1版本中支持的类型。请尝试使用df.hist()。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接