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

标签 python dictionary plotly

我正在使用 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/

相关文章:

python - 我在哪里可以找到 Python 的 hash() 函数的源代码或算法?

python - 在 Python 中,为什么没有在嵌套字典中查找键的内置函数?

javascript - 剧情 js : how to run my javascript ONLY after plot image is loaded

java - Java HashMap对象的并发访问

python-3.x - Pandas:如何使用开始和结束时间戳分析数据?

javascript - 绘图仪动态

python - 如何删除列表中仅位于特定位置的元素?

python - 如果注释掉错误,Eclipse IDE(Pydev) 中内置 max() 函数和其他内置函数会出现错误

python - Pandas:如何删除重复行,但保留所有行的最大值

c# - contains 和 equal(==) 的区别