简体   繁体   中英

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.

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:

在此处输入图像描述

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM