python - TypeError:没有数值数据可以在水平条中绘制
问题描述
我有这个数据集
Variable Value
0 x1 0.001092
1 x2 0.000001
2 x3 0.002040
3 x4 0.000021
4 x5 0.000000
我想绘制一个水平条形图:
df.plot(x = "Variable", y="Value", kind="barh")
但我得到了错误
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-130-0c2ee6896cfe> in <module>
----> 1 df.plot(x = "Variable", y="Value", kind="barh")
~/opt/anaconda3/lib/python3.7/site-packages/pandas/plotting/_core.py in __call__(self, *args, **kwargs)
953 data.columns = label_name
954
--> 955 return plot_backend.plot(data, kind=kind, **kwargs)
956
957 __call__.__doc__ = __doc__
~/opt/anaconda3/lib/python3.7/site-packages/pandas/plotting/_matplotlib/__init__.py in plot(data, kind, **kwargs)
59 kwargs["ax"] = getattr(ax, "left_ax", ax)
60 plot_obj = PLOT_CLASSES[kind](data, **kwargs)
---> 61 plot_obj.generate()
62 plot_obj.draw()
63 return plot_obj.result
~/opt/anaconda3/lib/python3.7/site-packages/pandas/plotting/_matplotlib/core.py in generate(self)
276 def generate(self):
277 self._args_adjust()
--> 278 self._compute_plot_data()
279 self._setup_subplots()
280 self._make_plot()
~/opt/anaconda3/lib/python3.7/site-packages/pandas/plotting/_matplotlib/core.py in _compute_plot_data(self)
439 # no non-numeric frames or series allowed
440 if is_empty:
--> 441 raise TypeError("no numeric data to plot")
442
443 self.data = numeric_data.apply(self._convert_to_ndarray)
TypeError: no numeric data to plot
我希望你能帮忙。
解决方案
你的代码对我来说非常好。我原以为您的 Value 列中有字符串,但显然情况并非如此。从下面的代码中复制并粘贴 dataframe-from-dictionary。如果你不能绘制它,那么你的环境就有问题。
df = pd.DataFrame({'Variable': {0: 'x1', 1: 'x2', 2: 'x3', 3: 'x4', 4: 'x5'}, 'Value': {0: 0.001092, 1: 1e-06, 2: 0.00204, 3: 2.1e-05, 4: 0.0}})
df.plot(x = "Variable", y="Value", kind="barh")
plt.show()
推荐阅读
- ms-access - Windows 10中的Visual Basic 6程序在删除后仍在使用mdb
- php - Codeigniter 无法连接到 MSSQL 数据库
- neural-network - 在 Fasttext skipgram 训练中,如果语料库中的某些句子只有一个单词会怎样?
- date - 如何将 moment.js 导入 vue
- python - 表示下一行代码的最佳 Python 方法是什么?
- mysql - ajuda com SELECT,帮我选择 mysql
- python - 如何确定 keras 损失函数中 y_true 和 y_pred 的“索引”?
- java - 如何使用带有 Java8 的 spring 创建一个列表?
- python - 如何在特定时间后停止键盘监听器?
- shell - 如何优化 grep