鱼C论坛

 找回密码
 立即注册
查看: 1535|回复: 4

pandas绘制散点图

[复制链接]
发表于 2021-10-8 23:47:52 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
在用pandas绘制散点图时报错,请问要怎么改
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 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[kind](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[x].values,
    971             data[y].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 = [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([key], 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'
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-10-9 00:46:46 | 显示全部楼层
变量引用错误?  把双引号去了看看
housing.plot(kind=scatter,x=longitude,y=latitude,alpha=0.1,figsize=(7,7))
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-10-9 19:55:24 | 显示全部楼层
z5560636 发表于 2021-10-9 00:46
变量引用错误?  把双引号去了看看

scatter这些参数要用双引号的,用了也还是不对
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-10-9 20:06:43 | 显示全部楼层
We.Mystic 发表于 2021-10-9 19:55
scatter这些参数要用双引号的,用了也还是不对

我自己x,y写错了。。网上下的数据集和书上的数据名字不一样
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-10-6 20:25

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表