python - pandas plotting bug TypeError: Empty 'DataFrame': no numeric data to plot
问题描述
想要从 pandas 数据框中绘制一些数据
相关信息
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)
尝试将 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
解决方案
您收到此错误是因为在“barh”图中,条形图是水平的,而“HDI”在 y 轴上。
试试这个:
df13.plot(kind='barh', y='HDI')
推荐阅读
- java - Windows 上 Docker 桌面上的 Spring Boot 应用程序主机
- python - PyCharm .spec 文件错误:“RpmSpecTokenType.( or RpmSpecTokenType.: expected, got '='”
- sql-server - 更新查询挂起
- javascript - 由于“没有重载匹配此调用”,无法将 eventName 和回调传递给 addEventListener
- android - MediaBrowserServiceCompat:Context.startForegroundService() 没有调用 Service.startForeground()
- react-native - 在 react native /w expo 中访问 event.nativeEvent.layout.x 的问题
- c++ - 尽管链接到库,但对函数的未定义引用
- .net-core - 如何在 Blazor wasm 和 .Net 5 之间共享课程?
- javascript - 如何使用 CSS3 制作这样的曲线边框?
- python - Yahoo Finance API 为某些代码返回 NAN