I am trying to add an additional element to the legend that specifies that grey countries have no data. The map I have is currently colored as a continuous variable, but I am also open to reconfiguring it as a discrete variable, if that would make this task much simpler. Currently, it would be nice if at the bottom of the scale, there were a grey box with the label βNo dataβ next to it.
Any help would be appreciated as the only post I could find online on the topic did not have any responses!
Based on the examples in the references, I have an idea of what you need. The idea is to create a data frame of countries with NA and replace the NA data with 0.0. In addition, set an arbitrary color scale for the NA data. The legend location is intentionally placed on the left side so that it does not overlap with the color scale on the right. We also add a setting to not display the color bar for the second map, as it is not needed. The new data frame is for Russia only, so Russia is grayed out.
import plotly.express as px
import plotly.graph_objects as go
df = px.data.gapminder().query("year==2007")
df2 = df.copy()
df2.drop(df.index, inplace=True)
df2.loc[0] = ['Rossia','Europe', 2007, 0.0, None, None,'RUS',643]
#fig = go.Figure()
fig = px.choropleth(df, locations="iso_alpha",
color="lifeExp",
hover_name="country",
color_continuous_scale=px.colors.sequential.Plasma
)
fig.add_trace(go.Choropleth(
locations=df2['iso_alpha'],
z=df2['lifeExp'],
locationmode='ISO-3',
colorscale=[[0,'rgb(248,248,255)'],[1,'rgb(248,248,255)']],
name='No data',
showlegend=True)
)
fig.update_layout(legend=dict(
yanchor="top",
y=0.99,
xanchor="left",
x=-0.06
))
fig.update_traces(showscale=False)
fig.show()