- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Histograms in Plotly using graph_objects class
Histograms are the graphical representations of the dataset distribution and can be created using Plotly , a Python library with a class known as graph_objects, using which we can create the histogram. Histograms are useful for understanding the shape of the dataset which also includes outliers, central tendency, and spread.
Plotly is a Python library that allows us to create interactive visualizations in various formats, including scatter plots, line charts, and histograms. The class graph_objects provides a high-level interface for creating some complex charts and allows us to customize every aspect of the plot. In this article, we will be exploring how to create histograms using the PLotly graph_objects class.
plotly.graph_object class
The graph_objects class is a sub-library of Plotly that allows users to create and modify visualizations using a more object-oriented approach. With graph_objects, users can create a Figure object and add various chart types to it as Traces. For example, to create a histogram using graph_objects, we can create a Figure object and add a Histogram trace to it.
The class called graph_objects is typically imported as go and contains an automatically- generated hierarchy of Python classes that represents non-leaf nodes in the figure schema. The term “graph_objects” refers to instances of these classes.
Syntax
plotly.graph_objects.Table(arg=None, cells=None, columnorder=None, columnordersrc=None, columnwidth=None, columnwidthsrc=None, customdata=None, customdatasrc=None, domain=None, header=None, hoverinfo=None, hoverinfosrc=None, hoverlabel=None, ids=None, idssrc=None, legendgrouptitle=None, legendrank=None, legendwidth=None, meta=None, metasrc=None, name=None, stream=None, uid=None, uirevision=None, visible=None, **kwargs)
Histogram in Plotly using graph_objects class
Creating histogram using graph_objects (by generating random data)
Follow the steps given below to create a histogram in Plotly using the graph_objects class by creating random data created using numpy −
Import the plotly.graph_objects module, which contains the Histogram class which we will use to create our plot, as well as the Figure class.
Generate some random data using the numpy.random.normal function, which generates random numbers from a normal distribution.
Create our histogram using the Histogram class and pass in our random data as the x parameter. This creates a histogram with the default settings, which we can customize later.
Use the update_layout method of the Figure class to add axis labels and a title to our plot.
Specify the title with the title parameter and the x and y-axis labels with the xaxis_title and yaxis_title parameters.
Use the show method of the Figure class to display our plot in a new window.
Example
import plotly.graph_objects as go import numpy as np # Generate some random data np.random.seed(123) x = np.random.normal(size=1000) # Create the histogram using graph_objects fig = go.Figure(data=[go.Histogram(x=x)]) # Add axis labels and title fig.update_layout( title="Histogram of Random Data", xaxis_title="Value", yaxis_title="Count", ) # Show the plot fig.show()
Output
Creating histogram using graph_objects (inbuilt dataset)
Follow the steps given below to create a histogram using graph_objects class with the tips dataset that is present in the Seaborn library of Python −
Import the necessary libraries, including the plotly.graph_objects module and seaborn module.
Load the built-in tips dataset using the load_dataset('tips') function.
Create four different types of histograms using the go.Histogram() function.
The first histogram is a basic histogram object using the Total Bill column.
Second histogram is the cumulative histogram object using the Tip column with the cumulative_enabled parameter set to True.
Third histogram is the normalized histogram object using the Size column with the histnorm parameter set to 'probability'.
Fourth histogram is the stacked histogram object using the Tip and Size columns with different marker colors and a legend name for each column.
Create a figure object using the go.Figure() function and add all four histograms to it.
The layout of the figure is customized using the update_layout() method to add a title and axis labels.
Display the figure using the show() method.
Example
import seaborn as s import plotly.graph_objects as go # Load the built-in tips dataset d= s.load_dataset('tips') # Create a basic histogram object using the Total Bill column hist1 = go.Histogram(x=d['total_bill'], nbinsx=30) # Create a cumulative histogram object using the Tip column hist2 = go.Histogram(x=d['tip'], nbinsx=20, cumulative_enabled=True) # Create a normalized histogram object using the Size column hist3 = go.Histogram(x=d['size'], nbinsx=5, histnorm='probability density') # Create a stacked histogram object using the Tip and Size columns hist4 = go.Histogram(x=d['tip'], y=d['size'], nbinsx=10, nbinsy=5, histfunc='sum', histnorm='probability') # Create a figure object and add all histograms to it tfig = go.Figure(data=[hist1, hist2, hist3, hist4]) # Customize the layout of the figure tfig.update_layout( title='Histograms of Tips Dataset', xaxis_title='Values', yaxis_title='Frequency', barmode='overlay', bargap=0.1, bargroupgap=0.1 ) # Display the figure tfig.show()
Output
Basic histogram object using the Total Bill column −
A cumulative histogram object using the Tip column
A normalized histogram object using the Size column −
A stacked histogram object using the Tip and Size columns −
Conclusion
In conclusion, the graph_objects class in Plotly provides us with an optimized and powerful tool for creating visualizations that are customizable, including histograms. We know that with graph_objects, we can easily generate different types of histograms (basic, normalized, cumulative, stacked) using built-in datasets or we can even on the dataset that is created by the users, and customize the number of bins, legends, colors, and other parameters according to their needs. The resulting visualizations are interactive, allowing users to zoom, pan, and hover over data points for more information.
To Continue Learning Please Login
Login with Google