Get startedGet started for free

Sliders

1. Sliders

Let's learn how to add basic sliders to our Plotly visualizations.

2. What are sliders?

Sliders are interactive elements that allow updating a plot. Commonly, it is used for viewing something over time such as data each year. However, it can be used for any group to toggle between, such as penguin islands! Just remember to consider the user experience - that the slider makes sense in your context.

3. Sliders in plotly.express

plotly express allows simple sliders via the animation_frame and animation_group arguments animation_frame is where you specify what will be on the slider. For example, Year or Island from the previous slide. plotly express assumes you aren't updating data, but viewing the same items over multiple frames. animation_group is how to identify each sample.

4. Revenue vs. Employees with slider

Let's create an example slider using the revenue data (but with data each year 2018-2020). The first part of the code should be familiar, setting up the scatterplot of revenue versus employee count. We want the year on our slider and we set the animation_group to be company as it is the same company over multiple years. We ensure the axis ranges are set beforehand, otherwise data may not all fit. Plotly express sliders don't change layout or data elements, so the axes are static between slider elements. Finally we use python's pop method to remove the part of the layout that has play and stop buttons for the animation. You can leave them in if you wish. Nice work!

5. plotly.express limitation: animate method

plotly express sliders implement an animation slider method. Check it out in your figure object plotly express is creating a slider by animating the same data point over different frames. This could be useful if you had the same data groups (like countries or penguins!) over time but if your data isn't suitable for this method, we need to use graph_objects to create our slider

6. Sliders with graph_objects

Our plan is as follows: We'll create a figure object with necessary traces, then create a sliders object that will effectively show or hide traces, and finally update the layout to add our slider.

7. Creating the figure

We start with a blank figure. then loop through and subset the DataFrame per island We add a trace for each subset of data, setting the variables we want and ensuring the mode is markers for a scatterplot, not line plot.

8. Creating the slider

A slider object is a list of dictionaries. We will only have one element as we have one slider. The main argument being the steps. We set the method to be update so it can update the data and layout. The label is each slider element's text, our island name. The args argument contains the visible dictionary, that hides or shows traces based on the slider element. Notice that each slider element sets one trace, a different one, to be visible. See the documentation for more formatting options for sliders.

9. Adding the slider

Adding the slider to our figure is easy using update_layout and the sliders argument Nice! Although, the first screen was a bit funny. Let's fix that!

10. Fixing the initial display

We can fix the initial display by setting only the relevant traces in the figure to be visible. Since our slider visibility trick hasn't kicked in yet, we need to set the second and third traces to be hidden; since our first slider element is what is started on. This code is inserted before we add our slider and show the figure. Much better!

11. Let's practice!

Let's practice adding sliders to our Plotly visualizations!