Add ticks and title on color scale - plotly scattermapbox

Viewed 665

Trying to add ticks and title label in color scale. Here's what the map and color scale looks like.

enter image description here

My code below:

datad.append({

                        "type": "scattermapbox",
                        "lat": df['Lat'],
                        "lon": df['Long'],
                        "name": "Location",
                        "showlegend": False,
                        "hoverinfo": "text",
                        "mode": "markers",
                        "marker": {
                            "autocolorscale": False,
                            "showscale":True,
                            "symbol": "circle",
                            "size": 9,
                            "opacity": 0.8,
                            "color": df['yhat'],
                            "colorscale": "blues"
                            }
                     }
        )

How do I add tick labels and title to the color scale? Documentation: https://plotly.com/python/reference/#scattermapbox-customdata

1 Answers

I replaced the example in the official reference with go.Scattermapbox() and added the color bar and title. The point is that there is a color bar setting in the marker settings. Use this for the data. The main code is editing this.

import plotly.graph_objects as go
import plotly.express as px

mapbox_access_token = open("mapbox_api_key.txt").read()
df = px.data.carshare()

fig = go.Figure(go.Scattermapbox(
        lat=df['centroid_lat'],
        lon=df['centroid_lon'],
        mode='markers',
        marker=go.scattermapbox.Marker(
            autocolorscale=False,
            showscale=True,
            size=df['peak_hour'],
            opacity=0.8,
            color=df['peak_hour'],
            colorscale='blues',
            colorbar=dict(
                title='Peak_hour',
                thickness=20,
                titleside='top',
                outlinecolor='rgba(68,68,68,0)',
                ticks='outside',
                ticklen=3)                
        ),
    ))

fig.update_layout(
    autosize=False,
    height=500,
    width=1000,
    hovermode='closest',
    mapbox=dict(
        accesstoken=mapbox_access_token,
        bearing=0,
        center=dict(
            lat=45.52341668343995,
            lon=-73.5918343429080,
        ),
        pitch=0,
        zoom=10
    ),
)

fig.show()

enter image description here

Related