如何摆脱Choropleth的白色背景

如何摆脱Choropleth的白色背景

本文介绍了情节:如何摆脱Choropleth的白色背景? (情节仪表板)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Potly 仪表板来构建仪表板。我使用的是黑色引导主题,因此我不想使用白色背景。

I am building a dashboard using Potly Dashboard. I am using a dark bootstrap theme therefore I don't want a white background.

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

However, my map now looks like this:

生成该代码的代码如下所示:

And the code that produced it is shown below:

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 ,但无法使其正常工作。

I have tried paper_bgcolor, and plot_bgcolor but couldn't make it work.

理想情况下,我想实现此目的图像外观(请忽略红点):

Ideally I would like to achieve how this image looks (please ignore the red dots):

推荐答案

通常:

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)’会给湖泊提供与各州相同的颜色,而不是bakground。因此,我将使用 lakecolor ='#4E5D6C'。当然,您可以使用 bgcolor 做同样的事情,但是将其设置为'rgba(0,0,0,0)'获得您专门要求的白色的 rid

And you might want to change the color of the lakes too. But do note that setting lakecolor = 'rgba(0,0,0,0)' will give the lakes the same color as the states and not the bakground. So I'd go with lakecolor='#4E5D6C'. You could of course do the same thing with bgcolor, but setting it to 'rgba(0,0,0,0)' gets rid of the white color which you specifically asked for.

湖泊颜色图:

湖色代码:

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 在这种情况下。为了更好地匹配您所需的最终结果,我们还可以为地色加香料:

And we could just as well change the state border colors, or what is more cryptically known as subunitcolor in this context. And to better match your desired endresult we could spice up the landcolor as well:

州边界和州色,绘制:

状态边框和状态颜色,代码:

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

这篇关于情节:如何摆脱Choropleth的白色背景? (情节仪表板)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 04:08