# Create Dropdown Menu in plotly Graph in Python (Example)

Hello! Welcome again to another tutorial from Statistics Globe. This tutorial promises to be a really exciting one, because I will show you how to create dropdown filters in your plotly graphs using the Python programming language.

But before we get started, here is a quick overview of what we will do:

Looks like we are ready to dive into it!

## Install & Import plotly

You will need to download and install plotly in your Python programming environment in order to be able to use it. Therefore, in your preferred Python coding IDE, run the line of code below:

`pip install plotly`

Next, we will need to import plotly and all its dependencies into our Python environment. To do so, run the code the below:

`import plotly.graph_objects as px`

This will ensure that we have access to all of plotly’s plot-building functions.

## Build Simple plotly Plot

We are now going to build a simple scatter plot. For this demonstration, we will be using the popular numpy Python library to generate random data that we will visualize in a scatter plot. So, please install and import the numpy library, just as we did earlier for plotly:

```pip install numpy import numpy as np```

Let’s now build our scatter plot:

```np.random.seed(52)   random_x = np.random.randint(2, 50, 50) random_y = np.random.randint(2, 50, 50)   fig = px.Figure(data=[px.Scatter( x=random_x, y=random_y, mode='markers',) ])   fig.update_layout(title = "A Simple Scatter Plot")   fig.show()```

In the above code, we set a random seed so that the data and visualization are reproducible. As you can see above, it is a simple scatter plot. However, we want our plot to be more exciting, and we will do this by adding a dropdown filter.

## Add Dropdown Filter to plotly Plot

We will add a dropdown filter that will restyle the plot entirely, such that the plot can change from a scatter plot to a box plot or a bar plot. In your IDE, please run the code below to add a dropdown filter to the scatter plot:

```import plotly.graph_objects as px import numpy as np     np.random.seed(52)   random_x = np.random.randint(1, 120, 100) random_y = np.random.randint(1, 120, 100)   fig = px.Figure(data=[px.Scatter( x=random_x, y=random_y, mode='markers',) ])   fig.update_layout( updatemenus=[ dict( buttons=list([ dict( args=["type", "scatter"], label="Scatter Plot", method="restyle" ), dict( args=["type", "box"], label="Box Plot", method="restyle" ), dict( args=["type", "bar"], label="Bar Plot", method="restyle" ) ]), direction="down", ), ], title = "Dropdown Menu in plotly Graph Using Python" )   fig.show()```

The above scatter plot now includes a dropdown menu, which allows us to select other ways to visualize the data.

In the `update_layout()` function, we parsed an array of a Python dictionary to the `updatemenus = ` argument. And then there is a buttons list of dictionaries that define the kinds of visualization we would like to create, with their respective labels.

The `method = ` argument enabled us to tell plotly that we want to restyle our plot, which means we want to modify the data and its attributes, and then the `direction = ` argument allowed us to specify that we want a downwards facing list, as is typical of a dropdown menu.

With that, we have successfully added a dropdown list to our scatter plot, and have made it even more interactive. You can try to include other visualizations in your plot and make the list bigger.

## Video, Further Resources & Summary

Do you need more explanations on how to make dropdown menus in plotly graphs in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

In the video, we explain in some more detail how to make dropdown menus in plotly graphs in Python.

So, I hope you found this tutorial interesting and helpful. If true, then be sure to check out other very interesting plotly Python tutorials on Statistics Globe:

This post has shown how to make dropdown menus in plotly graphs in Python. In case you have further questions, you may leave a comment below. I will see you at the next one!

This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.

Subscribe to the Statistics Globe Newsletter