我有这样的csv数据
,我想用matplotlib来显示它们,如图
,纬度和经度分别为x和y。
这是我的密码
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets
from sklearn.model_selection import train_test_split
from matplotlib.colors import ListedColormap
cmap = ListedColormap(['#FF0000','#00FF00','#0000FF'])
df = pd.DataFrame(data)
X = df.drop(['Wisata','Link Gambar','Nama','Region'],axis = 1) #this left with only latitude and #longitude
y = df['Wisata'] #this is the label
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size = 0.2,random_state=1)
我试过了,但不管用
plt.figure()
plt.scatter(X[:,0],X[:,1],c=y,cmap=cmap,edgecolors='k',s=20)
plt.show()
它会产生以下错误
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
D:\Anaconda\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
3628 try:
-> 3629 return self._engine.get_loc(casted_key)
3630 except KeyError as err:
D:\Anaconda\lib\site-packages\pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()
D:\Anaconda\lib\site-packages\pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()
TypeError: '(slice(None, None, None), 0)' is an invalid key
During handling of the above exception, another exception occurred:
InvalidIndexError Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_12876\2344342603.py in <module>
1 plt.figure()
----> 2 plt.scatter(X[:,0],X[:,1],c=y,cmap=cmap,edgecolors='k',s=20)
3 plt.show()
D:\Anaconda\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
3503 if self.columns.nlevels > 1:
3504 return self._getitem_multilevel(key)
-> 3505 indexer = self.columns.get_loc(key)
3506 if is_integer(indexer):
3507 indexer = [indexer]
D:\Anaconda\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
3634 # InvalidIndexError. Otherwise we fall through and re-raise
3635 # the TypeError.
-> 3636 self._check_indexing_error(key)
3637 raise
3638
D:\Anaconda\lib\site-packages\pandas\core\indexes\base.py in _check_indexing_error(self, key)
5649 # if key is not a scalar, directly raise an error (the code below
5650 # would convert to numpy arrays and raise later any way) - GH29926
-> 5651 raise InvalidIndexError(key)
5652
5653 @cache_readonly
InvalidIndexError: (slice(None, None, None), 0)
<Figure size 640x480 with 0 Axes>
我对python还是新手,不能读取错误并修复它,因为错误太长了。有什么建议吗?
1条答案
按热度按时间lvmkulzt1#
将经度绘制为纬度的函数的解决方案是简单地使用 * matplotlib * 中的plot函数。
我使用自己的数据,但目标是相同的。以下是我的 * csv * 文件:
之后,我会像你一样使用 * panda * 和 * matplotlib * 来管理数据。* matplotlib * 库中没有集成悬停时显示注解,我可以推荐你使用 * mplcursors *。以下是代码:
输出:
祝你愉快。