python - 如何摆脱 Choropleth 的白色背景?
问题描述
我正在使用 Potly Dashboard 构建仪表板。我使用的是深色引导主题,因此我不想要白色背景。
但是,我的地图现在看起来像这样:
生成它的代码如下所示:
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_bgcolor
,plot_bgcolor
但无法使其工作。
解决方案
一般来说:
fig.update_layout(geo=dict(bgcolor= 'rgba(0,0,0,0)'))
在您的具体示例中:
go.Layout(geo=dict(bgcolor= 'rgba(0,0,0,0)')
阴谋:
代码:
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)'
你特别要求的白色。
湖色图:
湖色代码:
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
我们也可以更改状态边界颜色,或者在这种情况下更神秘地称为。为了更好地匹配您想要的最终结果,我们还可以为土地颜色增添趣味:
国家边界和国家颜色,情节:
状态边界和状态颜色,代码:
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 - django 了解与主模型关联的子模型
- python - Django Rest Framework - 如何处理带有订单字段的项目更新
- c# - 在控制器 Web API 中创建自定义错误 401 响应
- python - 使用 SWIG 围绕 C++ 的 Python 包装器。参数类型无法识别
- react-native - React Native SearchBar Invariant Violation 错误'预期的功能,而不是对象的值'
- c# - 从 API 获取 json 对象,使用 C# 修改并发布它
- firebase - 如何解决〜在 null 上调用了 getter 'documentID'。〜?
- angular - Angular 6 与 Angular Material、sweetalert 和 webpack
- android - Firebase 之外的推送通知和电话身份验证服务
- amazon-web-services - Terraform for_each 变量以创建多个 AWS 侦听器规则并设置唯一的 target_group_arn