0

i have this kind of csv data example data i want to show them with matplotlib like this figure example figure with latitude and longitude as x and y.

this is my code

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)

i've tried this but it doesn't work

plt.figure()
plt.scatter(X[:,0],X[:,1],c=y,cmap=cmap,edgecolors='k',s=20)
plt.show()

it gives this error

---------------------------------------------------------------------------
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>

im still new to python and can't read the error and fix it because the error is so long.. any suggestions how to fix it?

Jessen Jie
  • 167
  • 1
  • 12
  • Does this answer your question? [Adding a legend to PyPlot in Matplotlib in the simplest manner possible](https://stackoverflow.com/questions/19125722/adding-a-legend-to-pyplot-in-matplotlib-in-the-simplest-manner-possible) – medium-dimensional Dec 23 '22 at 07:53

1 Answers1

1

A solution to plot longitude as a function of latitude is to simply use plot function from matplotlib.

I'm using my own data but the goal is the same. Here is my csv file :

enter image description here

Afterwards, I'm using pandas and matplotlib as you to manage datas. Showing annotations on hovering a point isn't integrated in matplotlib library, I can recommend you to use mplcursors. Here is the code :

import pandas as pd
import matplotlib.pyplot as plt
import mplcursors as mpl

data = pd.read_csv("5coords.csv", sep=";")
df = pd.DataFrame(data)

X, Y, labels = df['Latitude'], df['Longitude'], df['Wisata']

fig, ax = plt.subplots()
line, = ax.plot(X, Y, 'ro')

mpl.cursor(ax).connect(
    "add", lambda sel: sel.annotation.set_text(labels[sel.index]))

plt.show()

Output :

enter image description here

Have a good day.

mafedy
  • 58
  • 4
  • Thankyou very muchhh.. , how do you get data like that? even though csv cannot have coma in them – Jessen Jie Dec 23 '22 at 11:48
  • 1
    In fact, you can arrange the data as you want. Default separator in .csv format is « ; », but you can choose whatever you want. Just replace the separators by opening your .csv file that is containing the datas with a text editor, and set the same in read_csv() argument. – mafedy Dec 23 '22 at 17:31