Plotly:如何设置等值线图的颜色条位置?

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

我在文档中找不到任何关于控制颜色栏放置位置的内容,只是是否应该显示它以及使用什么颜色比例等。可以这样做吗?

如果有帮助,我正在使用 Dash 实现我的等值线图。

答案:

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

或:

fig.update_layout(coloraxis_colorbar_x=-0.1)

一些细节:

您没有提供任何代码,所以我将参考 example from the docs,其中颜色条沿 x 轴的位置默认为 1.2。您可以直接更改它,例如:

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

并得到:

完整代码:

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