I am using the plotly.express Python library to render a map as a PNG and I can’t seem to get rid of the white “canvas” area around the map.
As you will see from my Python, I have tried just about every configuration I can find in an attempt to reduce the margin and eliminate the white space, to no avail. Could anyone please point me in the right direction?
import plotly.express as px
import pandas as pd
# FIPS codes for all 64 Colorado counties
fips_codes = [
"08001", "08003", "08005", "08007", "08009", "08011", "08013", "08014",
"08015", "08017", "08019", "08021", "08023", "08025", "08027", "08029",
"08031", "08033", "08035", "08037", "08039", "08041", "08043", "08045",
"08047", "08049", "08051", "08053", "08055", "08057", "08059", "08061",
"08063", "08065", "08067", "08069", "08071", "08073", "08075", "08077",
"08079", "08081", "08083", "08085", "08087", "08089", "08091", "08093",
"08095", "08097", "08099", "08101", "08103", "08105", "08107", "08109",
"08111", "08113", "08115", "08117", "08119", "08121", "08123", "08125"
]
# Sample county names (replace with actual names if needed)
counties = [
"Adams", "Alamosa", "Arapahoe", "Archuleta", "Baca", "Bent", "Boulder", "Broomfield",
"Chaffee", "Cheyenne", "Clear Creek", "Conejos", "Costilla", "Crowley", "Custer", "Delta",
"Denver", "Dolores", "Douglas", "Eagle", "Elbert", "El Paso", "Fremont", "Garfield",
"Gilpin", "Grand", "Gunnison", "Hinsdale", "Huerfano", "Jackson", "Jefferson", "Kiowa",
"Kit Carson", "Lake", "La Plata", "Larimer", "Las Animas", "Lincoln", "Logan", "Mesa",
"Mineral", "Moffat", "Montezuma", "Montrose", "Morgan", "Otero", "Ouray", "Park",
"Phillips", "Pitkin", "Prowers", "Pueblo", "Rio Blanco", "Rio Grande", "Routt", "Saguache",
"San Juan", "San Miguel", "Sedgwick", "Summit", "Teller", "Washington", "Weld", "Yuma"
]
values = [
10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
]
colorscale = [
"#FEE08B",
"#D2042D"
]
df = pd.DataFrame( { "FIPS": fips_codes, "County": counties, "Value": values} )
fig = px.choropleth(
df,
geojson = "https://raw.githubusercontent.com/plotly/datasets/master/geojson-counties-fips.json",
locations = "FIPS",
color = "Value",
color_continuous_scale = colorscale,
# scope = "usa",
# labels = { "Value": "My Value" },
hover_data = { "County": False, "FIPS": False, "Value": False },
hover_name = "County",
title = "Colorado Counties Choropleth Map"
)
fig.update_geos(
fitbounds = "locations",
visible = False,
projection = { "type": "mercator" },
center = { "lat": 39, "lon": -105.5 }
)
fig.update_layout(
coloraxis_showscale = False,
title = None,
# width = 1000,
# paper_bgcolor = "white",
paper_bgcolor = "rgba(0,0,0,0)",
plot_bgcolor = "rgba(0,0,0,0)",
dragmode = False,
margin = dict(l = 0, r = 0, t = 0, b = 0, pad = 0),
autosize = False,
xaxis=dict(visible=False, showgrid=False, showline=False),
yaxis=dict(visible=False, showgrid=False, showline=False),
hovermode="closest"
)
#fig.show(config = { "displayModeBar": False, "scrollZoom": False } )
fig.write_image("colorado_map.png", width = 800, height = 600, scale = 2.2, engine="kaleido")