我正在尝试用欧洲歌唱大赛的结果来设置欧洲国家的 map 。 我有一个按钮可以选择不同的国家(意大利、法国、葡萄牙、英国等...)
例如,如果我选择查看瑞典的结果,我想在 map 上看到其他人根据色标给出的点数。我成功做到了!
但我想将瑞典可视化,例如在 map 上以黑色显示,以便更好地看到它在哪里,以及“符号的邻里效应”。
fig3 = go.Figure(data=go.Choropleth(
locations=Euro_tr['Country_code'], # Spatial coordinates
z = Euro_tr['Italy'], # Data to be color-coded
locationmode = "ISO-3",
colorbar_title = "Points donnés",
text=Euro_tr['Country'],
))
fig3.update_layout(
title_text = 'Score Eurovision',
margin={"r":55,"t":55,"l":55,"b":55},
height=500,
geo_scope="europe" ,
)
#Make a button for each country
button=[]
for country in Euro_tr.columns[1:-1] :
dico=dict (
label=country,
method="update",
args = [{'z': [ Euro_tr[country] ] }],)
button.append(dico)
fig3.update_layout(
updatemenus=[
dict(
buttons=button,
y=0.9,
x=0,
xanchor='right',
yanchor='top',
active=0,
),
])
正如您在显示瑞典得分的示例中看到的那样,我希望瑞典采用特定颜色,独立于其他国家/地区(已得分的国家和未得分的国家)。
感谢您的帮助!
最佳答案
即使您已经使用 px.express
构建了分区统计图,您始终可以使用带有 fig.add_traces(go. Choropleth)
例如:
col_swe = 'Black'
fig.add_traces(go.Choropleth(locations=df_swe['iso_alpha'],
z = [1],
colorscale = [[0, col_swe],[1, col_swe]],
colorbar=None,
showscale = False)
)
据我所知,不可能直接为单个国家/地区定义单一颜色,这就是为什么我分配了 z=[1]
作为值和自定义比例 colorscale = [[0, col_swe],[1, col_swe]]
以确保瑞典始终以“黑色”显示。
代码:
import plotly.express as px
import plotly.graph_objects as go
df = px.data.gapminder().query("year==2007")
fig = px.choropleth(df, locations="iso_alpha",
color="lifeExp", # lifeExp is a column of gapminder
hover_name="country", # column to add to hover information
color_continuous_scale=px.colors.sequential.Plasma)
df_swe = df[df['country']=='Sweden']
col_swe = 'Black'
fig.add_traces(go.Choropleth(locations=df_swe['iso_alpha'],
z = [1],
colorscale = [[0, col_swe],[1, col_swe]],
colorbar=None,
showscale = False)
)
f = fig.full_figure_for_development(warn=False)
fig.show()
关于python - 如何在plotly choropleth map 中使用和阻止某个位置的特定颜色(python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67854046/