def onehist(x,xlabel='',fontsize=12):
"""
Script that plots the histogram of x with the corresponding xlabel.
"""
pylab.clf()
pylab.rcParams.update({'font.size': fontsize})
pylab.hist(x,histtype='stepfilled')
pylab.legend()
#### Change the X-axis appropriately ####
pylab.xlabel(xlabel)
pylab.ylabel('Number')
pylab.draw()
pylab.show()
python类draw()的实例源码
def threehistsx(x1,x2,x3,x1leg='$x_1$',x2leg='$x_2$',x3leg='$x_3$',fig=1,fontsize=12,bins1=10,bins2=10,bins3=10):
"""
Script that pretty-plots three histograms of quantities x1, x2 and x3.
Arguments:
:param x1,x2,x3: arrays with data to be plotted
:param x1leg, x2leg, x3leg: legends for each histogram
:param fig: which plot window should I use?
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}$)')
Inspired by http://www.scipy.org/Cookbook/Matplotlib/Multiple_Subplots_with_One_Axis_Label.
"""
pylab.rcParams.update({'font.size': fontsize})
pylab.figure(fig)
pylab.clf()
pylab.subplot(3,1,1)
pylab.hist(x1,label=x1leg,color='b',bins=bins1)
pylab.legend(loc='best',frameon=False)
pylab.subplot(3,1,2)
pylab.hist(x2,label=x2leg,color='r',bins=bins2)
pylab.legend(loc='best',frameon=False)
pylab.subplot(3,1,3)
pylab.hist(x3,label=x3leg,color='y',bins=bins3)
pylab.legend(loc='best',frameon=False)
pylab.minorticks_on()
pylab.subplots_adjust(hspace=0.15)
pylab.draw()
pylab.show()
def show_tf(self):
if self._pylab is None:
import pylab
self._pylab = pylab
if self._tf_figure is None:
self._tf_figure = self._pylab.figure(2)
self.transfer_function.show(ax=self._tf_figure.axes)
self._pylab.draw()
def draw(self):
self._pylab.draw()
def snapshot(self, fn = None, clip_ratio = None):
import matplotlib.pylab as pylab
pylab.figure(2)
self.transfer_function.show()
pylab.draw()
im = Camera.snapshot(self, fn, clip_ratio)
pylab.figure(1)
pylab.imshow(im / im.max())
pylab.draw()
self.frames.append(im)
def _viz_Gauss_before_after(
curModel=None, propModel=None,
curSS=None, propSS=None,
Plan=None,
propLscore=None, curLscore=None,
Data_b=None, Data_t=None,
**kwargs):
pylab.subplots(
nrows=1, ncols=2, figsize=(8, 4), num=1)
h1 = pylab.subplot(1, 2, 1)
h1.clear()
GaussViz.plotGauss2DFromHModel(
curModel, compsToHighlight=Plan['btargetCompID'], figH=h1)
if curLscore is not None:
pylab.title('%.4f' % (curLscore))
h2 = pylab.subplot(1, 2, 2, sharex=h1, sharey=h1)
h2.clear()
newCompIDs = np.arange(curModel.obsModel.K, propModel.obsModel.K)
GaussViz.plotGauss2DFromHModel(
propModel, compsToHighlight=newCompIDs, figH=h2, Data=Data_t)
if propLscore is not None:
pylab.title('%.4f' % (propLscore))
Lgain = propLscore - curLscore
if Lgain > 0:
pylab.xlabel('ACCEPT +%.2f' % (Lgain))
else:
pylab.xlabel('REJECT %.2f' % (Lgain))
pylab.draw()
pylab.subplots_adjust(hspace=0.1, top=0.9, bottom=0.15,
left=0.15, right=0.95)
def mpl_runner(safe_execfile):
"""Factory to return a matplotlib-enabled runner for %run.
Parameters
----------
safe_execfile : function
This must be a function with the same interface as the
:meth:`safe_execfile` method of IPython.
Returns
-------
A function suitable for use as the ``runner`` argument of the %run magic
function.
"""
def mpl_execfile(fname,*where,**kw):
"""matplotlib-aware wrapper around safe_execfile.
Its interface is identical to that of the :func:`execfile` builtin.
This is ultimately a call to execfile(), but wrapped in safeties to
properly handle interactive rendering."""
import matplotlib
import matplotlib.pylab as pylab
#print '*** Matplotlib runner ***' # dbg
# turn off rendering until end of script
is_interactive = matplotlib.rcParams['interactive']
matplotlib.interactive(False)
safe_execfile(fname,*where,**kw)
matplotlib.interactive(is_interactive)
# make rendering call now, if the user tried to do it
if pylab.draw_if_interactive.called:
pylab.draw()
pylab.draw_if_interactive.called = False
return mpl_execfile
def twohists(x1,x2,xmin,xmax,range=None,x1leg='$x_1$',x2leg='$x_2$',xlabel='',fig=1,sharey=False,fontsize=12,bins1=10,bins2=10):
"""
Script that plots two histograms of quantities x1 and x2
sharing the same X-axis.
:param x1,x2: arrays with data to be plotted
:param xmin,xmax: lower and upper range of plotted values, will be used to set a consistent x-range
for both histograms.
:param x1leg, x2leg: legends for each histogram
:param xlabel: self-explanatory.
:param bins1,bins2: number of bins in each histogram
:param fig: which plot window should I use?
:param range: in the form (xmin,xmax), same as range argument for hist and applied to both
histograms.
Inspired by `Scipy <http://www.scipy.org/Cookbook/Matplotlib/Multiple_Subplots_with_One_Axis_Label>`_.
"""
pylab.rcParams.update({'font.size': fontsize})
fig=pylab.figure(fig)
pylab.clf()
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,bins1,label=x1leg,color='b',histtype='stepfilled',range=range)
a.legend(loc='best',frameon=False)
a.set_xlim(xmin,xmax)
b.hist(x2,bins2,label=x2leg,color='r',histtype='stepfilled',range=range)
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()
pylab.show()
def threehists(x1,x2,x3,xmin,xmax,x1leg='$x_1$',x2leg='$x_2$',x3leg='$x_3$',xlabel='',fig=1,sharey=False,fontsize=12):
"""
Script that plots three histograms of quantities x1, x2 and x3
sharing the same X-axis.
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?
- fig: which plot window should I use?
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 `Scipy <http://www.scipy.org/Cookbook/Matplotlib/Multiple_Subplots_with_One_Axis_Label>`_.
"""
pylab.rcParams.update({'font.size': fontsize})
fig=pylab.figure(fig)
pylab.clf()
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',histtype='stepfilled')
a.legend(loc='best',frameon=False)
a.set_xlim(xmin,xmax)
b.hist(x2,label=x2leg,color='r',histtype='stepfilled')
b.legend(loc='best',frameon=False)
c.hist(x3,label=x3leg,color='y',histtype='stepfilled')
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()
pylab.show()
def fourcumplot(x1,x2,x3,x4,xmin,xmax,x1leg='$x_1$',x2leg='$x_2$',x3leg='$x_3$',x4leg='$x_3$',xlabel='',ylabel='$N(x>x\')$',fig=1,sharey=False,fontsize=12,bins1=50,bins2=50,bins3=50,bins4=50):
"""
Script that plots the cumulative histograms of four variables x1, x2, x3 and x4
sharing the same X-axis. For each bin, Y is the fraction of the sample
with values above X.
Arguments:
- x1,x2,x3,x4: 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, x4leg: legends for each histogram
- xlabel: self-explanatory.
- sharey: sharing the Y-axis among the histograms?
- bins1,bins2,...: number of bins in each histogram
- fig: which plot window should I use?
Inspired by `Scipy <http://www.scipy.org/Cookbook/Matplotlib/Multiple_Subplots_with_One_Axis_Label>`_.
v1 Jun. 2012: inherited from fourhists.
"""
pylab.rcParams.update({'font.size': fontsize})
fig=pylab.figure(fig)
pylab.clf()
a=fig.add_subplot(4,1,1)
if sharey==True:
b=fig.add_subplot(4,1,2, sharex=a, sharey=a)
c=fig.add_subplot(4,1,3, sharex=a, sharey=a)
d=fig.add_subplot(4,1,4, sharex=a, sharey=a)
else:
b=fig.add_subplot(4,1,2, sharex=a)
c=fig.add_subplot(4,1,3, sharex=a)
d=fig.add_subplot(4,1,4, sharex=a)
a.hist(x1,bins1,label=x1leg,color='b',cumulative=-True,normed=True,histtype='stepfilled')
a.legend(loc='best',frameon=False)
a.set_xlim(xmin,xmax)
b.hist(x2,bins2,label=x2leg,color='r',cumulative=-True,normed=True,histtype='stepfilled')
b.legend(loc='best',frameon=False)
c.hist(x3,bins3,label=x3leg,color='y',cumulative=-True,normed=True,histtype='stepfilled')
c.legend(loc='best',frameon=False)
d.hist(x4,bins4,label=x4leg,color='g',cumulative=-True,normed=True,histtype='stepfilled')
d.legend(loc='best',frameon=False)
pylab.setp(a.get_xticklabels(), visible=False)
pylab.setp(b.get_xticklabels(), visible=False)
pylab.setp(c.get_xticklabels(), visible=False)
d.set_xlabel(xlabel)
c.set_ylabel(ylabel)
pylab.minorticks_on()
pylab.subplots_adjust(hspace=0.15)
pylab.draw()
pylab.show()