试图绘制子地块,但将其设置为空白

2024-04-25 00:23:29 发布

您现在位置:Python中文网/ 问答频道 /正文

试图绘制子图以比较3个不同的列,但显示为空白。将导入所有必需的库,并且使用的列名也与.csv文件中的列名匹配。下面是我使用的代码。我没有权限附加空白图的图像,所以我在下面贴了一个链接

# visualize the relationship between the features and the response using scatterplots
fig, axs = plt.subplots(1, 3, sharey=True)
data.plot(kind='scatter', x='TV', y='sales', ax=axs[0], figsize=(16, 8))
data.plot(kind='scatter', x='radio', y='sales', ax=axs[1])
data.plot(kind='scatter', x='newspaper', y='sales', ax=axs[2])

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
C:\Users\kanks\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   2896             try:
-> 2897                 return self._engine.get_loc(key)
   2898             except KeyError:

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'TV'

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-8-f7be7f05c794> in <module>
      1 # visualize the relationship between the features and the response using scatterplots
      2 fig, axs = plt.subplots(1, 3, sharey=True)
----> 3 data.plot(kind='scatter', x='TV', y='sales', ax=axs[0], figsize=(16, 8))
      4 data.plot(kind='scatter', x='radio', y='sales', ax=axs[1])
      5 data.plot(kind='scatter', x='newspaper', y='sales', ax=axs[2])

C:\Users\kanks\Anaconda3\lib\site-packages\pandas\plotting\_core.py in __call__(self, *args, **kwargs)
    736         if kind in self._dataframe_kinds:
    737             if isinstance(data, ABCDataFrame):
--> 738                 return plot_backend.plot(data, x=x, y=y, kind=kind, **kwargs)
    739             else:
    740                 raise ValueError(

C:\Users\kanks\Anaconda3\lib\site-packages\pandas\plotting\_matplotlib\__init__.py in plot(data, kind, **kwargs)
     59                 ax = plt.gca()
     60             kwargs["ax"] = getattr(ax, "left_ax", ax)
---> 61     plot_obj = PLOT_CLASSES[kind](data, **kwargs)
     62     plot_obj.generate()
     63     plot_obj.draw()

C:\Users\kanks\Anaconda3\lib\site-packages\pandas\plotting\_matplotlib\core.py in __init__(self, data, x, y, s, c, **kwargs)
    928             # the handling of this argument later
    929             s = 20
--> 930         super().__init__(data, x, y, s=s, **kwargs)
    931         if is_integer(c) and not self.data.columns.holds_integer():
    932             c = self.data.columns[c]

C:\Users\kanks\Anaconda3\lib\site-packages\pandas\plotting\_matplotlib\core.py in __init__(self, data, x, y, **kwargs)
    867         if is_integer(y) and not self.data.columns.holds_integer():
    868             y = self.data.columns[y]
--> 869         if len(self.data[x]._get_numeric_data()) == 0:
    870             raise ValueError(self._kind + " requires x column to be numeric")
    871         if len(self.data[y]._get_numeric_data()) == 0:

C:\Users\kanks\Anaconda3\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
   2978             if self.columns.nlevels > 1:
   2979                 return self._getitem_multilevel(key)
-> 2980             indexer = self.columns.get_loc(key)
   2981             if is_integer(indexer):
   2982                 indexer = [indexer]

C:\Users\kanks\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   2897                 return self._engine.get_loc(key)
   2898             except KeyError:
-> 2899                 return self._engine.get_loc(self._maybe_cast_indexer(key))
   2900         indexer = self.get_indexer([key], method=method, tolerance=tolerance)
   2901         if indexer.ndim > 1 or indexer.size > 1:

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'TV' 

enter image description here


Tags: thekeyinselfpandasdatagetif
1条回答
网友
1楼 · 发布于 2024-04-25 00:23:29

能否验证列TV是否存在?通过打印:

print(data.columns)

由于数据集中没有专栏电视,请尝试注释第一个情节。像这样,现在您应该至少看到2个绘图

#data.plot(kind='scatter', x='TV', y='sales', ax=axs[0], figsize=(16,8))

相关问题 更多 >