Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Bin Size in Matplotlib Histogram
Next article icon

Histograms are used to represent the frequencies across various intervals in a dataset. In this article, we will learn how to create overlapping histograms in Python using theMatplotlib library. Thematplotlib.pyplot.hist()function will be used to plot these histograms so that we can compare different sets of data on the same chart. This makes it easy to spot patterns and differences in data.

Step 1: Imporing the libraries

We will useMatplotlibfor plotting graphs andSeabornfor loading datasets and creating visualizations.

Python
importmatplotlib.pyplotaspltimportseabornassns

Step 2: Loading dataset

We will be using theIris dataset which contains measurements of sepal length, sepal width, petal length and petal width for three different species of Iris flowers.

Python
data=sns.load_dataset('iris')print(data.head(5))

Output:

1
Iris Dataset

Step 3: Ploting Histograms

We will plot histogram for sepal_length and petal_length.

Python
plt.hist(data['petal_length'],label='petal_length')plt.hist(data['sepal_length'],label='sepal_length')plt.legend(loc='upper right')plt.title('Overlapping')plt.show()

Output:

2
Overlapping Histogram

Here, we can see that some part of the histogram for petal_length has been hidden behind the histogram for sepal_length. To properly visualize both the histograms we need to set the transparency parameter i.ealpha to a suitable value. So let's check various values for alpha and find out suitable one.

Step 4: Setting Transparency

We will set alpha=0.5 for both sepal_length and petal_length.

Python
plt.hist(data['petal_length'],alpha=0.5,label='petal_length')plt.hist(data['sepal_length'],alpha=0.5,label='sepal_length')plt.legend(loc='upper right')plt.title('Overlapping with both alpha=0.5')plt.show()

Output:

3
Histogram with alpha = 0.5

After setting our alpha value to 0.5 we are able to properly see the histograms for both our values even though there is overlapping between them. Let us try to make further changes to our alpha and see its impact on our visualization.

Step 5: Setting Different Alpha Valuse

We will setalpha=0.1 for sepal_length and 0.9 for petal_length

Python
plt.hist(data['petal_length'],alpha=0.9,label='petal_length')plt.hist(data['sepal_length'],alpha=0.1,label='sepal_length')plt.legend(loc='upper right')plt.title('Overlapping with alpha=0.1 and 0.9 for sepal and petal')plt.show()

Output:

4
Histogram with a Different Alpha

Step 6: Create more than 2 overlapping histograms with customized colors.

Now, let us plot more than two overlapping histograms where we need custom colors.

Python
plt.hist(data['sepal_width'],alpha=0.5,label='sepal_width',color='red')plt.hist(data['petal_width'],alpha=0.5,label='petal_width',color='green')plt.hist(data['petal_length'],alpha=0.5,label='petal_length',color='yellow')plt.hist(data['sepal_length'],alpha=0.5,label='sepal_length',color='purple')plt.legend(loc='upper right')plt.show()

Output:

5
Histogram with Customized Colors

Here, we created overlapping histograms for four different measurements of the Iris flowers. Each histogram is given a different color and some transparency so we can easily compare how these measurements are distributed.


Improve
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp