Histograms and Density Plots in Python
Prerequisites: Seaborn
The histogram is the graphical representation that organizes a group of data points into the specified range. Creating the histogram provides the Visual representation of data distribution. By using a histogram we can represent a large amount of data, and its frequency.
Density Plot is the continuous and smoothed version of the Histogram estimated from the data. It is estimated through Kernel Density Estimation.
In this method Kernel (continuous curve) is drawn at every individual data point and then all these curves are added together to make a single smoothened density estimation. Histogram fails when we want to compare the data distribution of a single variable over the multiple categories at that time Density Plot is useful for visualizing the data.
Approach:
- Import the necessary libraries.
- Create or import a dataset from seaborn library.
- Select the column for which we have to make a plot.
- For making the plot we are using distplot() function provided by seaborn library for plotting Histogram and Density Plot together in which we have to pass the dataset column.
- We can also make Histogram and Density Plot individually using distplot() function according to our needs.
- For creating Histogram individually we have to pass hist=False as a parameter in the distplot() function.
- For creating Density Plot individually we have to pass kde=False as a parameter in the distplot() function.
- Now after making the plot we have to visualize that, so for visualization, we have to use show() function provided by matplotlib.pyplot library.
For plotting the Histogram and Density Plots together we are using diamond and iris dataset provided by seaborn library.
Example 1: Importing the dataset and Print them.
Python
import seaborn as sns
df = sns.load_dataset( 'diamonds' )
df
|
Output:
Example 2: Plotting the Histogram using seaborn library on the default setting.
Python
import seaborn as sns
import matplotlib.pyplot as plt
df = sns.load_dataset( 'diamonds' )
sns.distplot(a = df.carat, kde = False )
plt.show()
|
Output:
Example 3: Plotting the Density using seaborn library on the default setting.
Python
import seaborn as sns
import matplotlib.pyplot as plt
df = sns.load_dataset( 'diamonds' )
sns.distplot(a = df.carat, hist = False )
plt.show()
|
Output:
Example 4: Plotting Histogram and Density Plot together on default settings.
Python
import seaborn as sns
import matplotlib.pyplot as plt
df = sns.load_dataset( 'diamonds' )
sns.distplot(a = df.carat)
plt.show()
|
Output:
Example 5: Plotting Histogram and Density Plot together by setting bins and color.
Python
import seaborn as sns
import matplotlib.pyplot as plt
df = sns.load_dataset( 'diamonds' )
sns.distplot(a = df.carat, bins = 40 , color = 'purple' ,
hist_kws = { "edgecolor" : 'black' })
plt.show()
|
Output:
Example 6: Plotting Histogram and Density Plot together using Iris dataset.
Python
import seaborn as sns
import matplotlib.pyplot as plt
df2 = sns.load_dataset( 'iris' )
sns.distplot(a = df2.petal_length, color = 'green' ,
hist_kws = { "edgecolor" : 'black' })
plt.show()
|
Output:
We can also print the iris dataset by adding one line of code i.e, print(df2), and dataset looks like.
Example 7: Plotting Histogram and Density Plot together on sepal length.
Python
import seaborn as sns
import matplotlib.pyplot as plt
df2 = sns.load_dataset( 'iris' )
sns.distplot(a = df2.sepal_width, color = 'red' ,
hist_kws = { "edgecolor" : 'white' })
plt.show()
|
Output:
In this way, we can plot Histogram and Density Plot together on any dataset columns according to our needs.
Last Updated :
31 Jul, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...