def repo(label, colorfg, colorbg): crepo = canvas.canvas() labeltext = text.text(0, 0, r'\textsf{{{}}}'.format(label), [colorfg]) extrawd = 0.15 labelbox = labeltext.bbox().enlarged(extrawd) crepo.fill(labelbox.path(), [colorbg, deformer.smoothed(0.05), deco.stroked([colorfg])]) crepo.insert(labeltext) return crepo
def arrow(g, x_coord_init, y_coord_init, x_coord_final, y_coord_final, size, line_color, stroke_color=None, fill_color=None): """ Function that draws an arrow in a given plot INPUTS: g (Object) A graph-type object to which you want to add the text x_coord_init (Double) x-coordinate (in plot units) at which you want the arrow to start. y_coord_init (Double) y-coordinate (in plot units) at which you want the arrow to start. x_coord_final (Double) x-coordinate (in plot units) at which you want the arrow to end. y_coord_final (Double) y-coordinate (in plot units) at which you want the arrow to end. size (int) Size of the arrow. line_color (instance) Instance color that defines the color of the line of the arrow. stroke_color (instance, optional) Defines the stroke color (default is same as line_color). fill_color (instance, optional) Defines the color fill of the arrow (default is same as line_color). """ if stroke_color is None: stroke_color = line_color if fill_color is None: fill_color = line_color x0, y0 = g.pos(x_coord_init, y_coord_init) xf, yf = g.pos(x_coord_final, y_coord_final) g.stroke(pyx_path.line(x0,y0,xf,yf),\ [pyx_style.linewidth.normal, pyx_style.linestyle.solid, line_color,\ pyx_deco.earrow([pyx_deco.stroked([stroke_color]),\ pyx_deco.filled([fill_color])], size=0.1)])
def earrowLine(canv, pta, ptb, linewidth=LINE_DEF_WIDTH, color=LINE_DEF_COLOR, marks=False): line = path.line(pta.x, pta.y, ptb.x, ptb.y) arrow_type = deco.earrow([deco.stroked([color])], size=10) canv.stroke(line, [style.linewidth(linewidth), color, arrow_type]) if marks: pta.mark(canv)
markercolor = color.rgb(0.7, 0.5, 1.0) utf8markercolor = color.rgb(1.0, 0.7, 0) codecolor = color.rgb(0.7, 1, 0) codepoint = 0x00221E bits = 24 codepointbinary = [(codepoint & 2**n) / 2**n for n in range(bits)] codepointbinary.reverse() size = 0.4 x0 = 0 y0 = 3 dy = 0.07 c.fill(path.rect(x0, y0, 8 * size, size), [markercolor, deco.stroked([markercolor])]) c.stroke(path.rect(x0 + 8 * size, y0, 4 * size, size), [deco.filled([codecolor])]) c.stroke(path.rect(x0 + 12 * size, y0, 6 * size, size), [deco.filled([codecolor])]) c.stroke(path.rect(x0 + 18 * size, y0, 6 * size, size), [deco.filled([codecolor])]) for n in range(len(codepointbinary)): c.text(x0 + (n + 0.5) * size, y0 + dy, r"\sffamily %i" % codepointbinary[n], [text.halign.center]) p = path.path(path.moveto(0.2 * size, size + 0.03), path.lineto(0.2 * size, size + 0.07), path.lineto(3.8 * size, size + 0.07), path.lineto(3.8 * size, size + 0.03)) for n in range(bits // 4):
def pyxColor(string): """Return a pyxColor instance. :param string: RGB color name like 'ffffff' :return: pyx color. >>> assert pyxColor('ffffff') """ assert len(string) == 6 colorTuple = tuple( int('0x' + c, 16) for c in [string[i:i + 2] for i in range(0, 6, 2)]) return color.rgb(*[i / 255.0 for i in colorTuple]) if __name__ == '__main__': # pragma: no cover out = sys.argv[1] if not pyx: sys.exit(1) for icon in set(d['Color'] for d in reader('../cobl-data/cldf/languages.csv', dicts=True)): assert len(icon) == 6 c = canvas.canvas() c.draw( path.circle(20, 20, 13.6), [deco.stroked([linewidth]), deco.filled([pyxColor(icon)])]) stream = c.pipeGS("pngalpha", resolution=20, bbox=bbox.bbox(0, 0, 40, 40)) with open(os.path.join(out, 'c{0}.png'.format(icon)), 'wb') as fp: fp.write(stream.read()) sys.exit(0)
text.set(text.LatexRunner) c = canvas.canvas() codepoint = 0x00E9 bits = 16 codepointbinary = [(codepoint & 2**n)/2**n for n in range(bits)] codepointbinary.reverse() size = 0.4 x0 = 0 y0 = 3 dy = 0.07 c.fill(path.rect(x0, y0, 5*size, size), [color.grey(0.5), deco.stroked([color.grey(0.5)])]) c.stroke(path.rect(x0+5*size, y0, 5*size, size)) c.stroke(path.rect(x0+10*size, y0, 6*size, size)) for n in range(len(codepointbinary)): c.text(x0+(n+0.5)*size, y0+dy, r"\sffamily %i" % codepointbinary[n], [text.halign.center]) p = path.path(path.moveto(0.2*size, size+0.03), path.lineto(0.2*size, size+0.07), path.lineto(3.8*size, size+0.07), path.lineto(3.8*size, size+0.03)) for n in range(bits//4): c.stroke(p, [trafo.translate(4*n*size, y0)]) c.text((4*n+2)*size, size+0.14+y0, r"\sffamily %X" % (codepoint >> (bits//4-n-1)*4 & 0x0f), [text.halign.center])
def pyxColor(string): """ :param string: RGB color name like 'ffffff' :return: pyx color. >>> assert pyxColor('ffffff') """ assert len(string) == 6 colorTuple = tuple( int('0x' + c, 16) for c in [string[i:i + 2] for i in range(0, 6, 2)]) return color.rgb(*[i / 255.0 for i in colorTuple]) if __name__ == '__main__': # pragma: no cover if not pyx: sys.exit(1) asset_resolver = AssetResolver() for icon in ICONS: c = canvas.canvas() c.draw(shapes[icon.name[0]], [ deco.stroked([linewidth]), deco.filled([pyxColor(icon.name[1:])]) ]) stream = c.pipeGS("pngalpha", resolution=20, bbox=bbox.bbox(0, 0, 40, 40)) with open(asset_resolver.resolve(icon.asset_spec).abspath(), 'wb') as fp: fp.write(stream.read()) sys.exit(0)
('staging area', color.hsb(0.2, 1, 0.6), color.rgb(0, 0.5, 0), 'staged'), ('repository (.git)', color.hsb(0.53, 1, 0.6), color.grey(0.3), 'committed'))): xmid = nr * (wd + hdist) + 0.5 * wd c.stroke(path.rect(nr * (wd + hdist), 0, wd, ht), [deformer.smoothed(0.3), boxcolor, style.linewidth.Thick]) c.fill(path.rect(nr * (wd + hdist), ht + vdist, wd, htlabel), [deformer.smoothed(0.3), boxcolor]) c.text(xmid, ht + vdist + 0.5 * htlabel, label, [text.halign.center, text.valign.middle, color.grey(1)]) c.insert(filesymbol(size, symbolcolor), [trafo.translate(xmid, 0.5 * ht)]) c.text(xmid, 0.2 * ht, status, [text.halign.center, symbolcolor]) for nr, operation in enumerate(('git add', 'git commit')): xmid = nr * (wd + hdist) + 0.5 * wd c.stroke( path.line(xmid + 0.5 * size + 0.1, 0.5 * ht, xmid + wd + hdist - 0.5 * size - 0.1, 0.5 * ht), [deco.earrow.large, style.linewidth.Thick]) cop = canvas.canvas() optext = text.text(0, 0, operation, [text.halign.center, text.valign.middle]) tblarge = optext.bbox().enlarged(0.1) cop.fill(tblarge.path(), [deco.stroked([color.grey(0)]), color.grey(0.9)]) cop.insert(optext) c.insert( cop, [trafo.translate((nr + 1) * (wd + hdist) - 0.5 * hdist, 0.5 * ht)]) c.writePDFfile()
\usepackage{newtxsf}''') unit.set(vscale=1.2, wscale=1.3, xscale=1.3) c = canvas.canvas() c.stroke(path.line(0, 0, width, 0), [deco.earrow]) c.text(width + 0.1, 0, '$t$', [text.valign.middle]) c.stroke(path.line(0, lowertick, 0, height), [deco.earrow]) c.text(0.2, height, '$q$', [text.valign.top]) n = 6 for nt in range(n + 1): t = nt * tmax / n c.stroke(path.line(t, 0, t, lowertick)) c.stroke(path.line(t, 0, t, height - 0.26), [style.linestyle.dashed]) seed(424242) positions = [(height - 0.3) * random() for _ in range(n - 1)] positions.append(qf) p = path.path(path.moveto(0, qi)) for nt, q in enumerate(positions): p.append(path.lineto((nt + 1) * tmax / n, q)) c.stroke(p, [style.linejoin.round, color.rgb(0.2, 0, 0.8)]) c.text(0, -0.55, '$0$', [text.halign.center]) c.text(3 * tmax / n, -0.55, '$n{-}1$', [text.halign.center]) c.text(4 * tmax / n, -0.55, '$n$', [text.halign.center]) c.text(tmax, -0.55, '$N$', [text.halign.center]) for x, y in ((0, qi), (tmax, qf)): c.fill(path.circle(x, y, 0.04), [color.grey(1), deco.stroked([color.grey(0)])]) c.text(-0.1, qi, r'$q_\text{i}$', [text.halign.right, text.valign.middle]) c.text(tmax + 0.1, qf, r'$q_\text{f}$', [text.valign.middle]) c.text(width - 0.2, height - 0.26, '$N={}$'.format(n), [text.valign.top]) c.writePDFfile()
from math import exp, expm1 from pyx import color, deco, graph, style, text, unit text.set(text.LatexEngine) mypainter = graph.axis.painter.regular(innerticklength=None, outerticklength=graph.axis.painter.ticklength.normal) g = graph.graphxy(width=8, x=graph.axis.log(title="$x$", painter=mypainter), y=graph.axis.lin(title=r"$f(x)-1$", min=-1.1e-6, max=1.1e-6, painter=mypainter, parter=graph.axis.parter.lin(tickdists=[1e-6])), key=graph.key.key(pos="tr") ) g.plot(graph.data.function("y(x)=(exp(x)-1)/x-1", min=1e-10, max=1e-6, title=r"\textsf{\big(exp(x)-1\big)/x}"), [graph.style.symbol(symbol=graph.style.symbol.circle, size=0.15*unit.v_cm, symbolattrs=[deco.filled([color.rgb(0.2, 0.2, 1)]), deco.stroked([color.grey(1)])])]) g.plot(graph.data.function("y(x)=expm1(x)/x-1", min=1e-10, max=1e-6, title=r"\textsf{expm1(x)/x}", context={"expm1": expm1}), [graph.style.line([color.rgb(1, 0.5, 0), style.linewidth.Thick])]) g.writeGSfile("expm1.png", resolution=300)
text.set(text.LatexRunner) c = canvas.canvas() codepoint = 0x00221E bits = 24 codepointbinary = [(codepoint & 2**n) / 2**n for n in range(bits)] codepointbinary.reverse() size = 0.4 x0 = 0 y0 = 3 dy = 0.07 c.fill(path.rect(x0, y0, 8 * size, size), [color.grey(0.5), deco.stroked([color.grey(0.5)])]) c.stroke(path.rect(x0 + 8 * size, y0, 4 * size, size)) c.stroke(path.rect(x0 + 12 * size, y0, 6 * size, size)) c.stroke(path.rect(x0 + 18 * size, y0, 6 * size, size)) for n in range(len(codepointbinary)): c.text(x0 + (n + 0.5) * size, y0 + dy, r"\sffamily %i" % codepointbinary[n], [text.halign.center]) p = path.path(path.moveto(0.2 * size, size + 0.03), path.lineto(0.2 * size, size + 0.07), path.lineto(3.8 * size, size + 0.07), path.lineto(3.8 * size, size + 0.03)) for n in range(bits // 4): c.stroke(p, [trafo.translate(4 * n * size, y0)]) c.text((4 * n + 2) * size, size + 0.14 + y0, r"\sffamily %X" % (codepoint >> (bits // 4 - n - 1) * 4 & 0x0f),
"f": polygon((2, 36), (38, 36), (20, 5)), # inverted pyramid "d": polygon((20, 2), (38, 20), (20, 38), (2, 20)), # diamond } def pyxColor(string): """ :param string: RGB color name like 'ffffff' :return: pyx color. >>> assert pyxColor('ffffff') """ assert len(string) == 6 colorTuple = tuple(int('0x' + c, 16) for c in [string[i:i+2] for i in range(0, 6, 2)]) return color.rgb(*[i / 255.0 for i in colorTuple]) if __name__ == '__main__': # pragma: no cover if not pyx: sys.exit(1) asset_resolver = AssetResolver() for icon in ICONS: c = canvas.canvas() c.draw( shapes[icon.name[0]], [deco.stroked([linewidth]), deco.filled([pyxColor(icon.name[1:])])]) stream = c.pipeGS("pngalpha", resolution=20, bbox=bbox.bbox(0, 0, 40, 40)) with open(asset_resolver.resolve(icon.asset_spec).abspath(), 'wb') as fp: fp.write(stream.read()) sys.exit(0)
cval = c[nx * nlen + mx, ny * nlen + my] data.append((cval.real, cval.imag, partialdata[mx, my])) procdict[(nx, ny)] = procid procids = set(procdict.values()) colors = [ color.hsb(n / (len(procids) - 1) * 0.67, 1, 1) for n in range(len(procids)) ] proccolors = dict(zip(procids, colors)) text.set(text.LatexRunner) text.preamble(r'\usepackage{arev}\usepackage[T1]{fontenc}') g = graph.graphxy(width=8, height=8, x=graph.axis.lin(title=r'$\mathrm{Re}(c)$'), y=graph.axis.lin(title=r'$\mathrm{Im}(c)$')) g.plot(graph.data.points(data, x=1, y=2, color=3), [graph.style.density(keygraph=None)]) dx = (xmax - xmin) / n dy = (ymax - ymin) / n for k, v in procdict.items(): nx, ny = k tilecolor = proccolors[v] xll, yll = g.pos(xmin + dx * nx, ymin + dy * ny) xur, yur = g.pos(xmin + dx * (nx + 1), ymin + dy * (ny + 1)) g.fill(path.rect(xll, yll, xur - xll, yur - yll), [deco.stroked([color.grey(0)]), tilecolor, color.transparency(0.5)]) g.writePDFfile() # convert to PNG with Gimp to keep transparency
from pyx import color, deco, graph, style, unit unit.set(xscale=1.3) g = graph.graphxy(width=8, x=graph.axis.linear(title="$x$"), y=graph.axis.linear(title="$y$")) g.plot(graph.data.file("pyx1.dat", x=1, y=2), [graph.style.line([style.linestyle.dashed, color.rgb(0, 0, 1)]), graph.style.symbol(graph.style.symbol.circle, size=0.1, symbolattrs=[deco.filled([color.rgb.red]), deco.stroked([color.grey(0)])])]) g.writePDFfile() g.writeGSfile(device="png16m", resolution=800)