def plot_species(species_log, name=""): ''' Visualizes speciation throughout evolution. Lisa Meeden added a name parameter for handling multiple visualizations in co-evolution. ''' plot = biggles.FramedPlot() plot.title = "Speciation" plot.ylabel = r"Size per Species" plot.xlabel = r"Generations" generation = [i for i in range(len(species_log))] species = [] curves = [] for gen in range(len(generation)): for j in range(len(species_log), 0, -1): try: species.append(species_log[-j][gen] + sum(species_log[-j][:gen])) except IndexError: species.append(sum(species_log[-j][:gen])) curves.append(species) species = [] s1 = biggles.Curve(generation, curves[0]) plot.add(s1) plot.add(biggles.FillBetween(generation, [0]*len(generation), generation, curves[0], color=random.randint(0,90000))) for i in range(1, len(curves)): c = biggles.Curve(generation, curves[i]) plot.add(c) plot.add(biggles.FillBetween(generation, curves[i-1], generation, curves[i], color=random.randint(0,90000))) plot.write_img(1024, 800, name+'speciation.svg')
def plot_species_biggles(species_log, dest_dir='.'): if HAS_BIGGLES: plot = biggles.FramedPlot() plot.title = "Speciation" plot.ylabel = r"Size per Species" plot.xlabel = r"Generations" generation = [i for i in range(len(species_log))] species = [] curves = [] for gen in range(len(generation)): for j in range(len(species_log), 0, -1): try: species.append(species_log[-j][gen] + sum(species_log[-j][:gen])) except IndexError: species.append(sum(species_log[-j][:gen])) curves.append(species) species = [] new_curve = biggles.Curve(generation, curves[0]) plot.add(new_curve) plot.add( biggles.FillBetween(generation, [0] * len(generation), generation, curves[0], color=random.randint(0, 90000))) for i in range(1, len(curves)): curve = biggles.Curve(generation, curves[i]) plot.add(curve) plot.add( biggles.FillBetween(generation, curves[i - 1], generation, curves[i], color=random.randint(0, 90000))) try: plot.write_img(1300, 800, os.path.join(dest_dir, 'speciation.png')) except Exception: print(traceback.format_exc()) else: print('You do not have the Biggles package.')
def diagonal_fill(x0, y0, x1, y1, sep=0.2, size=0.05, invert=0, **kw): """ Fill the rectangle described by x0,y0, x1,y1 (lower left and upper right corner) with diagonal bars. @param x0: rectangular coorinates @type x0: float @param y0: rectangular coorinates @type y0: float @param x1: rectangular coorinates @type x1: float @param y1: rectangular coorinates @type y1: float @param sep: offset between (low edges of) diagonal lines (default: 0.2) @type sep: float @param size: width of diagonal line (default: 0.05) @type size: float @param invert: mirror diagonals (default: 0) @type invert: 1|0 @param kw: additional key-value pairs @type kw: key=value @return: list of biggles plot objects @rtype: [Biggles.FillBetween] """ r = [] ## will hold result vy = y0 - (x1 - x0) - sep ## virtual y coordinate ## shift mirrored lines horizontally to get better criss-cross effect if invert: vy = vy - sep / 2 while vy < (y1 - y0): ## lower diagonal xa, ya, xb, yb = boxed_diagonal(x0, y0, x1, y1, vy) ## upper diagonal if vy + size < y1 - y0: xc, yc, xd, yd = boxed_diagonal(x0, y0, x1, y1, vy + size) else: ## never start outside box xc, yc = xa, y1 - y0 xd, yd = xb, y1 - y0 ## mirror line if invert: xa = x1 - (xa - x0) xb = x0 - (xb - x1) xc = x1 - (xc - x0) xd = x0 - (xd - x1) r += [ B.FillBetween([xc, xa, xb, xd], [yc, ya, yb, yd], [xc, xd], [yc, yd], **kw) ] vy += sep return r
def plot_species(species_log): ''' Visualizes speciation throughout evolution. ''' if has_biggles: plot = biggles.FramedPlot() plot.title = "Speciation" plot.ylabel = r"Size per Species" plot.xlabel = r"Generations" generation = [i for i in xrange(len(species_log))] species = [] curves = [] for gen in xrange(len(generation)): for j in xrange(len(species_log), 0, -1): try: species.append(species_log[-j][gen] + sum(species_log[-j][:gen])) except IndexError: species.append(sum(species_log[-j][:gen])) curves.append(species) species = [] s1 = biggles.Curve(generation, curves[0]) plot.add(s1) plot.add( biggles.FillBetween(generation, [0] * len(generation), generation, curves[0], color=random.randint(0, 90000))) for i in range(1, len(curves)): c = biggles.Curve(generation, curves[i]) plot.add(c) plot.add( biggles.FillBetween(generation, curves[i - 1], generation, curves[i], color=random.randint(0, 90000))) plot.write_img(1024, 800, 'speciation.svg') else: print('You dot not have the Biggles package.')
def test_example1(self): x = numpy.arange(0, 3 * numpy.pi, numpy.pi / 30) c = numpy.cos(x) s = numpy.sin(x) p = biggles.FramedPlot() p.title = "title" p.xlabel = r"$x$" p.ylabel = r"$\Theta$" p.add(biggles.FillBetween(x, c, x, s)) p.add(biggles.Curve(x, c, color="red")) p.add(biggles.Curve(x, s, color="blue")) _write_example(1, p)
def box_fill( x0, y0, x1, y1, **kw ): """ Fill for a rectangle described by x0,y0, x1,y1 (lower left and upper right corner). @param x0: rectangular coorinates @type x0: float @param y0: rectangular coorinates @type y0: float @param x1: rectangular coorinates @type x1: float @param y1: rectangular coorinates @type y1: float @param kw: additional key-value pairs @type kw: key=value @return: biggles plot object @rtype: biggles.FillBetween """ return B.FillBetween( [x0,x1,x1],[y0,y0,y1],[x1,x0,x0],[y1,y1,y0], **kw)
def solid_fill( x0, y0, x1, y1, **kw ): """ Fill the rectangle described by x0,y0, x1,y1 (lower left and upper right corner). @param x0: rectangular coorinates @type x0: float @param y0: rectangular coorinates @type y0: float @param x1: rectangular coorinates @type x1: float @param y1: rectangular coorinates @type y1: float @param kw: additional key-value pairs @type kw: key=value @return: list of biggles plot objects @rtype: [Biggles.FillBetween] """ return [ B.FillBetween( [x0,x0,x1,x1], [y1,y0,y0,y1], [x0,x1], [y1,y1], filltype=1, **kw) ]
def bar_fill(x0, y0, x1, y1, sep=0.1, size=0.06, color='grey', **kw): """ Fill the rectangle described by x0, y0, x1, y1 with horizontal bars. @param x0: rectangular coorinates @type x0: float @param y0: rectangular coorinates @type y0: float @param x1: rectangular coorinates @type x1: float @param y1: rectangular coorinates @type y1: float @param sep: separation between lines (default: 0.1) @type sep: float @param size: line thickness (default: 0.06) @type size: float @param color: color name (default: grey) @type color: str @param kw: additional key-value pairs @type kw: key=value @return: list of biggles plot objects @rtype: [Biggles.FillBetween] """ kw.update({'color': color}) r = [] y = y1 while y >= y0: y_low = y - size if y_low < y0: y_low = y0 r += [ B.FillBetween([x0, x1], [y, y], [x0, x0, x1, x1], [y, y_low, y_low, y], **kw) ] y -= sep return r
def __init__(self, values): """ @param values: color mapping for each color used @type values: [(float, int)] """ if not biggles: raise ImportError, 'biggles module could not be imported.' FramedPlot.__init__(self) values = N0.array(values) self.frame.draw_spine = 1 n_values = 4 ## number of labeled ticks in legend step = len(values) / (n_values - 1) + 1 indices = range(0, len(values), step) indices.append(len(values) - 1) labels = ['%.1f' % values[i, 0] for i in indices] self.y.ticks = len(labels) self.y1.ticklabels = labels self.y2.draw_ticks = 0 self.x.draw_ticks = 0 self.x.ticklabels = [] i = 2 x = (2, 3) for value, color in values: y1 = (i, i) y2 = (i + 1, i + 1) cell = biggles.FillBetween(x, y1, x, y2, color=int(color)) self.add(cell) i += 1
#!/usr/bin/env python import sys sys.path.insert(1, '..') import biggles import numpy, math x = numpy.arange(0, 3 * math.pi, math.pi / 30) c = numpy.cos(x) s = numpy.sin(x) p = biggles.FramedPlot() p.title = "title" p.xlabel = r"$x$" p.ylabel = r"$\Theta$" p.add(biggles.FillBetween(x, c, x, s)) p.add(biggles.Curve(x, c, color="red")) p.add(biggles.Curve(x, s, color="blue")) #p.write_img( 400, 400, "example1.png" ) #p.write_eps( "example1.eps" ) p.show()
def __init__(self, matrix, mesh=0, palette="plasma", legend=0, step=1, vmin=None, vmax=None): """ @param matrix: the 2-D array to plot @type matrix: array @param mesh: create a plot with a dotted mesh @type mesh: 1|0 @param palette: color palette name see L{Biskit.ColorSpectrum} @type palette: str @param legend: create a legend (scale) showing the walues of the different colors in the plot. @type legend: 1|0 @param step: reduce matrix -- take only each step position in x and y @type step: int @param vmin: override minimal value, all values below will revert to default color @return: biggles plot object, view with biggles.FramedPlot.show() or save with biggles.FramedPlot.write_eps(file_name). @rtype: biggles.FramedPlot """ if not biggles: raise ImportError, 'biggles module could not be imported.' FramedPlot.__init__(self) if step != 1: matrix = self.__thinarray(matrix, step) if vmin is None: vmin = N0.amin(matrix) if vmax is None: vmax = N0.amax(matrix) self.palette = ColorSpectrum(palette, vmin=vmin, vmax=vmax) self.matrix = self.palette.color_array(matrix, resetLimits=0) s = N0.shape(self.matrix) for i in range(s[0]): for j in range(s[1]): col = self.matrix[i, j] x1 = (j, j + 1) y1 = (i, i) y2 = (i + 1, i + 1) cell = biggles.FillBetween(x1, y1, x1, y2, color=col) self.add(cell) if mesh: for i in range(s[0] + 1): self.add(biggles.LineY(i, linetype='dotted')) for i in range(s[1] + 1): self.add(biggles.LineX(i, linetype='dotted')) if legend: legend = self.__make_legend() self.add(legend) self.add(biggles.PlotBox((-0.17, -0.1), (1.25, 1.1))) self.aspect_ratio = 1.0
print("sim m: %g +/- %g alpha: %g +/- %g" % (sim_m, sim_merr, sim_alpha, sim_alphaerr)) simpts.label = 'image simulation' #perr = biggles.SymmetricErrorBarsY(shears, m, merr) #c = biggles.Curve(xp,fitter(xp),color='blue') c = biggles.Curve(xp, xp**2 / units, color=mcolor, type='shortdashed') #c.label=r'$%.2f + %.2f*g^2$' % (fm, falpha) c.label = r'$1.0 \gamma^2$' simc = biggles.Curve(xp, sim_m + sim_alpha / units * xp**2, color=simcolor) #c.label=r'$%.2f + %.2f*g^2$' % (fm, falpha) simc.label = r'$%.1f \gamma^2$' % sim_alpha key = biggles.PlotKey(0.1, 0.9, [pts, c, simpts, simc], halign='left') ax = numpy.array([-0.1, 0.2]) z = biggles.Curve(ax, ax * 0) allowed = biggles.FillBetween(ax, [-1.0e-3 / units] * 2, ax, [1.0e-3 / units] * 2, color='gray90') plt.add(allowed, z, c, simc, pts, ptsc, simpts, simerr, simptsc, key) #plt.show() plt.write_eps('results-noise0-m0.6.eps')
def test_pqr_shear_recovery(self, smin, smax, nshear, npair=10000, h=1.e-6, eps=None, expand_shear=None): """ Test how well we recover the shear with no noise. parameters ---------- smin: float min shear to test smax: float max shear to test nshear: number of shear values to test npair: integer, optional Number of pairs to use at each shear test value """ import lensing from .shape import Shape, shear_reduced shear1_true = numpy.linspace(smin, smax, nshear) shear2_true = numpy.zeros(nshear) shear1_meas = numpy.zeros(nshear) shear2_meas = numpy.zeros(nshear) # _te means expanded around truth shear1_meas_te = numpy.zeros(nshear) shear2_meas_te = numpy.zeros(nshear) theta = numpy.pi / 2.0 twotheta = 2.0 * theta cos2angle = numpy.cos(twotheta) sin2angle = numpy.sin(twotheta) # extra dim because working in 2d samples = numpy.zeros((npair * 2, self.ndim + 1)) g = numpy.zeros(npair) g1 = numpy.zeros(npair * 2) g2 = numpy.zeros(npair * 2) for ishear in xrange(nshear): s1 = shear1_true[ishear] s2 = shear2_true[ishear] tsamples = self.sample2d(npair) g1samp = tsamples[:, 0] g2samp = tsamples[:, 1] g1[0:npair] = g1samp g2[0:npair] = g2samp # ring test, rotated by pi/2 g1[npair:] = g1samp * cos2angle + g2samp * sin2angle g2[npair:] = -g1samp * sin2angle + g2samp * cos2angle # now shear all g1s, g2s = shear_reduced(g1, g2, s1, s2) #g=numpy.sqrt(g1s**2 + g2s**2) #print("gmin:",g.min(),"gmax:",g.max()) samples[:, 0] = g1s samples[:, 1] = g2s samples[0:npair, 2:] = tsamples[:, 2:] samples[npair:, 2:] = tsamples[:, 2:] P, Q, R = self.get_pqr_num(samples, h=h) if expand_shear is not None: s1expand = expand_shear[0] s2expand = expand_shear[1] else: s1expand = s1 s2expand = s2 P_te, Q_te, R_te = self.get_pqr_num(samples, s1=s1expand, s2=s2expand, h=h) g1g2, C = lensing.pqr.get_pqr_shear(P, Q, R) g1g2_te, C_te = lensing.pqr.get_pqr_shear(P_te, Q_te, R_te) #g1g2_te[0] += s1 #g1g2_te[1] += s2 shear1_meas[ishear] = g1g2[0] shear2_meas[ishear] = g1g2[1] shear1_meas_te[ishear] = g1g2_te[0] shear2_meas_te[ishear] = g1g2_te[1] mess = 'true: %.6f,%.6f meas: %.6f,%.6f expand true: %.6f,%.6f' print(mess % (s1, s2, g1g2[0], g1g2[1], g1g2_te[0], g1g2_te[1])) fracdiff = shear1_meas / shear1_true - 1 fracdiff_te = shear1_meas_te / shear1_true - 1 if eps: import biggles biggles.configure('default', 'fontsize_min', 3) plt = biggles.FramedPlot() #plt.xlabel=r'$\gamma_{true}$' #plt.ylabel=r'$\Delta \gamma/\gamma$' plt.xlabel = r'$g_{true}$' plt.ylabel = r'$\Delta g/g$' plt.aspect_ratio = 1.0 plt.add( biggles.FillBetween([0.0, smax], [0.004, 0.004], [0.0, smax], [0.000, 0.000], color='grey90')) plt.add( biggles.FillBetween([0.0, smax], [0.002, 0.002], [0.0, smax], [0.000, 0.000], color='grey80')) psize = 2.25 pts = biggles.Points(shear1_true, fracdiff, type='filled circle', size=psize, color='blue') pts.label = 'expand shear=0' plt.add(pts) pts_te = biggles.Points(shear1_true, fracdiff_te, type='filled diamond', size=psize, color='red') pts_te.label = 'expand shear=true' plt.add(pts_te) if nshear > 1: coeffs = numpy.polyfit(shear1_true, fracdiff, 2) poly = numpy.poly1d(coeffs) curve = biggles.Curve(shear1_true, poly(shear1_true), type='solid', color='black') #curve.label=r'$\Delta \gamma/\gamma~\propto~\gamma^2$' curve.label = r'$\Delta g/g = 1.9 g^2$' plt.add(curve) plt.add( biggles.PlotKey(0.1, 0.9, [pts, pts_te, curve], halign='left')) print(poly) print('writing:', eps) plt.write_eps(eps)