Plot latitude longitude from CSV in Python 3.6

Question:

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?

Asked By: Tina J

||

Answers:

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

Answered By: Xiaoyu Lu

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()
Answered By: Tirth

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

Answered By: leenremm