【发布时间】:2022-07-03 14:56:26
【问题描述】:
,msdnfvmnasdvfmsadnfbmnsdabvfmnasdbfmnasbdvfdmnsvb
【问题讨论】:
-
您是否有任何代码显示您到目前为止所尝试的内容?
,msdnfvmnasdvfmsadnfbmnsdabvfmnasdbfmnasbdvfdmnsvb
【问题讨论】:
由于 express 不支持下拉菜单,所以我切换到了图形对象。 go 不会自动设置 hover 数据,所以我引入了自定义数据并添加了城市名称和人口。 为了通过下拉切换地图,需要为每个按钮配置必要的地图和数据,因此我们将为每个按钮准备一个空列表(用于地图和按钮)并添加数据为每个状态提取。同时,地图的显示和不显示将与按钮绑定。请注意,下拉菜单不会滚动,因此必须调整图形高度和下拉字体大小以显示所有状态。
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
us_cities = pd.read_csv(
'https://raw.githubusercontent.com/plotly/datasets/master/us-cities-top-1k.csv'
)
traces = []
buttons = []
state_list = np.append('All_state', us_cities['State'].unique())
visible = state_list
for s in state_list:
#print(s)
if s == 'All_state':
filtered_df = us_cities.copy()
else:
filtered_df = us_cities[us_cities['State'] == s]
traces.append(go.Scattermapbox(
lat=filtered_df['lat'],
lon=filtered_df['lon'],
mode='markers',
visible=True if s == state_list[0] else False,
customdata=filtered_df,
hovertemplate='City: %{customdata[0]}<br>Population: %{customdata[2]}<extra></extra>',
marker=go.scattermapbox.Marker(
size=9,
color='fuchsia'
)
))
buttons.append(
dict(
method='update',
label=s,
args=[{'visible':list(visible==s)}],)
)
fig = go.Figure(data=traces)
fig.update_layout(
mapbox=dict(
style='open-street-map',
#accesstoken=mapbox_access_token,
bearing=0,
center=go.layout.mapbox.Center(
lat=us_cities['lat'].mean(),
lon=us_cities['lon'].mean(),
),
zoom=3
),
margin={'r':10, 't': 0, 'l': 0, 'b': 0}
)
fig.update_layout(
# autosize=False,
height=1000,
showlegend=False,
updatemenus=[
dict(
buttons=buttons,
direction='down',
x=0.05,
y=1.0,
xanchor='right',
yanchor='bottom',
font=dict(size=8)
),
]
)
fig.show()
【讨论】:
updatemenus 中包含什么。