> ## Content Index
> Fetch the complete content index at: https://datascientyst.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# Plot Latitude and Longitude from Pandas DataFrame in Python
- URL: https://datascientyst.com/plot-latitude-longitude-pandas-dataframe-python/
- Published: 2021-08-19T13:41:41.000Z
- Updated: 2022-10-29T21:12:52.000Z
- Author: John D K
- Tags: Other

Need to **plot latitude and longitude from Pandas DataFrame in Python**?

If so, you may use the following libraries to do so:

- `geopandas`
- `shapely`
- `matplotlib` \- optional - if the map is not displayed
- `plotly` \- alternative solution

Below you can find working example and all the steps in order to **convert pairs of latitude and longitude to a world map**.

## Step 1: Install required libraries - geopandas

In order to use the code below you need the latest Python. You need to install several libraries with:

```bash
pip install geopandas
pip install Shapely
pip install matplotlib

```

The `matplotlib` library is needed for Jupyter Notebook if the plot is not shown.

It's a good idea to update all libraries to the latest versions.

## Step 2: Create DataFrame with geocoding data

For this example we are going to use data from [Significant Earthquakes, 1965-2016](https://www.kaggle.com/usgs/earthquake-database?select=database.csv&ref=datascientyst.com).

You can download it from Kaggle or read it with Python - [How to Search and Download Kaggle Dataset to Pandas DataFrame](https://datascientyst.com/search-download-kaggle-dataset-pandas-dataframe/)

You can find the data below:

```python
import pandas as pd

df = pd.read_csv(f'../data/earthquakes_1965_2016_database.csv.zip')
df

```

data:

| Date       | Latitude | Longitude | Depth | Type       |
| ---------- | -------- | --------- | ----- | ---------- |
| 01/02/1965 | 19.246   | 145.616   | 131.6 | Earthquake |
| 01/04/1965 | 1.863    | 127.352   | 80.0  | Earthquake |
| 01/05/1965 | \-20.579 | \-173.972 | 20.0  | Earthquake |
| 01/08/1965 | \-59.076 | \-23.557  | 15.0  | Earthquake |
| 01/09/1965 | 11.938   | 126.427   | 15.0  | Earthquake |

## Step 3: Plot latitude and longitude to world map

Finally we are going to plot the pairs latitude and longitude on the world map. The points will be paint in red color while to earth will be blue:

```python
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
gdf.plot(ax=world.plot(figsize=(15, 15)), marker='o', color='red', markersize=15);

```

The result is:

![plot-latitude-longitude-pandas-dataframe-python](https://datascientyst.com/content/images/2022/10/plot-latitude-longitude-pandas-dataframe-python.webp)

## Step 4: Plot latitude and longitude to interactive map plus hover with plotly

As an alternative solution you can use library **`plotly` to draw a map from latitude and longitude**.

You can find the code for it below:

```python
import plotly.express as px
import pandas as pd

fig = px.scatter_geo(df,lat='Latitude',lon='Longitude', hover_name="Magnitude")
fig.update_layout(title = 'Significant Earthquakes, 1965-2016', title_x=0.5)
fig.show()

```

The advantage of these method is that **plots an interactive map which alows you to hover on the points**.

You can also display important information plus coordinates like:

- name
- latitude
- longitude

or zoom in/out:

![plot-latitude-longitude-pandas-dataframe-python-interactive-map](https://datascientyst.com/content/images/2022/10/plot-latitude-longitude-pandas-dataframe-python-interactive-map.webp)

## Resources

- [Notebook](https://github.com/softhints/datascientyst/blob/master/geocoding/1.plot-latitude-longitude-pandas-dataframe-python.ipynb?ref=datascientyst.com)
- [geopandas](https://pypi.org/project/geopandas/?ref=datascientyst.com)
- [Shapely](https://pypi.org/project/Shapely/?ref=datascientyst.com)
- [matplotlib](https://pypi.org/project/matplotlib/?ref=datascientyst.com)