Skip to content Skip to sidebar Skip to footer

How To Get Rid Of The White Background Of Choropleth?

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 tha

Solution 1:

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:

enter image description here

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:

enter image description here

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:

enter image description here

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

Solution 2:

I found my way here because I wanted to change the theme of my Choroplethmapbox. The accepted solution helped, but ultimately I found the code below worked for my situation:

instantiate figure

fig = go.Figure()

add some traces

fig.add_trace(go.Choroplethmapbox(geojson=data_for_choropleth_geojson, 
                                  locations=data_for_choropleth['fips'],
                                  z=data_for_choropleth['total_population'],
                                  featureidkey='properties.fips'
                              ))

finally, change the theme using update_layout

fig.update_layout(
    hovermode='closest',
    mapbox=dict(
                # style options: "basic", "streets", "outdoors", 
                # "dark", "satellite", or"satellite-streets","light"
                # "open-street-map", "carto-positron", 
                # "carto-darkmatter", "stamen-terrain", 
                # "stamen-toner"or"stamen-watercolor"
                style='light',
                bearing=0,
                pitch=0,
                accesstoken=TOKEN,
                zoom=5,
                center=dict(
                    lat=29.4652568,
                    lon=-98.613121
        )
)

Post a Comment for "How To Get Rid Of The White Background Of Choropleth?"