Forcing axis range in a px.scatter_mapbox figure

Good morning everyone!
I’m a bit puzzled with this one.
Here’s my code:

# Import libraries 
import pandas as pd 
import plotly.express as px 
# Create Pandas Dataframe from Form

 df_doors = pd.read_csv('https://docs.google.com/spreadsheets/d/e/2PACX-1vQzLHhweuUH0meMW0QfQrNCbat0Rw73fEBpBgKU-LQOlSqOTVQ0xMHhquDh-gAo9SNj50xbP1bULh87/pub?gid=404140102&single=true&output=csv')

  # Create Map 
  doors_map = px.choropleth_mapbox(df_doors, 
        geojson=doors,locations='name',featureidkey='properties.name',color='occupancy',
                                    color_continuous_scale="Reds",                           
                                    mapbox_style="carto-positron",
                                    center=dict(lon=-8.674051848928324, lat=39.63074957184749),
                                    zoom=16,
                                    opacity=0.6,
                                    height=900
  )
  # Do some styling Color Axis should be turned to False for produciton. 
  doors_map.update_layout(margin={"r":15,"t":0,"l":15,"b":0},coloraxis_showscale=True)

  # Make the hoverlabel oh so pretty 
  doors_map.update_layout(
      autorange=False,
      hoverlabel=dict(
        bgcolor="#273B80",
        font_size=16,
        font_family="sans-serif"

        )
  )

  # Force Y Axis Range 
  doors_map.update_yaxes(range = [0,100])

  # Sad Face Because it doesn't seem to work 
doors_map.show()

The result is

How can I force the yaxis to always be from 0 to 100 no matter what the values on the dataframe are?

Disclaimer: The solution will be used in a non-profit NGO project. No commercial use will be made

@adamschroeder @AnnMarieW sorry to bother you, but any idea how to solve this?
Thank you!

Hi @jgomes_eu
This should be the solution: range_color=[0,100].

doors_map = px.choropleth_mapbox(df_doors, geojson=doors,locations='name', featureidkey='properties.name', color='occupancy',
                                color_continuous_scale="Reds",
                                mapbox_style="carto-positron",
                                center=dict(lon=-8.674051848928324, lat=39.63074957184749),
                                zoom=16,
                                opacity=0.6,
                                height=900,
                                range_color=[0,100]
)
1 Like

px.choropleth_mapbox() instantiates a go.Figure containing a go.Choroplethmapbox. But Choroplethmapbox isn’t referenced to a cartesian system of coordinates (xaxis, yaxis). That’s why your:

doors_map.update_yaxes(range = [0,100])

has no effect. On the horizontal axis of a choroplethmapbox are projected lon values, while on the vertical one, the lat values.
Layout settings specific for maps are set via:

door_map.update_mapboxes(...)

see: Layout.mapbox in Python
How much from lons and lats are displayed is controled by layout.mapbox.zoom.

On the other hand, what @adamschroeder suggested it does not change the yaxis range, but the zmin, zmax values for choroplethmapbox.

2 Likes

Thanks for the detailed clarification @empet .

1 Like