我正在试着用他们在Eurovision上的结果来绘制一张欧洲国家的地图。我有一个按钮来选择不同的国家(意大利,法国,葡萄牙,英国,等等)
例如,如果我选择查看瑞典的结果,我希望在地图上看到其他人根据色标给出的点数。我成功地做到了!
但我想要可视化瑞典,例如地图上的黑色,以更好地了解它在哪里,以及“符号的邻里效应”。
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,
),
])正如你在这个例子中看到的给瑞典的分数,我希望瑞典是一种特定的颜色,独立于其他国家,那些给了分的国家和那些没有给分的国家。

感谢您的帮助!
发布于 2021-06-10 01:52:18
我关注了@vestland的答案,通过使用fig.add_traces(go.Choropleth),我成功地将我感兴趣的国家以一种颜色呈现给其他人
为了能够根据我感兴趣的国家/地区更改数据和跟踪,我使用streamlit和按钮。
import streamlit as st
import pandas as pd
import numpy as np
import plotly.express as px
import plotly.graph_objects as go
# Creation of graphes for each country
Graphes=[]
for country in Euro_tr.columns[1:-1] : #To pass each country
Graphe=go.Figure(data=go.Choropleth(
locations=Euro_tr['Country_code'], # Spatial coordinates
z = Euro_tr[country], # Data to be color-coded
locationmode = "ISO-3",
colorbar_title = "Points donnés",
autocolorscale= False,
colorscale="viridis",
text=Euro_tr['Country'],
))
# customisation : title according to the country and its points
Graphe.update_layout(
title_text = "Total :Points donnés à {fcountry} qui a remporté {fpoints} points".format(fcountry = country, fpoints = Eurovision_tot['Result_tot'][Eurovision_tot["Country"]==country].values[0]),
margin={"r":55,"t":55,"l":55,"b":55},
height=500,
)
)
# block a specific zoom on the map ( scope "europe" isn't complete for eurovision countries xD!)
Graphe.update_geos(
center=dict(lon= 23, lat= 54),
lataxis_range=[31.0529,-40.4296], lonaxis_range=[-24, 88.2421],
projection_scale=3
)
# add trace for the specific country.
Graphe.add_traces(go.Choropleth(locations=Country_df['Country_code'][Country_df["Country"]==country],
z = [1],
colorscale = [[0, col_swe],[1, col_swe]],
colorbar=None,
showscale = False))
Graphes.append(Graphe)
#creation selectbox to select country
col12, col22 = st.beta_columns([0.2,0.8]) # I use columns to put the selector on a side and the graph on other side
Pays=list(Euro_tr.columns[1:-1]) # List containing country's name
Selection_Pays = col12.selectbox('',(Pays)) #create a multiple selector with the different countries as possible choice
# define action according to the selection.
for country in Pays :
if Selection_Pays== country : #if country is selected
col22.plotly_chart(Graphes[Pays.index(country)]) # plot the corresponding map.

发布于 2021-06-07 14:57:57
即使你已经使用px.express构建了你的choropleth地图,你仍然可以使用plotly.graph_objects和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()https://stackoverflow.com/questions/67854046
复制相似问题