Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create multiple histograms on separate graphs with matplotlib?

I have 5 data sets from which I want to create 5 separate histograms. At the moment they are all going on one graph. How can I change this so that it produces two separate graphs?

For simplicity, in my example below I am showing just two histograms. I am looking at the distribution of angle a at 3 different times and the same for angle b.

n, bins, patches = plt.hist(a)
plt.xlabel('Angle a (degrees)') 
plt.ylabel('Frequency')
n, bins, patches = plt.hist(b)
label='2pm,3pm,4pm'
loc = 'center'
plt.legend(label, loc)

plt.xlabel('Angle b(degrees)')        
plt.title('Histogram of b')
plt.ylabel('Frequency')
label='2pm,3pm,4pm'
loc = 'center'
plt.legend(label, loc)

plt.show()
like image 642
Freya Lumb Avatar asked Aug 21 '14 15:08

Freya Lumb


People also ask

How to plot two histograms together in Matplotlib?

Function used 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.

How to draw multiple graphs in a single plot in Matplotlib?

In Matplotlib, we can draw multiple graphs in a single plot in two ways. One is by using subplot () function and other by superimposition of second graph on the first i.e, all graphs will appear on the same plot. We will look into both the ways one by one. Multiple Plots using subplot () Function

How to plot histogram with two DataFrames in one dataset?

Approach 1 Import module 2 Create or load data for two datasets 3 Plot histogram with both dataframes separately 4 Plot them together

How to create two histograms with and without overlapping bars?

Below shows methods to create the two histograms with and without overlapping bars. When we call plt.hist twice to plot the histograms individually, the two histograms will have the overlapped bars as you could see above. The alpha property specifies the transparency of the plot. 0.0 is transparent and 1.0 is opaque.


Video Answer


2 Answers

This is probably when you want to use matplotlib's object-oriented interface. There are a couple ways that you could handle this.

First, you could want each plot on an entirely separate figure. In which case, matplotlib lets you keep track of various figures.

import numpy as np
import matplotlib.pyplot as plt

a = np.random.normal(size=200)
b = np.random.normal(size=200)

fig1 = plt.figure()
ax1 = fig1.add_subplot(1, 1, 1)
n, bins, patches = ax1.hist(a)
ax1.set_xlabel('Angle a (degrees)')
ax1.set_ylabel('Frequency')

fig2 = plt.figure()
ax2 = fig2.add_subplot(1, 1, 1)
n, bins, patches = ax2.hist(b)
ax2.set_xlabel('Angle b (degrees)')
ax2.set_ylabel('Frequency')

Or, you can divide your figure into multiple subplots and plot a histogram on each of those. In which case matplotlib lets you keep track of the various subplots.

fig = plt.figure()
ax1 = fig.add_subplot(2, 1, 1)
ax2 = fig.add_subplot(2, 1, 2)

n, bins, patches = ax1.hist(a)
ax1.set_xlabel('Angle a (degrees)')
ax1.set_ylabel('Frequency')

n, bins, patches = ax2.hist(b)
ax2.set_xlabel('Angle b (degrees)')
ax2.set_ylabel('Frequency')

Answer to this question explain the numbers in add_subplot.

like image 110
Roger Fan Avatar answered Oct 26 '22 18:10

Roger Fan


I recently used pandas for doing the same thing. If you're reading from csv/text then it could be really easy.

import pandas as pd
data = pd.read_csv("yourfile.csv") # columns a,b,c,etc
data.hist(bins=20)

It's really just wrapping the matplotlib into one call, but works nicely.

like image 34
matt_s Avatar answered Oct 26 '22 17:10

matt_s