Custom buttons
1. Custom buttons
Let's learn how to add custom buttons to our Plotly visualizations.2. What can custom buttons do?
Custom buttons in Plotly are small but powerful. They can update the data or layout elements in our plot. Remember all the work you've done so far with update_layout. This could be in a button! Buttons can also be used for animations, though animations are beyond the scope of this course.3. Custom buttons in Plotly
Custom buttons are created through the updatemenus layout element. This is a list of dictionaries -one for each button group - with several important arguments. The type sets if it is buttons or a dropdown menu. We will cover dropdowns later! The direction sets if we want buttons beside or on top of each other. The x and y arguments position the button group. showactive allows us to indicate whether the active button appears as pressed. Active here means currently selected. And finally, the buttons argument is a list of button objects.4. Plot type with buttons
Let's first set up a bar chart using our revenue data. We can use plotly express to create bars per industry. Each sample is stacked with a small gap, so while it's not a single aggregate bar, the total height still shows the total revenue per industry.5. Button set up
Now, let's create the buttons to switch the plot type. This is a list of two dictionaries - one for each button. Each has a label for the button text. The method is set to "update" so the button can modify the layout or data. The args option is a dictionary of what should be updated. In our case, the first button updates the plot to a bar chart. The second switches to a scatter plot and sets the mode to 'markers' (rather than lines).6. The args argument
The args argument can be one of the most confusing parts of Plotly. It is a list of several dictionaries. The first dictionary sets the arguments and values to send to the data element of the figure trace, and the second dictionary contains the arguments to send to the figure's layout element. To understand this better, we can inspect the structure of a figure object using Python's dir() function. We see some familiar faces, like the layout and data elements and our familiar show method.7. Using args for layout updates
Let's go further to see inside the layout element. Again, we use Python's helpful dir function. Some of these are familiar. We can use args to update any of these.8. Using args for data updates
Let's also see inside the first data element. Remember, there is a data element for each trace. There are also some familiar arguments. We haven't used 'visible' before, but it will be helpful later!9. Button interactivity
Back to our example - let's place and configure the buttons. We use update_layout() and set the updatemenus argument, with type as "buttons". We set direction to "down" so the buttons stack vertically and position them using x and y to the right of the plot, about halfway up. We make the bar chart button active by default and set showactive to True, so it appears pressed. Otherwise, it might confuse users. Then we attach our buttons list and show the plot. Nice! Now, we can toggle between plot types with the click of a button.10. Let's practice!
Let's practice creating and styling buttons in 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.