熊猫绘图错误TypeError:空“ DataFrame”:没有要绘制的数字数据

时间:2018-07-26 13:34:04

标签: python pandas matplotlib

想从熊猫数据框中绘制一些数据

相关信息

df13.head()

Country     HDI
1   Norway  0.949
2   Australia   0.939
3   Switzerland     0.939
4   Germany     0.926
5   Denmark     0.925

df13.shape
>>> (169, 2)

df13['HDI'].dtype
>>> dtype('float64')

len(df13['Country'].unique())
>>> 169

因此没有重复的国家/地区名称

以这种方式进行绘图会创建一个没有国家名称的绘图

df13.plot.barh(figsize=(20,20), use_index=False)

enter image description here

尝试将x,y分配给绘图会引发错误,仅对HDI列执行此错误,

df13.plot(kind='barh', x='HDI')

TypeErrorTraceback (most recent call last)
<ipython-input-77-c7ec3932c7c4> in <module>()
----> 1 df13.plot(kind='barh', x='HDI')

c:\python27\lib\site-packages\pandas\plotting\_core.pyc in __call__(self, x, y, kind, ax, subplots, sharex, sharey, layout, figsize, use_index, title, grid, legend, style, logx, logy, loglog, xticks, yticks, xlim, ylim, rot, fontsize, colormap, table, yerr, xerr, secondary_y, sort_columns, **kwds)
   2939                           fontsize=fontsize, colormap=colormap, table=table,
   2940                           yerr=yerr, xerr=xerr, secondary_y=secondary_y,
-> 2941                           sort_columns=sort_columns, **kwds)
   2942     __call__.__doc__ = plot_frame.__doc__
   2943 

c:\python27\lib\site-packages\pandas\plotting\_core.pyc in plot_frame(data, x, y, kind, ax, subplots, sharex, sharey, layout, figsize, use_index, title, grid, legend, style, logx, logy, loglog, xticks, yticks, xlim, ylim, rot, fontsize, colormap, table, yerr, xerr, secondary_y, sort_columns, **kwds)
   1975                  yerr=yerr, xerr=xerr,
   1976                  secondary_y=secondary_y, sort_columns=sort_columns,
-> 1977                  **kwds)
   1978 
   1979 

c:\python27\lib\site-packages\pandas\plotting\_core.pyc in _plot(data, x, y, subplots, ax, kind, **kwds)
   1802         plot_obj = klass(data, subplots=subplots, ax=ax, kind=kind, **kwds)
   1803 
-> 1804     plot_obj.generate()
   1805     plot_obj.draw()
   1806     return plot_obj.result

c:\python27\lib\site-packages\pandas\plotting\_core.pyc in generate(self)
    256     def generate(self):
    257         self._args_adjust()
--> 258         self._compute_plot_data()
    259         self._setup_subplots()
    260         self._make_plot()

c:\python27\lib\site-packages\pandas\plotting\_core.pyc in _compute_plot_data(self)
    371         if is_empty:
    372             raise TypeError('Empty {0!r}: no numeric data to '
--> 373                             'plot'.format(numeric_data.__class__.__name__))
    374 
    375         self.data = numeric_data

TypeError: Empty 'DataFrame': no numeric data to plot

不确定为什么会弹出此错误,我还检查了空值,没有空值。

df13.isnull().any()

Country    False
HDI        False
dtype: bool

1 个答案:

答案 0 :(得分:2)

您会收到此错误,因为在'barh'中,图条是水平的,而'HDI'在y轴上。

尝试这个:

df13.plot(kind='barh', y='HDI')