A Plotly is a Python library that is used to design graphs, especially interactive graphs. It can plot various graphs and charts like histogram, barplot, boxplot, spreadplot, and many more. It is mainly used in data analysis as well as financial analysis. plotly is an interactive visualization library.
Creating Dropdown Menus
A drop-down menu is a part of the menu-button which is displayed on a screen all the time. Every menu button is associated with a Menu widget that can display the choices for that menu button when clicked on it. In plotly, there are 4 possible methods to modify the charts by using updatemenu method.
- restyle: modify data or data attributes
- relayout: modify layout attributes
- update: modify data and layout attributes
- animate: start or pause an animation
Example 1: Restyle Dropdown
The data and data attributes of the graph can be modified.
Python3
import plotly.graph_objects as px
import numpy as np
np.random.seed( 42 )
random_x = np.random.randint( 1 , 101 , 100 )
random_y = np.random.randint( 1 , 101 , 100 )
plot = px.Figure(data = [px.Scatter(
x = random_x,
y = random_y,
mode = 'markers' ,)
])
plot.update_layout(
updatemenus = [
dict (
buttons = list ([
dict (
args = [ "type" , "scatter" ],
label = "Scatter Plot" ,
method = "restyle"
),
dict (
args = [ "type" , "bar" ],
label = "Bar Chart" ,
method = "restyle"
)
]),
direction = "down" ,
),
]
)
plot.show()
|
Output:
Example 2:
Python3
import plotly.graph_objects as px
import numpy
np.random.seed( 42 )
random_x = np.random.randint( 1 , 101 , 100 )
random_y = np.random.randint( 1 , 101 , 100 )
x = [ 'A' , 'B' , 'C' , 'D' ]
plot = px.Figure(data = [go.Bar(
name = 'Data 1' ,
x = x,
y = [ 100 , 200 , 500 , 673 ]
),
go.Bar(
name = 'Data 2' ,
x = x,
y = [ 56 , 123 , 982 , 213 ]
)
])
plot.update_layout(
updatemenus = [
dict (
active = 0 ,
buttons = list ([
dict (label = "Both" ,
method = "update" ,
args = [{ "visible" : [ True , True ]},
{ "title" : "Both" }]),
dict (label = "Data 1" ,
method = "update" ,
args = [{ "visible" : [ True , False ]},
{ "title" : "Data 1" ,
}]),
dict (label = "Data 2" ,
method = "update" ,
args = [{ "visible" : [ False , True ]},
{ "title" : "Data 2" ,
}]),
]),
)
])
plot.show()
|
Output:
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
22 Sep, 2020
Like Article
Save Article