如何从 pandas 的数据框中绘制直方图 [英] how to plot histograms from dataframes in pandas

查看:85
本文介绍了如何从 pandas 的数据框中绘制直方图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在pandas中有一个简单的数据框,其中有两个数字列.我想通过熊猫使用matplotlib从列中制作直方图.下面的示例不起作用:

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:

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.

推荐答案

DataFrame有其自己的hist方法:

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

dataframe的每个有效列绘制一个直方图.

draws a histogram for each valid column of the dataframe.

这篇关于如何从 pandas 的数据框中绘制直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆