We.Mystic 发表于 2021-10-8 23:47:52

pandas绘制散点图

在用pandas绘制散点图时报错,请问要怎么改

We.Mystic 发表于 2021-10-8 23:50:58

代码:housing.plot(kind="scatter",x="longitude",y="latitude",alpha=0.1,figsize=(7,7))
报错:KeyError                                  Traceback (most recent call last)
d:\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   2645             try:
-> 2646               return self._engine.get_loc(key)
   2647             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: 'longitude'

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-12-b6dc1248a5f8> in <module>
----> 1 housing.plot(kind="scatter",x="longitude",y="latitude",alpha=0.1,figsize=(7,7))

d:\Anaconda3\lib\site-packages\pandas\plotting\_core.py in __call__(self, *args, **kwargs)
    790         if kind in self._dataframe_kinds:
    791             if isinstance(data, ABCDataFrame):
--> 792               return plot_backend.plot(data, x=x, y=y, kind=kind, **kwargs)
    793             else:
    794               raise ValueError(f"plot kind {kind} can only be used for data frames")

d:\Anaconda3\lib\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(data, **kwargs)
---> 61   plot_obj.generate()
   62   plot_obj.draw()
   63   return plot_obj.result

d:\Anaconda3\lib\site-packages\pandas\plotting\_matplotlib\core.py in generate(self)
    261         self._compute_plot_data()
    262         self._setup_subplots()
--> 263         self._make_plot()
    264         self._add_table()
    265         self._make_legend()

d:\Anaconda3\lib\site-packages\pandas\plotting\_matplotlib\core.py in _make_plot(self)
    968             label = None
    969         scatter = ax.scatter(
--> 970             data.values,
    971             data.values,
    972             c=c_values,

d:\Anaconda3\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
   2798             if self.columns.nlevels > 1:
   2799               return self._getitem_multilevel(key)
-> 2800             indexer = self.columns.get_loc(key)
   2801             if is_integer(indexer):
   2802               indexer =

d:\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   2646               return self._engine.get_loc(key)
   2647             except KeyError:
-> 2648               return self._engine.get_loc(self._maybe_cast_indexer(key))
   2649         indexer = self.get_indexer(, method=method, tolerance=tolerance)
   2650         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: 'longitude'

z5560636 发表于 2021-10-9 00:46:46

变量引用错误?把双引号去了看看
housing.plot(kind=scatter,x=longitude,y=latitude,alpha=0.1,figsize=(7,7))

We.Mystic 发表于 2021-10-9 19:55:24

z5560636 发表于 2021-10-9 00:46
变量引用错误?把双引号去了看看

scatter这些参数要用双引号的,用了也还是不对

We.Mystic 发表于 2021-10-9 20:06:43

We.Mystic 发表于 2021-10-9 19:55
scatter这些参数要用双引号的,用了也还是不对

我自己x,y写错了。。网上下的数据集和书上的数据名字不一样
页: [1]
查看完整版本: pandas绘制散点图