In my previous posts, we have seen how we can plot stacked histogram (filled) and a stacked Step histogram (unfilled). In this post, we will see how we can plot multiple histograms with different length using Python’s Matplotlib library on the same axis.
Basically, Histograms are a graphical representation of a frequency distribution of numerical data and it’s a great visualizing tool for quickly assessing a probability distribution of a continuous variable (quantitative variable). Here we have three values in an array and we will plot three different coloured histograms on the same axis.
First of all, to create any type of histogram whether it’s a simple histogram or a stacked histogram, we need to import libraries that will help us to implement our task.
Below are the two libraries Numpy and Matplotlib which helps us to perform our task:
import numpy as np from Matplotlib.pyplot as plt
Let’s plot multiple histograms with different length using Python’s Matplotlib library:
The below code will create the stacked step histogram (unfilled) using Python’s Matplotlib library. To plot, we have created an array with three values [] and then passed the array into np.random.randn() using for loop so that Matplotlib library can plot multiple histograms with different length on the same axis. Have a look at the below code:
n_bins=30 colors = ['blue', 'orange', 'green'] # Make a multiple-histogram of array of three values with different length. array = [10000, 5000, 2000] x_multi = [np.random.randn(n) for n in array ] plt.hist(x_multi, n_bins, histtype='bar', label=colors) plt.legend(loc="upper right") plt.title('Different Sample Sizes') plt.show()
Hope you like our post. To learn more about Matplotlib package, you can go through the official documentation here.
Leave a Reply