From fac982d5922fc393584ce5ff2e3c555a394e8c2a Mon Sep 17 00:00:00 2001 From: Sylwia Mielnicka Date: Fri, 20 Mar 2020 19:32:10 +0100 Subject: [PATCH] Update sliders.md 'Simple Slider Control' section: change method to 'update', add title. Add 'Sliders in Plotly Express', 'Methods' sections. --- doc/python/sliders.md | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/doc/python/sliders.md b/doc/python/sliders.md index e6eabb70b2b..9ac951340bf 100644 --- a/doc/python/sliders.md +++ b/doc/python/sliders.md @@ -33,8 +33,8 @@ jupyter: thumbnail: thumbnail/slider2017.gif --- -#### Simple Slider Control -Sliders can now be used in Plotly to change the data displayed or style of a plot! +### Simple Slider Control +Sliders can be used in Plotly to change the data displayed or style of a plot. ```python import plotly.graph_objects as go @@ -60,10 +60,11 @@ fig.data[10].visible = True steps = [] for i in range(len(fig.data)): step = dict( - method="restyle", - args=["visible", [False] * len(fig.data)], + method="update", + args=[{"visible": [False] * len(fig.data)}, + {"title": "Slider switched to step: " + str(i)}], # layout attribute ) - step["args"][1][i] = True # Toggle i'th trace to "visible" + step["args"][0]["visible"][i] = True # Toggle i'th trace to "visible" steps.append(step) sliders = [dict( @@ -80,5 +81,27 @@ fig.update_layout( fig.show() ``` +#### Methods +The method determines which [plotly.js function](https://plot.ly/javascript/plotlyjs-function-reference/) will be used to update the chart. Plotly can use several [updatemenu](https://plot.ly/python/reference/#layout-updatemenus-items-updatemenu-buttons-items-button-method) methods to add the slider: +- `"restyle"`: modify **data** attributes +- `"relayout"`: modify **layout** attributes +- `"update"`: modify **data and layout** attributes +- `"animate"`: start or pause an animation + +### Sliders in Plotly Express +Plotly Express provide sliders, but with implicit animation. The animation can be ommited by removing `updatemenus` in the `layout`: + +```python +import plotly.express as px + +df = px.data.gapminder() +fig = px.scatter(df, x="gdpPercap", y="lifeExp", animation_frame="year", animation_group="country", + size="pop", color="continent", hover_name="country", + log_x=True, size_max=55, range_x=[100,100000], range_y=[25,90]) + +fig["layout"].pop("updatemenus") # optional, drop animation buttons +fig.show() +``` + #### Reference Check out https://plot.ly/python/reference/#layout-updatemenus for more information!