Skip to content

Fix bad default zoom in Plotly Express map functions - #5686

Merged
emilykl merged 4 commits into
v7.0from
fix-bad-default-zoom-5450
Jul 30, 2026
Merged

Fix bad default zoom in Plotly Express map functions#5686
emilykl merged 4 commits into
v7.0from
fix-bad-default-zoom-5450

Conversation

@emilykl

@emilykl emilykl commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

Link to issue

Closes #5450

Description of change

  • Remove hard-coded default zoom of 8 from px.scatter_map(), px.line_map(), px.density_map() and px.choropleth_map() functions
    • 8 is a very high zoom level and not appropriate for most circumstances
  • Remove Plotly Express code which computes a center based on the data. This is no longer needed now that plotly.js automatically computes zoom and center by default
    • Note: auto-fit for choropleth is not yet supported by plotly.js, but it didn't work in Plotly Express either since px.choropleth_map() has no lat or lon arguments
  • Remove zoom argument from px.scatter_map() Percy test to visually verify auto-fit behavior

Demo

The following screenshots show the plots produced without passing any center or zoom settings to the px function.

Before After
scatter-before scatter-after
line-before line-after
density-before density-after
choropleth-before choropleth-after

Code:

Details
import json
from urllib.request import urlopen

import pandas as pd
import plotly.express as px

# px.scatter_map()
df = pd.DataFrame(
    {
        "city": ["San Marino", "Chicago", "Nairobi", "Istanbul", "Trondheim"],
        "lat": [43.9360958, 41.85003, -1.28333, 41.01384, 63.43049],
        "lon": [12.4417702, -87.65005, 36.81667, 28.94966, 10.39506],
    }
)
fig = px.scatter_map(df, lat="lat", lon="lon")
fig.update_layout(
    title_text="px.scatter_map()",
    title_font_size=18,
    margin={"r": 0, "t": 35, "l": 0, "b": 0},
    width=800,
    height=400,
)
fig.show()

# px.line_map()
us_cities = pd.read_csv(
    "https://raw.githubusercontent.com/plotly/datasets/master/us-cities-top-1k.csv"
)
us_cities = us_cities.query("State in ['New York', 'Ohio']")
fig = px.line_map(us_cities, lat="lat", lon="lon", color="State")
fig.update_layout(
    title_text="px.line_map()",
    title_font_size=18,
    margin={"r": 0, "t": 35, "l": 0, "b": 0},
    width=800,
    height=400,
)
fig.show()

# px.density_map()
df = pd.read_csv(
    "https://raw.githubusercontent.com/plotly/datasets/master/earthquakes-23k.csv"
)
df = df[:10]
fig = px.density_map(
    df,
    lat="Latitude",
    lon="Longitude",
    z="Magnitude",
    radius=10,
    map_style="open-street-map",
)
fig.update_layout(
    title_text="px.density_map()",
    title_font_size=18,
    margin={"r": 0, "t": 35, "l": 0, "b": 0},
    width=800,
    height=400,
)
fig.show()

# px.choropleth_map()
with urlopen(
    "https://raw.githubusercontent.com/plotly/datasets/master/geojson-counties-fips.json"
) as response:
    counties = json.load(response)
df = pd.read_csv(
    "https://raw.githubusercontent.com/plotly/datasets/master/fips-unemp-16.csv",
    dtype={"fips": str},
)

fig = px.choropleth_map(
    df,
    geojson=counties,
    locations="fips",
    color="unemp",
    color_continuous_scale="Viridis",
    range_color=(0, 12),
    map_style="carto-positron",
    opacity=0.5,
    labels={"unemp": "unemployment rate"},
)
fig.update_layout(
    title_text="px.choropleth_map()",
    title_font_size=18,
    margin={"r": 0, "t": 35, "l": 0, "b": 0},
    width=800,
    height=400,
)
fig.show()

Testing strategy

  • Removed explicit zoom from px.scatter_map() Percy test so that any changes in scattermap fitting behavior will be captured
  • Percy tests for px.line_map() and px.density_map() already don't pass explicit center or zoom

To test locally: Run the code snippet above

Guidelines

emilykl added 2 commits July 29, 2026 18:50
…() functions (now handled in plotly.js).

Note: choropleth map auto-centering is not yet supported in plotly.js, but the default plotly.js zoom of 1 is still better
than an arbitrary zoom of 8.
@emilykl emilykl changed the title Fix bad default zoom 5450 Fix bad default zoom in Plotly Express map functions Jul 29, 2026
@emilykl
emilykl requested a review from camdecoster July 29, 2026 23:37

@camdecoster camdecoster left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Could you update the zoom docstring in plotly/express/_doc.py? It still says the default is 8.

@emilykl

emilykl commented Jul 30, 2026

Copy link
Copy Markdown
Contributor Author

Looks good. Could you update the zoom docstring in plotly/express/_doc.py? It still says the default is 8.

Good catch, will do.

@emilykl
emilykl force-pushed the fix-bad-default-zoom-5450 branch from 57a7e55 to 79d791a Compare July 30, 2026 16:33
@emilykl
emilykl merged commit 8aef5df into v7.0 Jul 30, 2026
22 of 23 checks passed
@emilykl
emilykl deleted the fix-bad-default-zoom-5450 branch July 30, 2026 16:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants