Plotting a Histogram in Python using Seaborn
In this tutorial, I will be teaching you how to plot a histogram in Python using the seaborn library.
Before we begin, make sure you have the seaborn library and matplotlib library installed on your system. If not, then go to the following link to know how:-
Line chart plotting using Seaborn in Python
What is a Histogram?
A Histogram is formally defined as a graphical display of data with the help of bars of varying heights. A histogram group numbers into ranges and are very useful in certain use cases. For example, the heights of various people can be easily represented effectively using a histogram.
Importing the required libraries
import seaborn as sns import matplotlib.pyplot as plt
This will import the seaborn library with alias ‘sns’. Similarly, for matplotlib, the alias name here is ‘plt’.
Creating an alias name will make the code look simpler and more readable.
The Dataset
You can use any dataset you want, or you can even input the values yourself. But for this tutorial, I will be using the very popular “iris” dataset. This dataset has information about flowers. Make sure you are connected to the internet while running this code as the dataset is available online.
Program: Plot a Histogram in Python using Seaborn
#Importing the libraries that are necessary import seaborn as sns import matplotlib.pyplot as plt #Loading the dataset dataset = sns.load_dataset("iris") #Creating the histogram sns.distplot(dataset['sepal_length']) #Showing the plot plt.show()
First, the sns.distplot() function loads the dateset into the variable, ‘dataset’.
Next, the sns.distplot() function creates the histogram.
Finally, the plt.show() function shows the graph
OUTPUT:
You should refer to the following articles to learn more about Seaborn and plotting graphs in Python:-
Leave a Reply