Interacting with the Plots
Plotly provides various tools for interacting with the plots such as adding dropdowns, buttons, sliders, etc. These can be created using the update menu attribute of the plot layout. Let’s see how to do all such things in detail.
Creating Dropdown Menu in Plotly
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 update menu method.
- restyle: modify data or data attributes
- relayout: modify layout attributes
- update: modify data and layout attributes
- animate: start or pause an animation
Example:
Python3
import plotly.graph_objects as px import numpy as np # creating random data through randomint # function of numpy.random np.random.seed( 42 ) # Data to be Plotted 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' ,) ]) # Add dropdown 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:
In the above example we have created two graphs for the same data. These plots are accessible using the dropdown menu.
Plotly tutorial
Plotly library in Python is an open-source library that can be used for data visualization and understanding data simply and easily. Plotly supports various types of plots like line charts, scatter plots, histograms, box plots, etc. So you all must be wondering why Plotly is over other visualization tools or libraries. So here are some reasons :
- Plotly has hover tool capabilities that allow us to detect any outliers or anomalies in a large number of data points.
- It is visually attractive and can be accepted by a wide range of audiences.
- Plotly generally allows us endless customization of our graphs and makes our plot more meaningful and understandable for others.
This tutorial aims at providing you the insight about Plotly with the help of the huge dataset explaining the Plotly from basics to advance and covering all the popularly used charts.
Table of Content
- How to install Plotly?
- Package Structure of Plotly
- Getting Started
- Creating Different Types of Charts
- Heatmaps
- Error Bars
- 3D Line Plots
- 3D Scatter Plot Plotly
- 3D Surface Plots
- Interacting with the Plots
- Adding Buttons to the Plot
- Creating Sliders and Selectors to the Plot
- More Plots using Plotly