您可以通过Plotly在线获得所需的功能,但是在渲染第一个图形时需要加载所有数据。也许看看Plotly的Dash,它可以动态加载数据。
为了获得显示痕迹的下拉菜单,您可以执行以下操作:
- 首先创建地图,然后为每个国家/地区添加散点图,但只能通过设置
visible
属性来显示地图。 - 创建一个下拉菜单,显示所选国家/地区的散点图(通过将Boolean数组传递给
visible
) - 添加菜单项以再次显示地图
import pandas as pdimport plotlyplotly.offline.init_notebook_mode()df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2011_us_ag_exports.csv')# create the initial mapdata = [dict(type='choropleth', locations = df['pre'].astype(str), z=df['total exports'].astype(float), locationmode='USA-states', visible=True)]layout = dict(geo=dict(scope='usa', projection={'type': 'albers usa'}))# create the empty dropdown menuupdatemenus = list([dict(buttons=list()), dict(direction='down', showactive=True)])total_pres = len(df.pre.unique()) + 1for s, state in enumerate(df.pre.unique()): # add a trace for each state data.append(dict(type='scatter', x=[i for i in range(1980, 2016)], y=[i + random.random() * 100 for i in range(1980, 2016)], visible=False)) # add each state to the dropdown visible_traces = [False] * total_pres visible_traces[s + 1] = True updatemenus[0]['buttons'].append(dict(args=[{'visible': visible_traces}], label=state, method='update'))# add a dropdown entry to reset the map updatemenus[0]['buttons'].append(dict(args=[{'visible': [True] + [False] * (total_pres - 1)}], label='Map', method='update'))layout['updatemenus'] = updatemenusfig = dict(data=data, layout=layout)plotly.offline.iplot(fig)


