Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plot latitude longitude from CSV in Python 3.6

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?

like image 739
Tina J Avatar asked Nov 09 '18 21:11

Tina J


People also ask

How do I plot a Google map in Python?

gmplot is a matplotlib-like interface to generate the HTML and javascript to render all the data user would like on top of Google Maps. # latitude and longitude of given location . Code #3 : Scatter points on the google map and draw a line in between them .

How do I plot the coordinates of a map in Matplotlib?

Weird world. 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.

How do you plot latitude and longitude on Plotly?

To graph our longitude and latitude data we can use plotly’s “scatter_geo” function. This function is basically a scatter graph, but with an in-built geographical map that is overlayed below the scatter graph, which makes it easier for representation.

How do you plot geospatial data in Python?

Plotly: Plot Geographical Data using a plotly scatter graph Another way to graph our geospatial data is using a python library called “plotly”. Plotly is a well-known python library because of its ability to provide more graphical tools and functions compared to matplotlib.

What is longitude and latitude data?

Longitude and latitude data are geographical points in a map, this is also known as geospatial data, the latitude and longitude data points allow us to pinpoint an exact location of a place within the world.


2 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

like image 110
Xiaoyu Lu Avatar answered Oct 13 '22 22:10

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()
like image 28
Tirth Avatar answered Oct 13 '22 23:10

Tirth