Dropdowns
1. Dropdowns
Let's extend our knowledge of buttons to create dropdown menus in Plotly with Python.2. What is a dropdown?
A dropdown menu is a way of providing user input to select between different options, much like the buttons you previously created. We can create as many options as we like in Plotly that update data or layout elements. Check out the right-hand example. It has four options, each triggering a different update to the chart.3. Dropdowns in Plotly
Dropdowns are created almost the same way as buttons. Let's create a figure object, then loop through our DataFrame of Sydney property prices in several suburbs across 2015 and 2020. We subset the DataFrame each time as we want to create different plots with different data subsets. In each iteration, we'll add a bar chart using that specific subset. Why add so many traces? Because dropdowns often work by showing and hiding specific traces - depending on the selected option.4. Hiding a trace
Remember what we can update in a figure's data elements? There was a lot! The visible argument to each trace sets whether it is shown or not. We could, therefore, use args to effectively switch between traces by making some visible and others not. For example, in this dropdown button, we set the args dictionary to send a list of True/False to the visible argument of each trace. Since we used a list for the args value, it will update all 3 traces, effectively only showing the first one. Neat huh!5. The dropdown object
You'll notice the dropdown object is very similar to the buttons object We have added a title to the args list as well. We are also going to alter the visible argument of our traces to show or hide depending on which is selected.6. Adding the dropdown
Adding the dropdown to the graph is very similar. We have simply changed the type to 'dropdown' and removed the button-specific 'direction' argument. Here is our dropdown in action. Looks great!7. Let's practice!
Let's practice adding dropdown menus to our 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.