mdeverna
mdeverna

Reputation: 332

Plotly: How to set colorbar position for a choropleth map?

I can't find anything in the documentation about controlling where to place the colorbar, just whether or not it should be shown and with what color scale, etc. Can this be done?

If it helps, I am implementing my choropleth map with Dash.

Upvotes: 6

Views: 5015

Answers (1)

vestland
vestland

Reputation: 61114

The answer:

fig.data[0].colorbar.x=-0.1

or:

fig.update_layout(coloraxis_colorbar_x=-0.1)

Some details:

You haven't provided any code so I'll refer to an example from the docs where the position of the colorbar along the x-axis defaults to 1.2. You can change this directly through, for example:

fig.data[0].colorbar.x=-0.1

And get:

enter image description here

Complete code:

import plotly.graph_objects as go

import pandas as pd
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2011_us_ag_exports.csv')

fig = go.Figure(data=go.Choropleth(
    locations=df['code'], # Spatial coordinates
    z = df['total exports'].astype(float), # Data to be color-coded
    locationmode = 'USA-states', # set of locations match entries in `locations`
    colorscale = 'Reds',
    colorbar_title = "Millions USD",
))

fig.update_layout(
    title_text = '2011 US Agriculture Exports by State',
    geo_scope='usa', # limite map scope to USA
)

fig.data[0].colorbar.x=-0.1
fig.show()

Upvotes: 4

Related Questions