Plot your Geographical Data using Plotly and MapBox

Richard Gao
2 min readAug 20, 2021

--

Got 5 minutes? Got Python? Want to visualize your data like the image below? Then let’s get into it.

Data plotted over the United States

Don’t have coordinate data? You can generate some in less than 5 minutes in this article here: https://rich-gaogle.medium.com/get-coordinates-via-mapboxs-geocoding-api-cbe9b089b4a7?sk=517f6da5a982722d4d25aa00ed432026

Install the relevant libraries and import them

Plotly, pandas, json and request library import calls

Code

You can grab an access token from: https://docs.mapbox.com/help/getting-started/access-tokens/

We use the px.scatter_mapbox function to create a visualization of the latitude and longitude data provided. Certain visualizations can be used if we provide a mapbox token. Our data here is imported from a repository. If you have your own data you can replace these lines with data of your own. In our visualization, we color each data point by its state and make the point size based on population of that city. The results:

Done! Happy coding.

Code (For copying and pasting)

!pip install pandas;pip install plotly

# Import Libraries
import requests
import json
import pandas as pd
import ssl
MAPBOX_APIKEY=”<insert token here>”
ssl._create_default_https_context = ssl._create_unverified_context

us_cities = pd.read_csv(“https://raw.githubusercontent.com/plotly/datasets/master/us-cities-top-1k.csv")

px.set_mapbox_access_token(MAPBOX_APIKEY)
map_scatter_fig = px.scatter_mapbox(us_cities, lat=”lat”, lon=”lon”, color=”State”, size=”Population”)
map_scatter_fig.show()

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Richard Gao
Richard Gao

Written by Richard Gao

Computer Science and Data Enthusiast | Linkedin: https://www.linkedin.com/in/richard-gao-csecon/ | Shovelling data into the AI engine

No responses yet

Write a response