Not able to change map background color

fig = px.choropleth(
    df,
    geojson=geo_json,
    featureidkey="properties.State",  # Match this with the key in your GeoJSON properties
    locations="State",
    color="Transaction_amount",
    projection="mercator",
    width=900,  # Set the desired width
    height=700   # Set the desired height
    )

# Update the map to fit the GeoJSON data
fig.update_geos(fitbounds="locations", visible=False)

# Set background color to light grey
plot_color='#1F1F1F'
fig.update_layout(
    paper_bgcolor=plot_color,  # Light grey
    plot_bgcolor=plot_color    # Light grey
    )

# Show the map
fig.show()

Result:

hi @Mac
:wave: welcome to the community.

The paper_bgcolor will change the color of the outside background
The plot_bgcolor will change the background color of the plotting area in-between x and y axes, so this won’t work on maps.

Color change on scatter plot:

import plotly.express as px
fig = px.scatter(x=[0, 1, 2, 3, 4], y=[0, 1, 4, 9, 16])
fig.update_layout(
    paper_bgcolor='black',  # Change the outside background color
    plot_bgcolor='pink'     # Change the plot area background color
)

fig.show()

1 Like

Thank you Adam, Is there any other options to change background color of the map.

@Mac A map is not referenced to a cartesian system, with axes x, and y, that’s why paper_bgcolor does not work. Map are rendered in geographical coordinates.

To set your desired background color set the bgcolor in layout geo:

fig.update_geos(fitbounds='locations', visible=False, bgcolor='#1F1F1F')

and delete the lines corresponding to fig.update_layout.