Lesson

Geographic maps

Learn Geographic maps in SQLPad's Data Science in Action course with practical examples and guided lessons.

In this lesson, we will explore how to create beautiful and interactive geographic maps using Plotly. Geographic maps are an essential tool for data scientists, as they allow us to visualize and analyze spatial data effectively. We will cover different types of maps available in Plotly, such as choropleth maps, scatter plots on maps, and density heatmaps. You will also learn how to customize the appearance of the maps to enhance their utility and visual appeal.

To follow along with the code examples in this lesson, you'll need to have a basic understanding of Python, Pandas, and Plotly. Let's dive in and start creating some amazing geographic visualizations!

Basic Choropleth Map

In this lesson, we will create a basic choropleth map using the Plotly library and a built-in dataset from pandas. We will break down the code into two code blocks: one for constructing the pandas dataframe and another for creating the choropleth map using Plotly.

For this example, we will use the built-in "gapminder" dataset from the Plotly Express library. This dataset contains information about countries' life expectancy, GDP per capita, and population for different years.

import plotly.express as px

# Load the gapminder dataset
df = px.data.gapminder()

# Filter the dataset to visualize data for the year 2007
df = df[df['year'] == 2007]

# Display the first five rows of the dataframe
print(df.head())

Now, we will use the Plotly Express library to create a choropleth map based on the life expectancy of countries in the year 2007.

import plotly.express as px

# Create a choropleth map using the filtered gapminder dataset
fig = px.choropleth(df, locations='iso_alpha',
                    color='lifeExp',
                    hover_name='country',
                    color_continuous_scale=px.colors.sequential.Plasma,
                    title='Life Expectancy by Country in 2007')

# Show the choropleth map
fig.show()

Choropleth Map with Custom Color Scale

In this code example, we will create a Choropleth Map with a custom color scale using Plotly and a built-in dataset from Plotly.

First, let's load the required libraries and the dataset, then print the first 5 rows of the data:

import plotly.express as px

# Load the built-in Plotly dataset on Gapminder
df = px.data.gapminder()

# Filter the dataset to a specific year (e.g., 2007)
df = df[df['year'] == 2007]

# Display the first 5 rows of the filtered dataset
print(df.head())

Now, let's create a Choropleth Map with a custom color scale:

# Create a Choropleth Map with custom color scale
fig = px.choropleth(df, 
                    locations="iso_alpha", # Use country ISO codes as locations
                    color="gdpPercap", # Use GDP per capita as the color scale
                    hover_name="country", # Display country name on hover
                    projection="natural earth", # Set map projection style
                    color_continuous_scale=px.colors.sequential.Plasma, # Set custom color scale
                    title="GDP per Capita (2007)") # Set title of the map

# Show the figure
fig.show()

In this example, we've used the built-in Plotly dataset on Gapminder, filtered it to show data for the year 2007, and created a Choropleth Map with a custom color scale (Plasma) representing the GDP per capita for each country.

Density Map Box Plots on top of a base map

Here is an example where we lay the US mass shootings data on top of the US map.

The script generates an interactive scatter plot on a geographical map, where each point represents a mass shooting in the US.

The size and color of each point provide information about the number of victims and fatalities, respectively, and you can view additional details by hovering over each point.

First, let's import the necessary libraries and load the built-in dataset:

import pandas as pd
import plotly.express as px
import pyodide.http

# Load the data
data = pd.read_csv(pyodide.http.open_url("https://skillsai.s3.us-west-2.amazonaws.com/media/analytics/user_leonwei/e4e68c9c-000b-484a-804c-86734828f5e1/us_mass_shootings_bqtysQU.csv"))

# Remove missing latitude or longitude
data = data[data['latitude'].apply(lambda x:  x!='-')]
data = data[data['longitude'].apply(lambda x:  x!='-')]

data['latitude'] = pd.to_numeric(data['latitude'])
data['longitude'] = pd.to_numeric(data['longitude'])
data


Now, let's create a scatter plot on the geographic map using the dataset:

# Create a density map plot on a map
fig = px.density_mapbox(data, lat="latitude", lon="longitude", hover_name="case", 
                        hover_data=["location", "fatalities", "injured", "total_victims"],
                        zoom=3, height=900)

# Update the layout to use a custom raster source
fig.update_layout(
    mapbox_style="white-bg",
    mapbox_layers=[
        {
            "below": 'traces',
            "sourcetype": "raster",
            "sourceattribution": "United States Geological Survey",
            "source": [
                "https://basemap.nationalmap.gov/arcgis/rest/services/USGSImageryOnly/MapServer/tile/{z}/{y}/{x}"
            ]
        }
    ])

# Remove margins
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})

fig.show()

In this example, we have covered how to create density map plot. This powerful visualization tools can help you gain insights into your geospatial data and create engaging, interactive maps for your projects.

Customizing Map Appearance

In this code example, we will customize the appearance of a geographic map using the Plotly library. We will use the built-in dataset 'gapminder' from Plotly and create a choropleth map with custom colors and layout.

First, let's import the necessary libraries and load the 'gapminder' dataset.

import plotly.express as px

# Load the built-in gapminder dataset
df = px.data.gapminder()

print(df.head())

Now, let's create a choropleth map and customize its appearance by defining colors, title, and layout.

# Create a choropleth map with custom colors and layout
fig = px.choropleth(df, locations='iso_alpha', color='gdpPercap',
                    hover_name='country', animation_frame='year',
                    color_continuous_scale=px.colors.sequential.Plasma,
                    projection='natural earth')

# Customize the map appearance
fig.update_layout(
    title={
        'text': "GDP per Capita (1952-2007)",
        'y': 0.95,
        'x': 0.5,
        'xanchor': 'center',
        'yanchor': 'top'},
    geo=dict(
        showframe=False,
        showcoastlines=False,
        projection_type='equirectangular'
    ),
    annotations=[dict(
        x=0.55,
        y=0.1,
        xref='paper',
        yref='paper',
        text='Source: Gapminder',
        showarrow=False
    )]
)

fig.show()

In this example, we customized the color scale, title, and layout of the map. You can further customize the map appearance according to your preferences using the available options in the Plotly library.

Exercises

1. Geographic Maps with Plotly

Instruction

In this exercise, you will create a scatter plot on a map using the gapminder dataset. Follow these steps:

  1. Import the plotly.express library as px.
  2. Load the gapminder dataset for the year 2007 using the px.data.gapminder().query() function.
  3. Create a scatter plot on a map using the px.scatter_geo() function with the following parameters:
  4. locations set to 'iso_alpha'
  5. color set to 'continent'
  6. hover_name set to 'country'
  7. size set to 'pop'
  8. projection set to 'natural earth'
  9. Display the plot using the fig.show() method.

My Solution

# Your solution goes here

Hint

Start by importing the plotly.express library as px. Then, load the gapminder dataset for the year 2007 using the px.data.gapminder().query() function. Finally, create the scatter plot on a map using the px.scatter_geo() function with the specified parameters and display the plot using the fig.show() method.

Solution

import plotly.express as px

data = px.data.gapminder().query("year == 2007")

fig = px.scatter_geo(data, 
                     locations="iso_alpha",
                     color="continent",
                     hover_name="country",
                     size="pop",
                     projection="natural earth")
fig.show()