gpt4 book ai didi

python - 如何去掉 Choropleth 的白色背景?

转载 作者:行者123 更新时间:2023-11-30 21:51:45 26 4
gpt4 key购买 nike

我正在使用 Potly Dashboard 构建仪表板。我使用的是深色引导主题,因此我不需要白色背景。

但是,我的 map 现在看起来像这样:

my map

生成它的代码如下所示:

trace_map = html.Div(
[
dcc.Graph(
id = "map",
figure = go.Figure(
data=go.Choropleth(
locations=code, # Spatial coordinates
z = df.groupby(['month']).sum()['Sales'].astype(int),
locationmode = 'USA-states',
colorscale = 'Reds',
colorbar_title = "USD",
), layout = go.Layout(title = 'The Cities Sold the Most Product',
font = {"size": 9, "color":"White"},
titlefont = {"size": 15, "color":"White"},
geo_scope='usa',
margin={"r":0,"t":40,"l":0,"b":0},
paper_bgcolor='#4E5D6C',
plot_bgcolor='#4E5D6C',
)
)
)
]
)

我尝试过 paper_bgcolorplot_bgcolor 但无法使其工作。

理想情况下,我希望实现该图像的外观(请忽略红点): enter image description here

最佳答案

一般情况下:

fig.update_layout(geo=dict(bgcolor= 'rgba(0,0,0,0)'))

在您的具体示例中:

go.Layout(geo=dict(bgcolor= 'rgba(0,0,0,0)')

plotly :

enter image description here

代码:

import plotly.graph_objects as go

fig = go.Figure(
data=go.Choropleth(
#locations=code, # Spatial coordinates
#z = df.groupby(['month']).sum()['Sales'].astype(int),
locationmode = 'USA-states',
colorscale = 'Reds',
colorbar_title = "USD",
), layout = go.Layout(geo=dict(bgcolor= 'rgba(0,0,0,0)'),
title = 'The Cities Sold the Most Product',
font = {"size": 9, "color":"White"},
titlefont = {"size": 15, "color":"White"},
geo_scope='usa',
margin={"r":0,"t":40,"l":0,"b":0},
paper_bgcolor='#4E5D6C',
plot_bgcolor='#4E5D6C',
)
)

fig.show()

您可能也想改变湖泊的颜色。但请注意,设置 lakecolor = 'rgba(0,0,0,0)' 将为湖泊提供与州而不是背景相同的颜色。所以我会选择lakecolor='#4E5D6C'。您当然可以使用 bgcolor 执行相同的操作,但将其设置为 'rgba(0,0,0,0)'摆脱您特别要求的白色。

湖泊颜色图:

enter image description here

湖泊颜色代码:

import plotly.graph_objects as go

fig = go.Figure(
data=go.Choropleth(
#locations=code, # Spatial coordinates
#z = df.groupby(['month']).sum()['Sales'].astype(int),
locationmode = 'USA-states',
colorscale = 'Reds',
colorbar_title = "USD",
), layout = go.Layout(geo=dict(bgcolor= 'rgba(0,0,0,0)', lakecolor='#4E5D6C'),
title = 'The Cities Sold the Most Product',
font = {"size": 9, "color":"White"},
titlefont = {"size": 15, "color":"White"},
geo_scope='usa',
margin={"r":0,"t":40,"l":0,"b":0},
paper_bgcolor='#4E5D6C',
plot_bgcolor='#4E5D6C',
)
)

fig.show()

我们也可以更改状态边框颜色,或者在这种情况下更神秘地称为subunitcolor。为了更好地匹配您想要的最终结果,我们还可以为土地颜色增添趣味:

州边界和州颜色,绘图:

enter image description here

州边界和州颜色,代码:

import plotly.graph_objects as go

fig = go.Figure(
data=go.Choropleth(
#locations=code, # Spatial coordinates
#z = df.groupby(['month']).sum()['Sales'].astype(int),
locationmode = 'USA-states',
colorscale = 'Reds',
colorbar_title = "USD",
), layout = go.Layout(geo=dict(bgcolor= 'rgba(0,0,0,0)', lakecolor='#4E5D6C',
landcolor='rgba(51,17,0,0.2)',
subunitcolor='grey'),
title = 'The Cities Sold the Most Product',
font = {"size": 9, "color":"White"},
titlefont = {"size": 15, "color":"White"},
geo_scope='usa',
margin={"r":0,"t":40,"l":0,"b":0},
paper_bgcolor='#4E5D6C',
plot_bgcolor='#4E5D6C',
)
)

fig.show()

关于python - 如何去掉 Choropleth 的白色背景?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60056848/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com