首页 > 解决方案 > 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

我希望你能帮忙。

标签: pythonpandasmatplotlibvisualization

解决方案


你的代码对我来说非常好。我原以为您的 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()

在此处输入图像描述


推荐阅读