Python 如何从熊猫数据框中绘制直方图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28654003/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to plot histograms from dataframes in pandas
提问by lgd
I have a simple dataframe in pandas that has two numeric columns. I want to make a histogram out of the columns using matplotlib through pandas. The example below does not work:
我在 Pandas 中有一个简单的数据框,它有两个数字列。我想通过 Pandas 使用 matplotlib 从列中制作直方图。下面的例子不起作用:
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
why does it say invalid chart type? the columns are numeric and can be made into histograms.
为什么它说无效的图表类型?列是数字的,可以制成直方图。
采纳答案by JAB
DataFrame
has its own hist
method:
DataFrame
有自己的hist
方法:
df =pd.DataFrame({'col1':np.random.randn(100),'col2':np.random.randn(100)})
df.hist(layout=(1,2))
draws a histogram for each valid column of the dataframe
.
为 的每个有效列绘制直方图dataframe
。
回答by Bob Haffner
I don't believe 'hist' was a supported type in 0.14.1. Try df.hist() instead
我不相信 'hist' 是 0.14.1 中支持的类型。尝试 df.hist() 代替