How to plot two histograms together in Matplotlib?
The histogram is the graphical representation that organizes grouped data points into the specified range. Creating the histogram provides the visual representation of data distribution. By using a histogram we can represent the large amount of data and it’s frequency as one continuous plot.
For creating the Histogram in Matplotlib we use hist() function which belongs to pyplot module. For plotting two histograms together, we have to use hist() function separately with two datasets by giving some setting.
matplotlib.pyplot.hist(x, bins, edgecolor color, label)
Parameter Description x Array or dataset for plotting the histogram bins Integer values or sequence, used for intervals edgecolor or ec Sets the edge color of histogram bars color Sets the bar color of histogram bars label Used to represent the label of the histogram it is of string type. alpha Used for setting amount of transparency. label Used to represent the name or label of the histogram.
- Import module
- Create or load data for two datasets
- Plot histogram with both dataframes separately
- Plot them together
Example 3: Histograms representing two age groups
Example 5: Changing bar color from default