I'm using this code to create a map of Europe and I would like France to be completely invisible in some views, I wouldn't want to delete it from the list of countries. Changing the opacity does not change the result.
Basically I would like to change the opacity of individual countries.
import pandas as pd
import plotly.express as px
import kaleido
from plotly.graph_objs import layout
df_france = pd.DataFrame({
"country": ["France"]
})
df_ukraine = pd.DataFrame({
"country": ["Ukraine"]
})
df = pd.concat([df_france, df_ukraine], ignore_index=True)
fig = px.choropleth(df, locations="country",
locationmode="country names",
color="country",
scope="europe")
fig.update_geos(
resolution=50,
fitbounds="locations",
showcountries=False,
countrycolor="#2d32aa"
)
fig.update_layout(width=1920, height=1080)
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0}, showlegend=False)
fig.update_traces(selector=dict(country="Ukraine"), marker=dict(opacity=0.7))
fig.update_traces(selector=dict(location="France"), marker=dict(opacity=0.3))
fig.update_layout(margin={"l": 0, "b": 0, "r": 0, "t": 0})
fig.update_layout(width=1935, height=1840)
# Show the plot
fig.show()
You are using the wrong selectors when calling .update_traces()
method : country
and location
are not valid properties, but locations
is (cf. Figure Reference: Choropleth Traces) :
fig.update_traces(selector=dict(locations=["Ukraine"]), marker=dict(opacity=0.7))
fig.update_traces(selector=dict(locations=["France"]), marker=dict(opacity=0.3))
If you want to completely ignore France on the map, that is, exclude it from the layout geo's fitbounds="locations"
feature, which automatically zooms the map to show just the area of interest, use the visible
property :
fig.update_traces(selector=dict(locations=["France"]), visible=False)