Sliders
1. Sliders
Let's learn how to add sliders to our Plotly visualizations.2. What are sliders?
Sliders are interactive elements that allow users to update a plot. Commonly, it is used to view 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 provides simple slider functionality using the animation_frame and animation_group arguments. animation_frame sets what values will appear on the slider - like "Year" or "Island". Plotly Express assumes we're animating the same items over time, not switching datasets. That's where animation_group comes in - it identifies which samples stay consistent across frames.4. Revenue vs. Employees with slider
Let's create an example using our revenue data for the years 2018 to 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 set the animation_group to be a company, as it has been 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 with the play and stop buttons for the animation. You can leave them in if you wish. Nice work!5. Limitation: animate method
Plotly Express implements sliders using its animation method. If we inspect our figure object, we'll see how it uses frames to animate the same data points over time. This works well when we have consistent categories - like countries or penguins - changing over time. But when our data doesn't work with this animation model, we'll need to build the slider manually using traces.6. Our plan
Our plan is as follows: We'll create a figure with the necessary traces, then define a sliders object that will effectively show or hide traces, and finally, update the layout to add the slider to the figure.7. Creating the figure
First, we create a blank figure. Then, we loop through the DataFrame, subsetting by island. We add one trace per subset, setting the appropriate variables.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 is steps, where each step corresponds to one slider position. Each step is a dictionary with method set to 'update', so it can update the data and layout, a label showing the island name, and an args dictionary. Inside args, we update the visible property by setting one trace to True and the rest to False. This way, only the selected trace appears. Check the documentation for even more slider formatting options.9. Adding the slider
We use update_layout() with the sliders argument to add the slider to our figure. By using trace visibility instead of animation frames, we've worked around Plotly Express's limitations. Looks great!10. Let's practice!
Let's practice adding sliders to our Plotly visualizations!Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.