How to Create an Attractive Bubble Map in Jupyter | Towards Data Science

Table of ContentsIntroduction1. Libraries2. Data Scraping with BeautifulSoup3. The Mapbox Token4. Plotly.express5. Other Datasets6. Comparing to Plotly.graph_objectConclusion
In this article, I am going to show you how to scrape the latest data from CSSEGISandData’s COVID-19 Github repo and creating a bubble map with Plotly and Mapbox.
We are going to use JupyterLab (or Jupyter Notebook if you prefer), Plotly express, BeautifulSoup, and Mapbox.
Previously it was difficult to install Plotly on JupyterLab, but it is easy now to install. You can find it here.
import requestsfrom bs4 import BeautifulSoupimport plotly.express as pximport pandas as pd
We use Python’s HTTP library, requests to send get request to a webpage. BeautifulSoup is a Python library for pulling data out of HTML and XML files.
plotly.express is a high-level wrapper for Plotly. It uses simpler syntax and is easy to use. pandas is a fast, powerful, flexible, and easy to use open-source data analysis and manipulation tool.
Let’s start writing code for the scraping part.
We set variables github and URL.
github = 'https://github.com'URL = 'https://github.com/CSSEGISandData/COVID-19/tree/master/csse_covid_19_data/csse_covid_19_daily_reports_us'
We get a webpage using request.get() and create a Beautifulsoup object by parsing the page content.
page = requests.get(URL)soup = BeautifulSoup(page.content, 'html.parser')
After inspecting the page HTML, you can find that the latest report is always the fourth link from the last. If you need to confirm it you can print the latest.
latest = str(soup.table.find_all('a')[-4]['href'])# print(latest)
Output:
/CSSEGISandData/COVID-19/blob/master/csse_covid_19_data/csse_covid_19_daily_reports_us/06-04-2020.csv
We create the link to the latest data, and again get the webpage content using requests.get(). We can confirm the latestURL by printing it.
latestURL = github + latestlatestPage = requests.get(latestURL, 'html.parser')print(latestURL)
Output:
https://github.com/CSSEGISandData/COVID-19/blob/master/csse_covid_19_data/csse_covid_19_daily_reports_us/06-04-2020.csv
Github has a raw page with the id of raw-url. We get the URL of the raw page URL. We can confirm it by printing the rawURL.
soup = BeautifulSoup(latestPage.content, 'html.parser')raw = str(soup.find(id='raw-url')['href'])rawURL = github + rawprint(rawURL)
Output:
https://github.com/CSSEGISandData/COVID-19/raw/master/csse_covid_19_data/csse_covid_19_daily_reports_us/06-04-2020.csv
Now the raw page is a CSV format, we can use Pandas to read it. We can confirm it by printing the data using head().
df = pd.read_csv(rawURL)print(df.head())
We drop the rows (axis=0) where the Lat value is missing. We also fill up the fields with 0 where data is missing.
df = df.dropna(axis=0, subset=['Lat'])df = df.fillna(0)print(df.head())
We pick up the date and time from the first row in the third column. (The first index is 0)
date, time = df.iloc[0, 2].split()print(date)print(time)
Output:
2020-06-0404:33:37
towardsdatascience.com
Create an account at Mapbox. And create a token.
In your JupyterLab/Jupyter Notebook, create a file .mapbox_token and copy and paste the created token from Mapbox to this file.
px.set_mapbox_access_token(open(".mapbox_token").read())
We set the Mapbox token using Plotly express set_mapbox_access_token. If you are using Git, then add .mapbox_token to your .gitignore file.
The final part of the code is to set the plotly.express parameters and layout.
The following list contains some key parameters for plotly.express.scatter_mapbox.
We use color, size, color_continuous_scale, size_max, zoom, center, hover_data, and hover_name. You can set more parameters if you wish.
fig = px.scatter_mapbox(    df, lat="Lat", lon="Long_",    color="Deaths",    size="Deaths",    color_continuous_scale=px.colors.sequential.Jet,    size_max=40,    zoom=3,     center={"lat": 37.0902, "lon": -95.7129},                  hover_data=["Confirmed"],    hover_name='Province_State')
We can set the title in fig.update_layout. And fig.show() to display the image.
fig.update_layout(    title=f'COVID-19 Deaths in USA by states.  Date: {date}',)fig.show()
All the code:
We can use this for other CSSEGISandData’s COVID-19 pages. For example, using the worldwide daily report link:
URL = 'https://github.com/CSSEGISandData/COVID-19/tree/master/csse_covid_19_data/csse_covid_19_daily_reports'
The following gif image is created using Plotly.graph_object. Can you see the difference? It does not have state names, terrain, rivers, and details as the Mapbox has. You can find the code here.
You now know how to make a bubble map using Plotly.express. The Mapbox library was used because it gives you a better presentation and you can control the map with various parameters. How about using a bubble map for your Data Science project.
Get full access to every story on Medium by becoming a member.
You can find latitude and longitude on the map from https://www.latlong.net/
JupyterLab installation
Plotly Built-in Continuous Color Scales in Python