我正在尝试修改 Plotly-Dash Choropleth (Mapbox) 图的标准选择行为。
- 默认行为是“经典”的 Shift + 选择进行多选:按住 Shift 并单击/套索选择,每个新选择都会添加到选择池中。当不再按下 Shift 键时,任何后续选择都将重置池。
问题:我想要一种更持久的选择行为,其中每个选择都会添加到池中(不移位),然后使用重置按钮来重置选择和清空池。这可以用plotly-dash 实现吗?
我检查了文档和layout.clickmode
似乎没有这样的选择。我是否必须为每次点击不断更新图表还是有更好的方法?
这里有一个小的 MWE:
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State
import plotly.express as px
import geopandas as gpd
print('Loading data...')
gdf = gpd.read_file('https://gist.githubusercontent.com/Tlaloc-Es/5c82834e5e4a9019a91123cb11f598c0/raw/709ce9126861ef7a7c7cc4afd6216a6750d4bbe1/mexico.geojson')
gdf = gdf.to_crs(epsg=4326)
print('Done!')
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
app.layout = html.Div([
html.Div([
dcc.RadioItems(
id='radio-color_on',
options=[{'label': i, 'value': i} for i in ['AREA','PERIMETER']],
value='AREA',
labelStyle={'display': 'inline-block'}
),
],style={'width': '40%', 'display': 'inline-block',}),
html.Div([], style={'width':'100%'}),
html.Div([
dcc.Graph(id="fig")
],style={'width': '60%', 'display': 'inline-block', 'padding': '0 10',},),
])
@app.callback(
Output("fig", "figure"),
[Input("radio-color_on", "value")])
def draw_choropleth(color_on):
fig = px.choropleth_mapbox(gdf,
geojson=gdf.geometry,
locations=gdf.index,
color=color_on,
color_continuous_scale="Viridis",
range_color=(0, 12),
mapbox_style="open-street-map",
zoom=3,
center = {"lat":gdf.centroid.y.mean(), "lon":gdf.centroid.x.mean()},
opacity=0.5,
)
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0},
clickmode='event+select',
height=700,
)
#fig.update_geos(fitbounds="locations", visible=False)
return fig
if __name__ == '__main__':
app.run_server(debug=True)
生成的仪表板应用程序 GIF 显示了它的外观以及选择行为。起初,我按住shift,当释放并选择时,之前的选择会重置。