Plotly: How to get rid of the white background of Choropleth? (Plotly Dashboard)

后端 未结 1 679
南方客
南方客 2021-01-25 05:18

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 lik

1条回答
  •  鱼传尺愫
    2021-01-25 06:06

    Generally:

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

    And in your specific example:

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

    Plot:

    Code:

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

    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.

    Lake color plot:

    Lake color code:

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

    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:

    State border and state colors, plot:

    State border and state colors, code:

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

    0 讨论(0)
提交回复
热议问题