Custom Button in plotly Graph in Python (Example)
Hello again! Welcome to another interesting plotly in Python programming tutorial. In this one, you’ll learn how to create a custom button, which will enable you to switch between different plots, in plotly graphs using Python.
But first, let take a sneak peek at the content of this tutorial:
Let’s dive into it!
Install & Import plotly
If you do not have plotly already installed in your Python environment, then in your preferred Python IDE, run the code below to download and install plotly; otherwise, you may skip to the next section:
pip install plotly
Plotly, along with its dependencies, will now be installed in your Python programming environment. Next, we will import plotly by running:
import plotly.graph_objs as go
We now have access to plotly’s plot-building functions, including the one we will use in this tutorial.
Create Custom Buttons in plotly Graph
We are now going to create custom buttons that will enable us to switch between a box plot and a violin plot. Therefore, in your IDE, please run the code below.
fig = go.Figure() fig.add_trace(go.Box(y = [900,350,650,750,350,1350,250,1000,850,690,720])) fig.layout.update( updatemenus = [ go.layout.Updatemenu( type = "buttons", direction = "left", buttons=list( [ dict(args = ["type", "box"], label = "Box", method = "restyle"), dict(args = ["type", "violin"], label = "Violin", method = "restyle") ] ), pad = {"r": 2, "t": 2}, showactive = True, x = 0.11, xanchor = "left", y = 1.1, yanchor = "top" ), ] ) fig
In the plotly graph above, you can choose to view either a box plot or a violin plot, by clicking their respective buttons.
Thanks to the go.layout.Updatemenu()
function, we specified that we wanted a button and then parsed a list of Python dictionaries to the buttons =
argument. The Python dictionaries contain information about each visualization we want to display.
There you have it! We have created custom buttons in plotly graphs using Python. There are, however, other plotly customizations that can be automated with buttons that are beyond just switching between plots. However, this is a step to creating more advanced custom buttons in plotly.
Video, Further Resources & Summary
Do you need more explanations on how to create custom buttons 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 create custom buttons in plotly graphs in Python.
The YouTube video will be added soon.
So, I hope you enjoyed this tutorial, and I will urge you to check out other interesting plotly Python tutorials on Statistics Globe:
- How to Draw a plotly Histogram in Python (Example)
- Format the Title of plotly Graph in Python (Example)
- Format Hover Text of plotly Graph in Python (Example)
- Visualize pandas DataFrame in plotly Graph in Python (2 Examples)
- Transparent plotly Graph Background in Python (Example)
- How to Draw a plotly Scatterplot in Python (Example)
- Learn Python Programming
This post has shown how to create custom buttons in plotly graphs in Python. In case you have further questions, you may leave a comment below. See you soon 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.
Statistics Globe Newsletter