def example5(): data = numpy.random.normal(loc=0.2, scale=1.2, size=1000) x = numpy.linspace(-3.5, 3.5, 30) y = numpy.exp( - 0.5*(x-0.1)**2 ) key=graph.key.key(pos='tl') g = graph.graphxy( width=8, x=axis.lin(title=r'$score$'), y=axis.lin(min=0, max=1.1), key=key, ) d = graph.data.values( x=x, y=y, title='data', ) lineattrs=[colors('blue')] styles=[ graph.style.histogram(lineattrs=lineattrs,steps=1), ] g.plot(d, styles=styles) g.writePDFfile("example5.pdf") g.writeGSfile("example5.png")
def example4(): g = graph.graphxy( width=8, x=axis.lin(title=r'$x_1$'), y=axis.lin(min=-1,max=1.5,title=r'$y_1$'), ) g.plot( graph.data.function("y(x)=2*exp(-30*x)-exp(-3*x)", min=0, max=1), ) insetparams = dict( #height = width*float(image.shape[0])/image.shape[1] labeldist=0.2, labelattrs=[text.size.footnotesize], ) p=axis.painter.regular(**insetparams) gi = graph.graphxy( width=3, x=axis.lin(min=0, max=2, painter=p), y=axis.lin(painter=p), ) gi.plot(graph.data.function("y(x)=cos(20*x)*exp(-2*x)", min=0,max=2)) x, y = g.vpos(0.9, 0.9) g.insert(gi, [trafo.translate(x-gi.bbox().right(), y-gi.bbox().top())]) g.writePDFfile("example4.pdf")
def test_basic(): # can define by hex string or rgb value, normalized to 1 #yellow=color.rgbfromhexstring('#FFFF00') yellow=color.rgb(r=1.0, g=1.0, b=0.0) # need to specify one or both of width, height #key=graph.key.key(pos="br", dist=0.1) key=graph.key.key(pos='tl') g = graph.graphxy( width=8, height=8, key=key, x=axis.lin(title=r"$x$-axis"), y=axis.lin(title=r"$y$-axis"), ) # either provide lists of the individual coordinates factor=1.2343 x=list(range(10)) y=[factor*xx**2 for xx in x] values=graph.data.values(x=x, y=y,title='points') symbol1=graph.style.symbol( #symbol=graph.style._circlesymbol, symbol=graph.style.symbol.circle, symbolattrs=[deco.filled([yellow])], ) symbol2=graph.style.symbol( #symbol=graph.style._circlesymbol, symbol=graph.style.symbol.circle, symbolattrs=[color.rgb.red], ) g.plot(values,[symbol1,symbol2]) # or provide one list containing the whole points #values2=graph.data.points(list(zip(range(10), range(10))), x=1, y=2) #g.plot(values2) # also a function c=graph.data.function( "y(x)=%g * x**2" % factor, min=0, max=9, title=r'$y=x^2$' ) g.plot(c) g.writeEPSfile("points-and-line") g.writePDFfile("points-and-line")
def plot_c(x, corr, err_corr, nocorr, err_nocorr, ymin, ymax, xaxis): ylabel=r'$c~[10^{-4}]$' yaxis=axis.lin(min=ymin,max=ymax,title=ylabel,density=2) ypos=0.0 key=None c,g = doplot_full(x,corr,err_corr,nocorr,err_nocorr,xaxis,yaxis,ypos,key) return c,g
def example3(): """ 0,0 is bottom left in pyx land, but it is more common for "first" to be upper left """ c = canvas.canvas() g1 = graph.graphxy( width=8, x=axis.lin(title=r'$x_1$'), y=axis.lin(title=r'$y_1$'), ) g1.plot( [graph.data.function("y(x)=0", min=0, max=1), graph.data.function("y(x)=2*exp(-30*x)-exp(-3*x)", min=0, max=1)], ) c.insert(g1) g2 = graph.graphxy( width=8, ypos=g1.height+1.5, x=axis.lin(title=r'$x_2$'), y=axis.lin(title=r'$y_2$'), ) c.insert(g2) g2.plot(graph.data.function("y(x)=cos(20*x)*exp(-2*x)",min=0,max=2)) g3 = graph.graphxy( width=8, ypos=g1.height+1.5, xpos=g1.width+2.0, x=axis.lin(title=r'$x_3$'), y=axis.lin(title=r'$y_3$'), ) g3.plot(graph.data.function("y(x)=cos(20*x)*exp(-2*x)",min=0,max=2)) c.insert(g3) g4 = graph.graphxy( width=8, #ypos=g1.height, xpos=g1.width+2.0, x=axis.lin(title=r'$x_4$'), y=axis.lin(title=r'$y_4$'), ) g4.plot(graph.data.function("y(x)=cos(20*x)*exp(-2*x)",min=0,max=2)) c.insert(g4) c.writePDFfile("example3.pdf") c.writeGSfile("example3.png")
def plot_m(x, corr, err_corr, nocorr, err_nocorr, ymin, ymax, cplt): xaxis=graph.axis.linkedaxis(cplt.axes["x"]) ylabel=r'$m~[10^{-3}]$' yaxis=axis.lin(min=ymin,max=ymax,title=ylabel,density=2) ypos=cplt.height+0.5 key=graph.key.key(pos="br") c,g = doplot_full(x,corr,err_corr,nocorr,err_nocorr,xaxis,yaxis,ypos,key) return c,g
def plot_c2thresh(): nocorr = [0.17, 0.93, 0.81, 0.58, 0.49, 0.42] err_nocorr = [0.09, 0.10, 0.10, 0.10, 0.11, 0.11] corr = [0.17, 0.17, 0.15, 0.13, 0.14, 0.12] err_corr = [0.09, 0.10, 0.10, 0.10, 0.11, 0.11] xlabel=r'$S/N$ threshold' ymin,ymax=-3,3 xaxis=axis.lin(min=xrng[0],max=xrng[1],title=xlabel) c,g=plot_c(thresh,corr,err_corr,nocorr,err_nocorr,ymin,ymax,xaxis) return c,g
def _get_axes(kw): xlog=kw.get('xlog',False) ylog=kw.get('ylog',False) xkw=dict( title=kw.get('xlabel',None), min=kw.get('xmin',None), max=kw.get('xmax',None), # density of ticks density=kw.get('xdensity',1), reverse=kw.get('xreverse',0), ) ykw=dict( title=kw.get('ylabel',None), min=kw.get('ymin',None), max=kw.get('ymax',None), # density of ticks density=kw.get('ydensity',1), reverse=kw.get('yreverse',0), ) if xlog: xaxis = axis.log(**xkw) else: xaxis = axis.lin(**xkw) if ylog: yaxis = axis.log(**ykw) else: yaxis = axis.lin(**ykw) return xaxis, xlog, yaxis, ylog
def doplot(self, d, xlabel=None, xlog=False, ymin=-0.0029, ymax=0.0029, xmin=None, xmax=None, nocorr=False, fitlines=False, **kw): """ plot the results parameters ---------- d: dict result of running something like process_run or process_flist **kw: extra plotting keywords """ import pyxtools from pyx import graph, deco, style from pyx.graph import axis if xlabel is None: xlabel = 'x' xdensity = kw.get('xdensity', 1.5) ydensity = kw.get('ydensity', 1.5) red = pyxtools.colors('red') blue = pyxtools.colors('blue') xvals = self._extract_xvals(d) if nocorr: sh = d['shear_nocorr']['shear'] sherr = d['shear_nocorr']['shear_err'] else: sh = d['shear']['shear'] sherr = d['shear']['shear_err'] if xlog: xcls = axis.log else: xcls = axis.lin xaxis = xcls( title=xlabel, density=xdensity, min=xmin, max=xmax, ) yaxis = axis.lin( title=r"$\langle g \rangle$", density=ydensity, min=ymin, max=ymax, ) key = graph.key.key(pos='bl') g = graph.graphxy( width=8, ratio=1.2, key=key, x=xaxis, y=yaxis, ) c = graph.data.function( "y(x)=0", title=None, min=xvals[0], max=xvals[-1], ) g.plot(c) g1values = graph.data.values( x=list(xvals), y=list(sh[:, 0]), dy=list(sherr[:, 0]), title=r'$g_1$', ) g2values = graph.data.values( x=list(xvals), y=list(sh[:, 1]), dy=list(sherr[:, 1]), title=r'$g_2$', ) symbol1 = graph.style.symbol( symbol=graph.style.symbol.circle, symbolattrs=[red, deco.filled([red])], size=0.1, ) symbol2 = graph.style.symbol( symbol=graph.style.symbol.triangle, symbolattrs=[blue, deco.filled([blue])], size=0.1, ) g.plot(g1values, [symbol1, graph.style.errorbar(errorbarattrs=[red])]) g.plot(g2values, [symbol2, graph.style.errorbar(errorbarattrs=[blue])]) use_errors = True if fitlines and not use_errors: res1 = fitline(xvals, sh[:, 0]) res2 = fitline(xvals, sh[:, 1]) fmt = '(%(slope).3g +/- %(slope_err).3g) x + (%(offset).3g +/- %(offset_err).3g)' print("line1: " + fmt % res1) print("line2: " + fmt % res2) if self.element is not None: tit1 = r'$g_1=%.2g ~g^{psf}_%d + %.2g$' % ( res1['slope'], self.element + 1, res1['offset']) tit2 = r'$g_2=%.2g ~g^{psf}_%d + %.2g$' % ( res2['slope'], self.element + 1, res2['offset']) else: tit1 = r'$g_1=%.2g ~x + %.2g$' % (res1['slope'], res1['offset']) tit2 = r'$g_2=%.2g ~x + %.2g$' % (res1['slope'], res1['offset']) c1 = graph.data.function( "y(x)=%g*x + %g" % tuple((res1['slope'], res1['offset'])), title=tit1, min=xvals[0], max=xvals[-1], ) c2 = graph.data.function( "y(x)=%g*x + %g" % tuple((res2['slope'], res2['offset'])), title=tit2, min=xvals[0], max=xvals[-1], ) styles1 = [ graph.style.line([red, style.linestyle.solid]), ] styles2 = [ graph.style.line([blue, style.linestyle.dashed]), ] g.plot(c1, styles1) g.plot(c2, styles2) if fitlines and use_errors: # old code import fitting l1 = fitting.fit_line(xvals, sh[:, 0], yerr=sherr[:, 0]) l2 = fitting.fit_line(xvals, sh[:, 1], yerr=sherr[:, 1]) res1 = l1.get_result() res2 = l2.get_result() pars1, err1 = res1['pars'], res1['perr'] pars2, err2 = res2['pars'], res2['perr'] fmt = '(%.3g +/- %.3g) x + (%.3g +/- %.3g)' print("line1: " + fmt % (pars1[0], err1[0], pars1[1], err1[1])) print("line2: " + fmt % (pars2[0], err2[0], pars2[1], err2[1])) if self.element is not None: tit1 = r'$g_1=%.2g ~g^{psf}_%d + %.2g$' % ( pars1[0], self.element + 1, pars1[1]) tit2 = r'$g_2=%.2g ~g^{psf}_%d + %.2g$' % ( pars2[0], self.element + 1, pars2[1]) else: tit1 = r'$g_1=%.2g ~x + %.2g$' % (pars1[0], pars1[1]) tit2 = r'$g_2=%.2g ~x + %.2g$' % (pars2[0], pars2[1]) c1 = graph.data.function( "y(x)=%g*x + %g" % tuple(pars1), title=tit1, min=xvals[0], max=xvals[-1], ) c2 = graph.data.function( "y(x)=%g*x + %g" % tuple(pars2), title=tit2, min=xvals[0], max=xvals[-1], ) styles1 = [ graph.style.line([red, style.linestyle.solid]), ] styles2 = [ graph.style.line([blue, style.linestyle.dashed]), ] g.plot(c1, styles1) g.plot(c2, styles2) if 'file' in kw: print("writing:", kw['file']) pyxtools.write(g, kw['file'], dpi=200) if fitlines and 'fitfile' in kw: print("writing:", kw['fitfile']) with open(kw['fitfile'], 'w') as fobj: fobj.write(repr(l1)) fobj.write('\n') fobj.write(repr(l2)) fobj.write('\n') return g
from math import pi from pyx import * from pyx.graph import axis xgridpainter = axis.painter.regular(gridattrs=[]) ygridpainter = axis.painter.regular( gridattrs=[attr.changelist([style.linestyle.dashed, None])]) g = graph.graphxy(width=10, x=axis.lin(painter=xgridpainter, divisor=pi, texter=axis.texter.rational(suffix=r"\pi"), min=-4 * pi, max=4 * pi), y=axis.lin(painter=ygridpainter)) g.plot(graph.data.function("y(x)=sin(x)/x", points=200)) g.writeEPSfile() g.writePDFfile() g.writeSVGfile()
def doplot(self, d, xlabel=None, xlog=False, ymin=-0.0029, ymax=0.0029, xmin=None,xmax=None, nocorr=False, fitlines=False, **kw): """ plot the results parameters ---------- d: dict result of running something like process_run or process_flist **kw: extra plotting keywords """ import pyxtools from pyx import graph, deco, style from pyx.graph import axis if xlabel is None: xlabel='x' xdensity=kw.get('xdensity',1.5) ydensity=kw.get('ydensity',1.5) red=pyxtools.colors('red') blue=pyxtools.colors('blue') xvals=self._extract_xvals(d) if nocorr: sh=d['shear_nocorr']['shear'] sherr=d['shear_nocorr']['shear_err'] else: sh=d['shear']['shear'] sherr=d['shear']['shear_err'] if xlog: xcls=axis.log else: xcls=axis.lin xaxis=xcls( title=xlabel, density=xdensity, min=xmin, max=xmax, ) yaxis=axis.lin( title=r"$\langle g \rangle$", density=ydensity, min=ymin, max=ymax, ) key=graph.key.key(pos='bl') g = graph.graphxy( width=8, ratio=1.2, key=key, x=xaxis, y=yaxis, ) c=graph.data.function( "y(x)=0", title=None, min=xvals[0], max=xvals[-1], ) g.plot(c) g1values=graph.data.values( x=list(xvals), y=list(sh[:,0]), dy=list(sherr[:,0]), title=r'$g_1$', ) g2values=graph.data.values( x=list(xvals), y=list(sh[:,1]), dy=list(sherr[:,1]), title=r'$g_2$', ) symbol1=graph.style.symbol( symbol=graph.style.symbol.circle, symbolattrs=[red,deco.filled([red])], size=0.1, ) symbol2=graph.style.symbol( symbol=graph.style.symbol.triangle, symbolattrs=[blue,deco.filled([blue])], size=0.1, ) g.plot(g1values,[symbol1, graph.style.errorbar(errorbarattrs=[red])]) g.plot(g2values,[symbol2, graph.style.errorbar(errorbarattrs=[blue])]) use_errors=True if fitlines and not use_errors: res1=fitline(xvals,sh[:,0]) res2=fitline(xvals,sh[:,1]) fmt='(%(slope).3g +/- %(slope_err).3g) x + (%(offset).3g +/- %(offset_err).3g)' print("line1: "+fmt % res1) print("line2: "+fmt % res2) if self.element is not None: tit1=r'$g_1=%.2g ~g^{psf}_%d + %.2g$' % (res1['slope'],self.element+1,res1['offset']) tit2=r'$g_2=%.2g ~g^{psf}_%d + %.2g$' % (res2['slope'],self.element+1,res2['offset']) else: tit1=r'$g_1=%.2g ~x + %.2g$' % (res1['slope'],res1['offset']) tit2=r'$g_2=%.2g ~x + %.2g$' % (res1['slope'],res1['offset']) c1=graph.data.function( "y(x)=%g*x + %g" % tuple( (res1['slope'], res1['offset'])), title=tit1, min=xvals[0], max=xvals[-1], ) c2=graph.data.function( "y(x)=%g*x + %g" % tuple( (res2['slope'], res2['offset'])), title=tit2, min=xvals[0], max=xvals[-1], ) styles1=[ graph.style.line([red, style.linestyle.solid]), ] styles2=[ graph.style.line([blue, style.linestyle.dashed]), ] g.plot(c1,styles1) g.plot(c2,styles2) if fitlines and use_errors: # old code import fitting l1=fitting.fit_line(xvals,sh[:,0],yerr=sherr[:,0]) l2=fitting.fit_line(xvals,sh[:,1],yerr=sherr[:,1]) res1=l1.get_result() res2=l2.get_result() pars1,err1=res1['pars'],res1['perr'] pars2,err2=res2['pars'],res2['perr'] fmt='(%.3g +/- %.3g) x + (%.3g +/- %.3g)' print("line1: "+fmt % (pars1[0],err1[0],pars1[1],err1[1])) print("line2: "+fmt % (pars2[0],err2[0],pars2[1],err2[1])) if self.element is not None: tit1=r'$g_1=%.2g ~g^{psf}_%d + %.2g$' % (pars1[0],self.element+1,pars1[1]) tit2=r'$g_2=%.2g ~g^{psf}_%d + %.2g$' % (pars2[0],self.element+1,pars2[1]) else: tit1=r'$g_1=%.2g ~x + %.2g$' % (pars1[0],pars1[1]) tit2=r'$g_2=%.2g ~x + %.2g$' % (pars2[0],pars2[1]) c1=graph.data.function( "y(x)=%g*x + %g" % tuple(pars1), title=tit1, min=xvals[0], max=xvals[-1], ) c2=graph.data.function( "y(x)=%g*x + %g" % tuple(pars2), title=tit2, min=xvals[0], max=xvals[-1], ) styles1=[ graph.style.line([red, style.linestyle.solid]), ] styles2=[ graph.style.line([blue, style.linestyle.dashed]), ] g.plot(c1,styles1) g.plot(c2,styles2) if 'file' in kw: print("writing:",kw['file']) pyxtools.write(g, kw['file'], dpi=200) if fitlines and 'fitfile' in kw: print("writing:",kw['fitfile']) with open(kw['fitfile'],'w') as fobj: fobj.write(repr(l1)) fobj.write('\n') fobj.write(repr(l2)) fobj.write('\n') return g
from math import pi from pyx import * from pyx.graph import axis xgridpainter = axis.painter.regular(gridattrs=[]) ygridpainter = axis.painter.regular(gridattrs=[attr.changelist([style.linestyle.dashed, None])]) g = graph.graphxy(width=10, x=axis.lin(painter=xgridpainter, divisor=pi, texter=axis.texter.rational(suffix=r"\pi"), min=-4*pi, max=4*pi), y=axis.lin(painter=ygridpainter)) g.plot(graph.data.function("y(x)=sin(x)/x", points=200)) g.writeEPSfile() g.writePDFfile() g.writeSVGfile()