Creating a plot with two histograms
Here is a method that you can use to plot two histograms in the same figure sharing the same X-axis, keeping some distance between the histograms:
def twohists(x1,x2,xmin,xmax,x1leg='$x_1$',x2leg='$x_2$',xlabel='',sharey=False):
"""
Script that plots two histograms of quantities x1 and x2.
Arguments:
- x1,x2: arrays with data to be plotted
- xmin,xmax: lower and upper range of plotted values, will be used to set a consistent x-range
for both histograms.
- x1leg, x2leg: legends for each histogram
- xlabel: self-explanatory.
Inspired by http://www.scipy.org/Cookbook/Matplotlib/Multiple_Subplots_with_One_Axis_Label.
Rodrigo Nemmen
v1 Dec. 2011
v1.1 Feb. 2012: Added sharey argument.
"""
pylab.clf()
pylab.rcParams.update({'font.size': 15})
fig=pylab.figure()
a=fig.add_subplot(2,1,1)
if sharey==True:
b=fig.add_subplot(2,1,2, sharex=a, sharey=a)
else:
b=fig.add_subplot(2,1,2, sharex=a)
a.hist(x1,label=x1leg,color='b')
a.legend(loc='best',frameon=False)
a.set_xlim(xmin,xmax)
b.hist(x2,label=x2leg,color='r')
b.legend(loc='best',frameon=False)
pylab.setp(a.get_xticklabels(), visible=False)
b.set_xlabel(xlabel)
b.set_ylabel('Number',verticalalignment='bottom')
pylab.minorticks_on()
pylab.subplots_adjust(hspace=0.15)
pylab.draw()
... and here is a example script that uses the method above:
"""
Illustrates how to use the twohists method.
"""
import nemmen
import scipy, pylab
# Generates a normal distribution
x1=scipy.random.standard_normal(100)
# Generates a uniform random distribution
x2=scipy.random.uniform(-3,3,100)
nemmen.twohists(x1,x2,-3,3,'Normal','Uniform')
pylab.show()
... to create the following plot:
Creating a plot with three histograms
I also wrote a recipe that makes a plot with
three histograms:
def threehists(x1,x2,x3,xmin,xmax,x1leg='$x_1$',x2leg='$x_2$',x3leg='$x_3$',xlabel='',sharey=False):
"""
Script that plots three histograms of quantities x1, x2 and x3.
Arguments:
- x1,x2,x3: arrays with data to be plotted
- xmin,xmax: lower and upper range of plotted values, will be used to set a consistent x-range
for both histograms.
- x1leg, x2leg, x3leg: legends for each histogram
- xlabel: self-explanatory.
- sharey: sharing the Y-axis among the histograms?
Example:
x1=Lbol(AD), x2=Lbol(JD), x3=Lbol(EHF10)
>>> threehists(x1,x2,x3,38,44,'AD','JD','EHF10','$\log L_{\\rm bol}$ (erg s$^{-1}$)',sharey=True)
Inspired by http://www.scipy.org/Cookbook/Matplotlib/Multiple_Subplots_with_One_Axis_Label.
Rodrigo Nemmen
v1 Dec. 2011
v1.1 Feb. 2012: Added sharey keyword.
"""
pylab.clf()
pylab.rcParams.update({'font.size': 15})
fig=pylab.figure()
a=fig.add_subplot(3,1,1)
if sharey==True:
b=fig.add_subplot(3,1,2, sharex=a, sharey=a)
c=fig.add_subplot(3,1,3, sharex=a, sharey=a)
else:
b=fig.add_subplot(3,1,2, sharex=a)
c=fig.add_subplot(3,1,3, sharex=a)
a.hist(x1,label=x1leg,color='b')
a.legend(loc='best',frameon=False)
a.set_xlim(xmin,xmax)
b.hist(x2,label=x2leg,color='r')
b.legend(loc='best',frameon=False)
c.hist(x3,label=x3leg,color='y')
c.legend(loc='best',frameon=False)
pylab.setp(a.get_xticklabels(), visible=False)
pylab.setp(b.get_xticklabels(), visible=False)
c.set_xlabel(xlabel)
b.set_ylabel('Number')
pylab.minorticks_on()
pylab.subplots_adjust(hspace=0.15)
pylab.draw()
... and as before, here is a script that illustrates how to use the above method:
"""
Illustrates how to use the threehists method.
"""
import nemmen
import scipy, pylab
# Generates a normal distribution
x1=scipy.random.standard_normal(100)
# Generates a uniform random distribution
x2=scipy.random.uniform(-3,3,100)
x3=scipy.random.standard_normal(1000)
nemmen.threehists(x1,x2,x3,-3,3,'Normal ($n=100$)','Uniform','Normal ($n=1000$)')
pylab.show()
... creating this plot: