Source code for tesfdmtools.utils.fiterrplot

#!/usr/bin/env python3
#
'''
.. module:: fiterrplot
   :synopsis: make a plot of data with the function fit, showing the errors in sigma 
.. moduleauthor:: Cor de Vries <c.p.de.vries@sron.nl>

'''

import numpy


if __name__ == "__main__":

   import matplotlib
   matplotlib.use('GTK3Agg')

import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
from mpl_toolkits.axes_grid1 import make_axes_locatable

# from printbutton import printbutton

[docs]def fiterrplot(x,y,yerr,fit,xtitle='X',ytitle='Y',ptitle='Plot',ptxt=None,\ pltax=None,sigrange=None,xcurve=None,ptxtsize=None): ''' make a plot of data with the function fit, showing the errors in sigma Args: * `x` = x-coordinates for plot * `y` = y-coordinates of the data * `yerr` = 1-sigma errors of the data * `fit` = y-coordinates of the function fit to the data Kwargs: * `xtitle` = title for the X-axis [default 'X'] * `ytitle` = title for the Y-axis [default 'Y'] * `ptitle` = plot title [default 'Plot'] * `ptxt` = text to appear in the plot (can be multiline with '\\n') [default None] * `pltax` = plot instance for the plot. If None [default] generate plot instance. * `sigrange` = Y-axis range for sigma plot. When set, also plot Y-grid on integer Y values. * `xcurve` = an extra curve to be inserted in the plot * `ptxtsize` = size in pts for the fonts for ptxt. If **None** take default size ''' if pltax is None: fig = plt.figure() ax1 = fig.add_subplot(111) axsize=1.2 else: ax1 = pltax axsize=0.6 ax1.plot(x,y,'b-',drawstyle='steps-mid') ax1.plot(x,fit,'r-') if xcurve is not None: ax1.plot(x,xcurve,'g-') ax1.set_xlabel(xtitle) ax1.set_ylabel(ytitle) ax1.set_title(ptitle) ax1.axes.get_xaxis().set_visible(False) ranges=ax1.axis() if ptxt != None: xt=ranges[0]+0.05*(ranges[1]-ranges[0]) yt=ranges[2]+0.95*(ranges[3]-ranges[2]) if ptxtsize is None: ax1.text(xt,yt,ptxt,verticalalignment='top') else: ax1.text(xt,yt,ptxt,verticalalignment='top',fontsize=ptxtsize) div = make_axes_locatable(ax1) ax2 = div.append_axes("bottom", axsize, pad=0.0, sharex=ax1) ax2.plot(x,(y-fit)/yerr,'b.') ax2.set_ylabel(r'$\Delta/\sigma$') ax2.set_xlabel(xtitle) ax2.set_title('') if sigrange is not None: if len(sigrange) == 2: rrr=list(ax2.axis()) rrr[2:4]=sigrange ax2.axis(rrr) isr=numpy.array(sigrange,dtype=int) dsx=0.10*(rrr[1]-rrr[0]) sxr=numpy.array([(rrr[0]+dsx),(rrr[1]-dsx)]) for ys in numpy.arange((isr[0]+1),isr[1],1): ax2.plot(sxr,[ys,ys],'g--') if pltax is None: plt.tight_layout() # printbutton(fig) plt.show() plt.close('all')
#=============================================================================== if __name__ == "__main__": from tesfdmtools.methods.MCeventlist import MCeventlist from tesfdmtools.methods.holzer import holzer,holzerfit erange=[5850.0,5940.0] binwidth=0.5 bins=int((erange[1]-erange[0])/binwidth) elist=MCeventlist(counts=5000,res=2.7) hist,ebin=numpy.histogram(elist,bins=bins) energy=ebin[0:-1]+(ebin[1]-ebin[0])/2.0 ee=energy/1000.0 pp=holzerfit(ee,hist) fit=holzer(pp,ee) yrr=numpy.sqrt(hist) yerr=numpy.where(yrr == 0,1.0,yrr) tt=['Pos1:','Pos2:','Counts:','FWHM:','C-stat:','Degr. of frdm:'] ptxt= '%7s %7.4f\n' % (tt[0],pp[0]) ptxt=ptxt+'%7s %7.4f\n' % (tt[1],pp[1]) ptxt=ptxt+'%7s %7d\n' % (tt[2],pp[2]) ptxt=ptxt+'%7s %5.2f (eV)\n' % (tt[3],pp[3]*1000.0) ptxt=ptxt+'%7s %7.1f\n' % (tt[4],pp[4]) ptxt=ptxt+'%s %d\n' % (tt[5],pp[5]) xpp=pp.copy() xpp[3]=0.0 xcurve=holzer(xpp,ee) xcurve=xcurve/xcurve.max()*fit.max() fiterrplot(energy,hist,yerr,fit,\ xtitle='Energy (eV)',ytitle='Counts',ptitle='Test',ptxt=ptxt,\ sigrange=[-3.5,3.5],xcurve=xcurve)