Plot latitude longitude from CSV in Python 3.6

61,323

Solution 1

If you are just looking at plotting the point data as a scatterplot, is as simple as

import matplotlib.pyplot as plt
plt.scatter(x=df['Longitude'], y=df['Latitude'])
plt.show()

If you want to plot the points on the map, it's getting interesting because it depends more on how you plot your map.

A simple way is to use shapely and geopandas. The code below is not tested given my limited access on the laptop I am currently using, but it should give you a conceptual roadmap.

import pandas as pd
from shapely.geometry import Point
import geopandas as gpd
from geopandas import GeoDataFrame

df = pd.read_csv("Long_Lats.csv", delimiter=',', skiprows=0, low_memory=False)

geometry = [Point(xy) for xy in zip(df['Longitude'], df['Latitude'])]
gdf = GeoDataFrame(df, geometry=geometry)   

#this is a simple map that goes with geopandas
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
gdf.plot(ax=world.plot(figsize=(10, 6)), marker='o', color='red', markersize=15);

Find below an example of the rendered image:

enter image description here

Solution 2

You can also use plotly express to plot the interactive worldmap for latitude and longitude

DataFrame Head

import plotly.express as px
import pandas as pd

df = pd.read_csv("location_coordinate.csv")

fig = px.scatter_geo(df,lat='lat',lon='long', hover_name="id")
fig.update_layout(title = 'World map', title_x=0.5)
fig.show()
Share:
61,323
Tina J
Author by

Tina J

Updated on August 02, 2022

Comments

  • Tina J
    Tina J almost 2 years

    I'm trying to plot a large number of latitude longitude values from a CSV file on a map, having this format (first column and second column):

    enter image description here

    I'm using python 3.6 (apparently some libraries like Basemap doesn't operate on this version).

    How can I do that?

    • Xiaoyu Lu
      Xiaoyu Lu over 5 years
      Are you asking about a web-based map using google maps api? Or just a scatterplot with a map in the background?
    • Tina J
      Tina J over 5 years
      I'm sorry...I mean on a real map!
    • Tina J
      Tina J about 5 years
      This question has earned "popular question" badge, but I see a -2 vote!! Weird world.
    • MPatel1
      MPatel1 about 2 years
      Not really Python but check out Kepler.gl
  • Tina J
    Tina J over 5 years
    I'm sorry...I mean a real map!
  • Xiaoyu Lu
    Xiaoyu Lu over 5 years
    @TinaJ I apologize for misunderstanding. Has updated the answer. Cannot test if the code works on current laptop but would revise later if it does not.
  • Mahfuja nilufar
    Mahfuja nilufar about 4 years
    @ Xiaoyu Lu, I have 3 type of class. So for every lattitiude and longitude there assign one class. How can I put 3 different color for 3 differnt class. i.e. class1 there is 60% geolocation, class2 20% and in class3 20% geolocation. here is my data structure, df['lattitiude','longitude','class'] = [53.679886 , 9.372680, 1]; class is labeled by 1,0,2
  • user
    user over 3 years
    +1. Also, geopandas has a wrapper function: points_from_xy() which is equivalent to: [Point(x, y) for x, y in zip(df.Longitude, df.Latitude)] geopandas.org/gallery/…
  • Superdooperhero
    Superdooperhero about 3 years
    How can I zoom the naturalearth_lowres map into South Africa?
  • Sulphur
    Sulphur about 3 years
    For people in future reading the answer, note the sequence: it is Longitude and then Latitude. Not vice versa.
  • swag2198
    swag2198 about 3 years
    No need to put the dataframe head as a link/image. You can simply format it as code and put it inline with your answer.
  • Tirth
    Tirth about 3 years
    Sure Noted Thank you.
  • independentvariable
    independentvariable over 2 years
    @Superdooperhero do we have an answer yet about zooming in:?