#1 Data Analytics Program in India
₹2,499₹1,499Enroll Now
5 min read min read

Geographic Maps

Learn to create interactive maps with Plotly

Geographic Maps

Why Maps?

Maps show location-based data:

  • Sales by country
  • Population by state
  • Store locations

Scatter Map (Points on Map)

code.py
import plotly.express as px
import pandas as pd

df = pd.DataFrame({
    'City': ['New York', 'Los Angeles', 'Chicago'],
    'Lat': [40.7128, 34.0522, 41.8781],
    'Lon': [-74.0060, -118.2437, -87.6298],
    'Population': [8336817, 3979576, 2693976]
})

fig = px.scatter_geo(df, lat='Lat', lon='Lon',
                     size='Population',
                     hover_name='City')
fig.show()

USA Map

code.py
fig = px.scatter_geo(df, lat='Lat', lon='Lon',
                     scope='usa',
                     size='Population',
                     hover_name='City')
fig.show()

Choropleth Map (Colored Regions)

Color countries/states by value:

code.py
df = pd.DataFrame({
    'Country': ['USA', 'Canada', 'Mexico', 'Brazil'],
    'Code': ['USA', 'CAN', 'MEX', 'BRA'],
    'Value': [100, 80, 60, 70]
})

fig = px.choropleth(df, locations='Code',
                    color='Value',
                    hover_name='Country')
fig.show()

USA States Choropleth

code.py
df = pd.DataFrame({
    'State': ['California', 'Texas', 'Florida', 'New York'],
    'Code': ['CA', 'TX', 'FL', 'NY'],
    'Sales': [500, 400, 300, 450]
})

fig = px.choropleth(df, locations='Code',
                    locationmode='USA-states',
                    color='Sales',
                    scope='usa',
                    hover_name='State')
fig.show()

Mapbox (Street Maps)

More detailed maps:

code.py
df = pd.DataFrame({
    'City': ['New York', 'Los Angeles', 'Chicago'],
    'Lat': [40.7128, 34.0522, 41.8781],
    'Lon': [-74.0060, -118.2437, -87.6298],
    'Sales': [1000, 800, 600]
})

fig = px.scatter_mapbox(df, lat='Lat', lon='Lon',
                        size='Sales',
                        hover_name='City',
                        zoom=3,
                        mapbox_style='open-street-map')
fig.show()

Color Scale

code.py
fig = px.choropleth(df, locations='Code',
                    color='Value',
                    color_continuous_scale='Viridis')
fig.show()

Scales: 'Viridis', 'Blues', 'Reds', 'Greens', 'Plasma'

Add Title and Labels

code.py
fig = px.choropleth(df, locations='Code',
                    color='Sales',
                    locationmode='USA-states',
                    scope='usa',
                    title='Sales by State',
                    labels={'Sales': 'Total Sales'})
fig.show()

Animation Over Time

code.py
df = pd.DataFrame({
    'Country': ['USA', 'Canada', 'USA', 'Canada'],
    'Code': ['USA', 'CAN', 'USA', 'CAN'],
    'Year': [2023, 2023, 2024, 2024],
    'Value': [100, 80, 120, 90]
})

fig = px.choropleth(df, locations='Code',
                    color='Value',
                    animation_frame='Year',
                    hover_name='Country')
fig.show()

Complete Example

code.py
import plotly.express as px
import pandas as pd

# US state sales data
df = pd.DataFrame({
    'State': ['CA', 'TX', 'FL', 'NY', 'IL', 'PA', 'OH', 'GA'],
    'Sales': [500, 400, 350, 450, 250, 200, 180, 220],
    'Stores': [50, 45, 35, 40, 25, 20, 18, 22]
})

fig = px.choropleth(df, locations='State',
                    locationmode='USA-states',
                    color='Sales',
                    scope='usa',
                    color_continuous_scale='Blues',
                    hover_data=['Stores'],
                    title='Sales by State')

fig.update_layout(geo=dict(
    showlakes=True,
    lakecolor='lightblue'
))
fig.show()

Key Points

  • scatter_geo() for points on map
  • choropleth() for colored regions
  • scatter_mapbox() for street maps
  • Use scope='usa' for US only
  • Use locationmode='USA-states' for state codes
  • animation_frame for time animation

What's Next?

Learn to create animated charts with Plotly.

SkillsetMaster - AI, Web Development & Data Analytics Courses