Dendrobates
Dendrobates

Reputation: 3534

python plotly express mapbox: configure custom 45 degree angle on map

I use Plotly.express choropleth_mapbox to visualize suburbs in a given town. I set both center and zoom to specify longitude and latitude centre + boundaries of the map. I like the fact that I can also change the "angle" (pitch) of the map in the rendered visualization, for example a 45 degrees angle, looking from the south-west (bearing) on the map. Is there a way to load the map with the "angle" (direction, height, angle) prespecified? See images below for clarification.

My code looks like:

import plotly.express as px
import pandas as pd
import json

with open("geojson_suburbs.json") as response:
    suburbs = json.load(response)

""" geojson_suburbs.json:
{
    "type": "FeatureCollection", 
    "features": [
        {"type": "Feature", "properties": {"suburb": "a"}, "geometry": {"type": "Polygon", "coordinates": [[[4.7582, 52.2536], [4.7581, 52.2513], [4.7582, 52.2536]]]}, "id": "a"},
        {"type": "Feature", "properties": {"suburb": "b"}, "geometry": {"type": "Polygon", "coordinates": [[[4.7582, 52.2536], [4.7666, 52.2544], [4.7582, 52.2536]]]}, "id": "b"}
    ]
}
"""


df = pd.DataFrame(data={'suburb': ['a', 'b'], 'phase': ['d', 'e']})

mapbox_token='pk.eyJxxx'
px.set_mapbox_access_token(mapbox_token)

fig = px.choropleth_mapbox(
    df, 
    geojson=suburbs, 
    locations='suburb',
    color='phase',
    color_continuous_scale="Viridis",
    range_color=(0, 2),
    zoom=14, 
    center = {"lat": 52.2540, "lon": 4.7641},
    opacity=0.2)

fig.show()

My current view looks like this: enter image description here

And I would like to render the map with a custom "angle", like this: enter image description here

Upvotes: 0

Views: 803

Answers (1)

r-beginners
r-beginners

Reputation: 35205

The display angle of the map can be set using the following information. Perhaps the geojson file was not set up properly, but the regions are not colored.

fig.update_mapboxes(pitch=45)

enter image description here

Upvotes: 1

Related Questions