Plot latitude longitude from CSV in Python 3.6
Asked Answered
F

4

47

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?

Fillmore answered 9/11, 2018 at 21:1 Comment(5)
Are you asking about a web-based map using google maps api? Or just a scatterplot with a map in the background?Fremont
I'm sorry...I mean on a real map!Fillmore
This question has earned "popular question" badge, but I see a -2 vote!! Weird world.Fillmore
Not really Python but check out Kepler.glSelfexplanatory
@TinaJ - I would suggest the new OpenStreets answer to be the most current & correct answer.Albertype
F
63

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

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
# deprecated: world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
world = gpd.read_file(geodatasets.data.naturalearth.land['url'])
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

Fremont answered 9/11, 2018 at 21:29 Comment(8)
I'm sorry...I mean a real map!Fillmore
@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.Fremont
@ 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,2Bodine
+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/…Thorwald
How can I zoom the naturalearth_lowres map into South Africa?Dworman
For people in future reading the answer, note the sequence: it is Longitude and then Latitude. Not vice versa.Engineer
@Dworman do we have an answer yet about zooming in:?Hornet
@Engineer I'm late over here but I think it is because of the convention of x being the first argument and y the second. Since, x depends on longitude, here longitude is the first argument. (x,y) -> (lon,lat)Spouse
A
28

Here's an example of adding Lat & Long to a real OpenStreet map:

import plotly.express as px
import pandas as pd

df = pd.read_csv("dataset/dataset.csv")

df.dropna(
    axis=0,
    how='any',
    thresh=None,
    subset=None,
    inplace=True
)

color_scale = [(0, 'orange'), (1,'red')]

fig = px.scatter_mapbox(df, 
                        lat="Lat", 
                        lon="Long", 
                        hover_name="Address", 
                        hover_data=["Address", "Listed"],
                        color="Listed",
                        color_continuous_scale=color_scale,
                        size="Listed",
                        zoom=8, 
                        height=800,
                        width=800)

fig.update_layout(mapbox_style="open-street-map")
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
fig.show()

Example CSV:

Address,     Lat,       Long,      Listed
Address #1,  -33.941,   18.467,    1250000
Address #2,  -33.942,   18.468,    1900000
Address #3,  -33.941,   18.467,    1200000
Address #4,  -33.936,   18.467,    1195000
Address #5,  -33.944,   18.470,    2400000

Example output (interactive map):

enter image description here

enter image description here

Albertype answered 18/10, 2022 at 5:5 Comment(2)
Is there a way to add a scroll to navigate back and forth through time based series?Bactria
In this format: 8.7,2015-03-09 15:56:39,34.642496,-92.890685,590 7.41,2015-03-09 15:56:39,34.642496,-92.890685,590 6.7,2015-03-09 15:56:39,34.642496,-92.890685,590 7.41,2015-03-09 15:56:39,34.642496,-92.890685,590 10.1,2015-03-09 15:56:39,34.642496,-92.890685,590 6.6,2015-03-09 15:56:39,34.642496,-92.890685,590 10.3,2015-03-09 15:56:39,34.642496,-92.890685,590 6.7,2015-03-09 15:56:39,34.642496,-92.890685,590Bactria
S
18

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()
Slier answered 7/5, 2021 at 3:44 Comment(3)
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.Desiderata
Sure Noted Thank you.Slier
It tells me that I havent installed nbformat, even though I had. Doesnt workJillayne
A
0

Not python-based but if you just want to see your points on a map, we created this really simple app to do that, drag and drop your CSV onto the webpage - just name your first column "Long" and second "Lat" and it should do the job. The link is latlongplot.com

Alpenhorn answered 25/6, 2024 at 8:41 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.