def state(x0, y0, r, c): x1 = x0 - r / sqrt(2) y1 = y0 + r / sqrt(2) x2 = x0 - 3 * r / 4 / sqrt(2) y2 = y1 + r / 8 x3 = x0 - r / 4 / sqrt(2) y3 = y1 - r / 8 x4 = x0 + r / 4 / sqrt(2) y4 = y1 + r / 8 x5 = x0 + 3 * r / 4 / sqrt(2) y5 = y1 - r / 8 x6 = x0 + r / sqrt(2) y6 = y1 delta = r / 40 c.fill(path.circle(x0, y0, r), [color.cmyk.RoyalBlue]) c.fill(path.rect(x1 + delta, y0 + delta, r * sqrt(2) - 2 * delta, r), [color.rgb.white]) inside = path.curve(x1, y1, x2, y2, x3, y3, x0, y1) << \ path.curve(x0, y1, x4, y4, x5, y5, x6, y6) << \ path.line(x6, y6, x6, y0) << \ path.line(x6, y0, x1, y0) inside.append(path.closepath()) c.fill(inside, [color.cmyk.RoyalBlue]) c.stroke(path.circle(x0, y0, r), [style.linewidth(r / 25)])
def array34(arange, hlshape=None): c = canvas.canvas() if hlshape is None: c.text(2, 3.3, 'shape=(3, 4)', [text.halign.center]) else: c.text(2, 3.3, 'shape=%s' % repr(hlshape), [text.halign.center]) if hlshape is not None: if len(hlshape) == 1: hlshape = (1, hlshape[0]) if arange: gridcolor = color.grey(0) else: gridcolor = color.grey(0.5) if hlshape is None: arange = True elif (hlshape[0] in (1, 3)) and (hlshape[1] in (1, 4)): arange = False else: arange = None drawgrid(c, 4, 3, 0, gridcolor, arange=arange) if hlshape is not None: c.stroke(path.rect(0, 3, hlshape[1], -hlshape[0]), [deco.filled([color.rgb(1, 0.8, 0.4)])]) drawgrid(c, hlshape[1], hlshape[0], 3 - hlshape[0], arange=False) if arange is None: alertcolor = color.rgb(0.6, 0, 0) c.stroke(path.line(0, 0, 4, 3), [alertcolor, style.linewidth.Thick]) c.stroke(path.line(0, 3, 4, 0), [alertcolor, style.linewidth.Thick]) return c
def draw_sector(self, end): """draw the sector""" segment = path.path( path.arc(self.xo, self.yo, self.inner_r, self.start_angle, self.end_angle), path.arcn(self.xo, self.yo, self.sector_width + self.inner_r, self.end_angle, self.start_angle), path.closepath()) self.shape_canvas.fill(segment, [self.sector_color]) # draw a delimiting line between sectors line_color = color.gray(0.15) if end and (self.end_angle - self.start_angle) < 0.25: line_color = color.rgb.red r = self.inner_r + self.sector_width start_radians = self.start_angle * pi / 180.0 end_radians = self.end_angle * pi / 180.0 x0 = self.inner_r * cos(start_radians) + self.xo y0 = self.inner_r * sin(start_radians) + self.yo x1 = r * cos(start_radians) + self.xo y1 = r * sin(start_radians) + self.yo self.shape_canvas.stroke(path.line(x0, y0, x1, y1), [style.linewidth(0.01), line_color]) x0 = self.inner_r * cos(end_radians) + self.xo y0 = self.inner_r * sin(end_radians) + self.yo x1 = r * cos(end_radians) + self.xo y1 = r * sin(end_radians) + self.yo self.shape_canvas.stroke(path.line(x0, y0, x1, y1), [style.linewidth(0.01), line_color])
def draw_matrix(M, flow=None): st_text = center rows = M.rows cols = M.cols dx = 0.5 dy = 0.5 H = len(rows) * dy W = len(cols) * dx layout = {} for i, col in enumerate(cols): x, y = i * dx, H + dy layout[col] = x, y cvs.text(x, y, "$%s$" % col, st_text) for j, row in enumerate(rows): x, y = -dx, H - j * dy layout[row] = x, y cvs.text(x, y, "$%s$" % row, st_text) cvs.stroke(path.line(-1.4 * dx, H + 0.5 * dy, W - 0.5 * dx, H + 0.5 * dy)) cvs.stroke(path.line(-0.5 * dx, 0.6 * dy, -0.5 * dx, H + 1.4 * dy)) for i, col in enumerate(cols): for j, row in enumerate(rows): value = M[row, col] if value == 0: c = '.' else: c = str(value) x, y = i * dx, H - j * dy layout[row, col] = x, y cvs.text(x, y, "$%s$" % c, st_text) if flow is None: return r = 0.2 critical = flow.get_critical() for item in rows + cols: if item in critical: x, y = layout[item] cvs.stroke(path.circle(x, y, r), st_thick + [red]) for key in flow.get_pairs(): if key not in layout: continue x, y = layout[key] cvs.stroke(path.circle(x, y, r), st_thick + [orange])
def arrow(x0, y0, x1, y1): alpha = 0.3 beta = 0.5 x2 = x1 + (x1 - x0) * alpha y2 = y1 + (y1 - y0) * alpha x3 = x2 + (y1 - y0) * beta y3 = y2 + (x0 - x1) * beta x4 = x2 - (y1 - y0) * beta y4 = y2 - (x0 - x1) * beta arrow = path.line(x0, y0, x3, y3) << path.line( x3, y3, x1, y1) << path.line(x1, y1, x4, y4) arrow.append(path.closepath()) return arrow
def drawgrid(c, nxcells, nycells, yoff, gridcolor=color.grey(0), arange=None): c.stroke(path.rect(0, yoff, nxcells, nycells), [gridcolor]) for nx in range(nxcells-1): c.stroke(path.line(nx+1, yoff, nx+1, yoff+nycells), [gridcolor]) for ny in range(nycells-1): c.stroke(path.line(0, yoff+ny+1, nxcells, yoff+ny+1), [gridcolor]) entry = '1' if arange is not None: for nx in range(nxcells): for ny in range(nycells): if arange: entry = str(4*ny+nx) c.text(nx+0.5, 2.5-ny, entry, [text.halign.center, text.valign.middle, gridcolor])
def frontplane(z, nxmax, mymax, facecolor, edgecolor, trans): p = path.path(path.moveto(*projector(0, z, 0)), path.lineto(*projector(nxmax, z, 0)), path.lineto(*projector(nxmax, z, nymax)), path.lineto(*projector(0, z, nymax)), path.closepath()) c.fill(p, [facecolor, color.transparency(trans)]) c.stroke(p, [edgecolor]) for nx in range(1, nxmax): x0, y0 = projector(nx, z, 0) x1, y1 = projector(nx, z, nymax) c.stroke(path.line(x0, y0, x1, y1), [edgecolor]) for ny in range(1, nymax): x0, y0 = projector(0, z, ny) x1, y1 = projector(nxmax, z, ny) c.stroke(path.line(x0, y0, x1, y1), [edgecolor])
def arraygraphics(a, idxstr, title=True, xscale=1.0, fgcolor=color.grey(1), bgcolor=color.hsb(0.9, 1, 0.5)): """create a graphical representation of a two-dimensional array a array containing the data to be shown slicestr string defining the slice to be highlighted xscale PyX scaling for text fgcolor color of highlighted data bgcolor color of highlighted cells """ assert a.ndim == 2 n0, n1 = a.shape highlighted = np.zeros_like(a, dtype=bool) exec("highlighted{} = True".format(idxstr)) unit.set(xscale=xscale) text.set(text.LatexRunner) text.preamble( r'\usepackage[sfdefault,scaled=.85,lining]{FiraSans}\usepackage{newtxsf}' ) c = canvas.canvas() for ny, nx in zip(*np.nonzero(highlighted)): c.fill(path.rect(nx, n0 - ny, 1, -1), [bgcolor]) c.stroke(path.rect(0, 0, n1, n0)) for nx in range(1, n1): c.stroke(path.line(nx, 0, nx, n0)) for ny in range(1, n0): c.stroke(path.line(0, ny, n1, ny)) textcentered = [text.halign.center, text.valign.middle] textcentered_highlighted = textcentered + [fgcolor] for nx in range(n1): for ny in range(n0): if highlighted[ny, nx]: textattrs = textcentered_highlighted else: textattrs = textcentered c.text(nx + 0.5, n0 - ny - 0.5, a[ny, nx], textattrs) if title: textcolor = bgcolor else: textcolor = color.grey(1) titlestr = r"\Large a" + idxstr.replace('%', '\%') c.text(0.5 * n1, n0 + 0.4, titlestr, [text.halign.center, textcolor]) return c
def draw_config(name): c = canvas.canvas() config = configs[name] ysize = len(config) xsize = len(config[0]) for nx in range(xsize): for ny in range(ysize): if config[ny][nx]: c.fill(path.rect(nx, ysize-ny, 1, -1)) strokecolor = color.grey(0.6) c.stroke(path.rect(0, 0, xsize, ysize), [strokecolor]) for nx in range(1, xsize): c.stroke(path.line(nx, 0, nx, ysize), [strokecolor]) for ny in range(1, ysize): c.stroke(path.line(0, ny, xsize, ny), [strokecolor]) return c
def plate_circle_layer(plate, information, message, plate_radius=32.6): outerclip = 39.6 box_xoffset = 1. box_xsize = 4. box_ysize = 16. text_ybuffer = 0.5 text_xbuffer = 0.3 clippath = path.circle(0., 0., outerclip) plate_clipobject = canvas.clip(clippath) plate_circle = canvas.canvas([plate_clipobject]) plate_circle.stroke(path.circle(0., 0., plate_radius), [style.linewidth.THICk]) plate_circle.stroke( path.line(-plate_radius + box_xoffset, -box_ysize * 0.5, -plate_radius + box_xoffset - box_xsize, -box_ysize * 0.5), [style.linewidth.THICk]) plate_circle.stroke( path.line(-plate_radius + box_xoffset - box_xsize, -box_ysize * 0.5, -plate_radius + box_xoffset - box_xsize, box_ysize * 0.5), [style.linewidth.THICk]) plate_circle.stroke( path.line(-plate_radius + box_xoffset, box_ysize * 0.5, -plate_radius + box_xoffset - box_xsize, box_ysize * 0.5), [style.linewidth.THICk]) tab_path = path.line(-plate_radius - text_xbuffer, -box_ysize * 0.5 + text_ybuffer, -plate_radius - text_xbuffer, box_ysize * 0.5 - text_ybuffer) tab_text = r"\font\myfont=cmr10 at 100pt {\myfont " + str(plate) + "}" plate_circle.draw(tab_path, [deco.curvedtext(tab_text)]) information_path = (path.circle(0., 0., plate_radius).transformed( trafo.rotate(90.))) plate_circle.draw(information_path, [ deco.curvedtext(information, textattrs=[text.valign.top, text.vshift.topzero]) ]) message_path = (path.circle(0., 0., plate_radius).transformed(trafo.rotate(-90.))) plate_circle.draw(message_path, [ deco.curvedtext(message, textattrs=[text.valign.top, text.vshift.topzero]) ]) return plate_circle
def edge(p, q): d = distance(p, q) / radius v = [(p[i] - q[i]) / d for i in (0, 1, 2)] p = [p[i] - v[i] for i in (0, 1, 2)] q = [q[i] + v[i] for i in (0, 1, 2)] lx1, ly1 = perspective(p) lx2, ly2 = perspective(q) c.stroke(path.line(lx1, ly1, lx2, ly2), edgeColor)
def draw_drop(x, y, r, c): c.fill(path.circle(x, y - 3 * r, r), [color.cmyk.RoyalBlue]) c.stroke(path.circle(x, y - 3 * r, r)) triangle = path.line( x, y, x + sqrt(8) * r / 3, y - 8 * r / 3) << path.line( x + sqrt(8) * r / 3, y - 8 * r / 3, x - sqrt(8) * r / 3, y - 8 * r / 3) triangle.append(path.closepath()) c.fill(triangle, [color.cmyk.RoyalBlue]) c.stroke( path.line(x - sqrt(8) * r / 3, y - 8 * r / 3, x, y) << path.line(x, y, x + sqrt(8) * r / 3, y - 8 * r / 3)) c.fill(path.circle(x - 0.5 * r, (y - 3 * r) / 2, 0.2 * r), [color.rgb.white, trafo.scale(sx=1, sy=2)])
def draw(self): """draw the text!!""" for layer in self.sectors: radius = self.sectors[layer]['radius'] letter_radius = radius + 0.25 * self.sector_width prev_radian = 0 for radian in self.sectors[layer]['letters']: end = False letter = self.sectors[layer]['letters'][radian][0] if len(letter) > 1: end = True freq = self.sectors[layer]['letters'][radian][1] offset = False cur_radian = radian if (radian - prev_radian) * letter_radius < 0.22: cur_radian = prev_radian + 0.22 / letter_radius prev_radian = cur_radian offset = True centroid_x = radius * cos(radian) + self.xo centroid_y = radius * sin(radian) + self.yo if end: letter_x = radius * cos(cur_radian) + self.xo letter_y = radius * sin(cur_radian) + self.yo else: letter_x = letter_radius * cos(cur_radian) + self.xo letter_y = letter_radius * sin(cur_radian) + self.yo # rotate the text accordingly transform = trafo.rotate(radian * 180 / pi) if cur_radian > pi / 2 and cur_radian < (3 * pi / 2): transform = trafo.rotate(180 + radian * 180 / pi) # if this condition is true then the letter is actually the # whole word, it is an end sector, so display the frequency if len(letter) > 1: letter += ' ' letter += str(freq) # the random floats are me tuning the color just right lol text_color = color.rgb(0, 0.0784 * 1.4, 0.156 * 1.4) self.canvas.text( letter_x, letter_y, r"\texttt{" + letter + '}', [ text.halign.center, text.valign.middle, transform, text.size.scriptsize, text_color ]) if offset: self.canvas.stroke( path.line(centroid_x, centroid_y, letter_x, letter_y), [style.linewidth(0.0035), text_color]) self.canvas.fill(path.circle(centroid_x, centroid_y, 0.0065), [text_color]) prev_radian = cur_radian
def outline_layer(): tab_xsize = 1.3 tab_ysize = 2.0 clippath = path.circle(0., 0., limit_radius) clipobject = canvas.clip(clippath) outline = canvas.canvas([clipobject]) outline.stroke(path.circle(0., 0., full_radius), [style.linewidth.thick]) outline.stroke( path.line(-full_radius, -tab_ysize * 0.5, -full_radius - tab_xsize, -tab_ysize * 0.5), [style.linewidth.thick]) outline.stroke( path.line(-full_radius, tab_ysize * 0.5, -full_radius - tab_xsize, tab_ysize * 0.5), [style.linewidth.thick]) outline.stroke( path.line(-full_radius - tab_xsize, -tab_ysize * 0.5, -full_radius - tab_xsize, tab_ysize * 0.5), [style.linewidth.thick]) outline.stroke(path.line(-full_radius - tab_xsize, 0., -limit_radius, 0.), [style.linewidth.thick]) return outline
def pLine(canv, pt, r, fi, linewidth=LINE_DEF_WIDTH, marks=False): pta = pt ptb = pPoint(r, fi, pt) line = path.line(pta.x, pta.y, ptb.x, ptb.y) canv.stroke(line, [style.linewidth(linewidth)]) if marks: pta.mark(canv) ptb.mark(canv)
def Line(canv, pta, ptb, linewidth=LINE_DEF_WIDTH, color=LINE_DEF_COLOR, marks=False): line = path.line(pta.x, pta.y, ptb.x, ptb.y) canv.stroke(line, [style.linewidth(linewidth), color]) if marks: pta.mark(canv) ptb.mark(canv)
def draw_lines_to_canvas(c, lines: torch.Tensor, lw=1, lcols=None): if lcols is not None: lcols = lcols.detach().cpu() if lines is not None: lines = lines.detach().cpu() for i in range(lines.shape[0]): _lw = lw[i] if isinstance(lw, torch.Tensor) else lw if _lw > 0: if lcols is None: c.stroke( path.line(lines[i, 0, 1], -lines[i, 0, 0], lines[i, 1, 1], -lines[i, 1, 0]), [style.linewidth(_lw), style.linecap.round]) else: c.stroke( path.line(lines[i, 0, 1], -lines[i, 0, 0], lines[i, 1, 1], -lines[i, 1, 0]), [ style.linewidth(_lw), style.linecap.round, color.rgb(*lcols[i]) ])
def makePath(self): ''' makes pyx path object from pygame points ''' # so we can use pop() w/o modifying self.positions positions = copy.copy(self.points) positions.reverse() p = None # BEZIER CURVES while len( positions) > 4: # need at least 4 points to make bezier curve # make a bezier curve points = [] for _ in range(3): points += positions.pop() points += positions[-1] # so next curve will start at this point if p: p += path.curve(*points) else: p = path.curve(*points) # LINES while len(positions) > 1: # use up rest of points with lines points = [] points += positions.pop() points += positions[-1] if p: p += path.line(*points) else: p = path.line(*points) # store curve in object p = deformer.smoothed(2.0).deform(p) # smooth curve self.path = p self.arclen = p.arclen_pt() * metersPerPoint self.duration = self.arclen / self.velocity
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)
def plot_single_range(data, binsize, width, height): plot_width = width plot_height = height c = canvas.canvas() xmax = 0.0 methods = data.keys() methods.sort() for method in methods: where = numpy.where((data[method]['binsize'] == binsize) * (data[method]['interaction'] == 'cis') * (data[method]['range'] > 0)) if where[0].shape[0] > 0: xmax = max(xmax, numpy.amax(data[method]['range'][where])) X = data[method]['range'][where] X = numpy.r_[0, X] X[0] = X[1]**2.0 / X[2] xmin = X[0] g = graph.graphxy(width=plot_width, height=plot_height, x=graph.axis.log(painter=painter, min=X[0], max=xmax), y=graph.axis.lin(painter=painter), x2=graph.axis.lin(parter=None, min=0, max=1), y2=graph.axis.lin(parter=None, min=0, max=1)) for x in X[1:-1]: pos = ((log(x) - log(xmin)) / (log(xmax) - log(xmin)) * plot_width) g.stroke(path.line(pos, 0, pos, plot_height), [style.linestyle.dotted, style.linewidth.THin]) X = (X[1:]**0.5) * (X[:-1]**0.5) for method in methods: where = numpy.where((data[method]['binsize'] == binsize) * (data[method]['interaction'] == 'cis') * (data[method]['range'] > 0)) if where[0].shape[0] > 0: Y = data[method]['correlation'][where] g.plot(graph.data.points(zip(X, Y), x=1, y=2), [ graph.style.line( lineattrs=[method_colors[method], style.linewidth.Thick]) ]) if binsize / 1000000 > 0: binstring = "%iMb" % (binsize / 1000000) elif binsize / 1000 > 0: binstring = "%iKb" % (binsize / 1000) else: binstring = str(binsize) g.text(plot_width / 2, plot_height + 0.3, "%s binning" % (binstring), [text.halign.center, text.valign.top, text.size(-2)]) c.insert(g) return c
def draw_line_segments(lines: torch.Tensor, filename, lw=1, svg=False): c = canvas.canvas() lines = lines.detach().cpu() lw = style.linewidth(lw) for i in range(lines.shape[0]): c.stroke( path.line(lines[i, 0, 1], -lines[i, 0, 0], lines[i, 1, 1], -lines[i, 1, 0]), [lw, style.linecap.round]) if svg: c.writeSVGfile(file=filename) else: c.writePDFfile(file=filename)
def plot_correlation_diffs(corr1, corr2, name, width, height): ho = 1.2 vo = 0.4 plot_width = width - ho plot_height = height - vo diffs = {} ymin = numpy.inf ymax = -numpy.inf for n in ['Phillips', 'Nora']: for meth in meth_names.keys(): cname = "%s_%s" % (meth, n) diff = corr2[cname] - corr1[cname] ymin = min(ymin, diff) ymax = max(ymax, diff) for meth in meth_names.keys(): cname = "%s_%s" % (meth, name) diffs[meth] = corr2[cname] - corr1[cname] yspan = ymax - ymin ymin -= yspan * 0.05 ymax += yspan * 0.05 yspan = ymax - ymin c = canvas.canvas() g = graph.graphxy( width=plot_width, height=plot_height, x=graph.axis.bar(painter=graph.axis.painter.bar(nameattrs=None)), y=graph.axis.lin(painter=painter, min=ymin, max=ymax), x2=graph.axis.lin(parter=None, min=0, max=1), y2=graph.axis.lin(parter=None, min=0, max=1)) w = plot_width / float(len(meth_names) + 1) y0 = -ymin / yspan * plot_height for i, meth in enumerate(methods): col = method_colors[meth] g.stroke( path.rect((i + 0.5) * w, y0, w, diffs[meth] / yspan * plot_height), [deco.filled([col])]) g.stroke(path.line(0, y0, plot_width, y0), [style.linestyle.dotted, style.linewidth.THin]) c.insert(g, [trafo.translate(ho, vo)]) c.text( 0, plot_height * 0.5 + vo, r"$r_{0K} - r_{50K}$", [text.halign.center, text.valign.top, text.size(-3), trafo.rotate(90)]) c.text(plot_width * 0.5 + ho, vo * 0.5, name, [text.halign.center, text.valign.middle, text.size(-3)]) return c
def corner(nx, ny, z, facecolor, edgecolor, trans, xdir, ydir): if xdir: p = path.path(path.moveto(*projector(nx, z, ny)), path.lineto(*projector(nx - 1, z, ny)), path.lineto(*projector(nx - 1, z + 1, ny)), path.lineto(*projector(nx, z + 1, ny)), path.closepath()) c.fill(p, [facecolor, color.transparency(trans)]) if ydir: p = path.path(path.moveto(*projector(nx, z, ny)), path.lineto(*projector(nx, z, ny + 1)), path.lineto(*projector(nx, z + 1, ny + 1)), path.lineto(*projector(nx, z + 1, ny)), path.closepath()) c.fill(p, [facecolor, color.transparency(trans)]) x0, y0 = projector(nx, z, ny) x1, y1 = projector(nx, z + 1, ny) c.stroke(path.line(x0, y0, x1, y1), [edgecolor])
def draw_graph(graph, pts=None, name="output"): import pyx from pyx import path, deco, trafo, style, text, color, deformer from pyx.color import rgb, cmyk from pyx.color import rgbfromhexstring as rgbhex black = rgb(0., 0., 0.) blue = rgb(0., 0., 0.8) lred = rgb(1., 0.4, 0.4) red = rgb(1., 0.0, 0.0) white = rgb(1., 1., 1.) if pts is None: pts = pos_circ(graph) directed = isinstance(graph, (nx.DiGraph, nx.MultiDiGraph)) #W = 10. #H = 10. R = 1.5 r = 0.2 c = pyx.canvas.canvas() for edge in graph.edges(): src, tgt = edge x0, y0 = pts[src] x1, y1 = pts[tgt] color = black if edge in edge_colors: color = eval(edge_colors[edge]) c.stroke(path.line(R * x0, R * y0, R * x1, R * y1), [color]) for node in graph.nodes(): x, y = pts[node] p = path.circle(R * x, R * y, r) color = white if node in node_colors: color = eval(node_colors[node]) c.fill(p, [color]) c.stroke(p, [black]) c.writePDFfile(name)
def array(shape): baseshape = (3, 4) bgcolor = color.grey(0.5) c = canvas.canvas() c.text(baseshape[1] / 2, baseshape[0] + 0.3, 'shape=%s' % repr(shape), [text.halign.center]) if len(shape) == 1: shape = (1, shape[0]) assert len(shape) == 2 c.stroke(path.rect(0, 0, baseshape[1], baseshape[0]), [bgcolor]) for nx in range(1, baseshape[1]): c.stroke(path.line(nx, 0, nx, baseshape[0]), [bgcolor]) for ny in range(1, baseshape[0]): c.stroke(path.line(0, ny, baseshape[1], ny), [bgcolor]) if not (shape == baseshape): c.fill(path.rect(0, baseshape[0], shape[1], -shape[0]), [color.rgb(1, 0.8, 0.4)]) if shape[0] in (1, baseshape[0]) and shape[1] in (1, baseshape[1]): for nx in range(baseshape[1]): for ny in range(baseshape[0]): c.text( nx + 0.5, baseshape[0] - ny - 0.5, str(baseshape[1] * min(ny, shape[0] - 1) + min(nx, shape[1] - 1) + 1), [text.halign.center, text.valign.middle, bgcolor]) else: alertcolor = color.rgb(0.6, 0, 0) c.stroke(path.line(0, 0, baseshape[1], baseshape[0]), [alertcolor, style.linewidth.Thick]) c.stroke(path.line(0, baseshape[0], baseshape[1], 0), [alertcolor, style.linewidth.Thick]) else: for nx in range(baseshape[1]): for ny in range(baseshape[0]): c.text(nx + 0.5, baseshape[0] - ny - 0.5, str(baseshape[1] * ny + nx + 1), [text.halign.center, text.valign.middle]) c.stroke(path.rect(0, baseshape[0], shape[1], -shape[0])) for nx in range(1, shape[1]): c.stroke(path.line(nx, baseshape[0], nx, baseshape[0] - shape[0])) for ny in range(1, shape[0]): c.stroke(path.line(0, ny, shape[1], ny)) if not (shape == baseshape): for nx in range(shape[1]): for ny in range(shape[0]): c.text(nx + 0.5, baseshape[0] - ny - 0.5, str(baseshape[1] * ny + nx + 1), [text.halign.center, text.valign.middle]) return c
def plot(self, canvas, other=None): """Plot these directions on a pyx canvas. Plot these directions using standard AMS conventions: Lambert equal-area projection; major direction as a square; intermediate direction as a triangle; minor direction as a circle. If the "other" argument is provided, additionally draw a line from each direction in this PrincipalDirs object to its corresponding direction in the other PrincipalDirs object; this is intended to provide a visual comparison between pairs of PrincipalDirs objects, in particular when the directions are close to one another. """ self.p1.plot(canvas, "s") self.p2.plot(canvas, "t") self.p3.plot(canvas, "c") if other is not None: for p in "p1", "p2", "p3": v1 = getattr(self, p).project() v2 = getattr(other, p).project() canvas.stroke(path.line(v1[0], v1[1], v2[0], v2[1]), [pyx.style.linecap.round])
def winding(n, radius, angle=60, windingnumber=False, endpointcolor=color.rgb(0.6, 0.2, 0), pathcolor=color.rgb(0.4, 0.3, 0.8)): c = canvas.canvas() ticklen = 0.1 c.stroke(path.circle(0, 0, radius), [style.linewidth.Thin]) tick = path.line((1 - ticklen) * radius, 0, (1 + ticklen) * radius, 0) c.stroke(tick, [endpointcolor, style.linewidth.Thick]) c.stroke(tick, [trafo.rotate(angle), endpointcolor, style.linewidth.Thick]) tick_outer = (1 + ticklen) * radius for _angle, label in ((0, r'$\phi_\text{i}$'), (radians(angle), r'$\phi_\text{f}$')): c.text((tick_outer + 0.3) * cos(_angle), (tick_outer + 0.3) * sin(_angle), label, [text.halign.center, text.valign.middle, endpointcolor]) if windingnumber: c.text(0, -radius - 0.3, '$n={}$'.format(n), [text.halign.center, text.valign.top]) c.stroke(spiral(radius, angle, n), [pathcolor, style.linewidth.Thick]) return c
def draw_square(x, y, kante): c.fill(path.rect(x, y, kante, kante), [color.grey(1)]) c.stroke(path.line(x, y, x + kante, y + kante), [style.linewidth.thick, color.grey(0.5)]) c.stroke(path.rect(x, y, kante, kante), [style.linewidth.thick])
text.set(text.LatexRunner) c = canvas.canvas() c.stroke(path.circle(0, 0, 1)) c.stroke(path.circle(5, 0, 1)) c.stroke(path.circle(15, 0, 1)) c.stroke(path.circle(20, 5, 1)) c.stroke(path.circle(9, 0, 0.1), [style.linewidth.THICK]) c.stroke(path.circle(10, 0, 0.1), [style.linewidth.THICK]) c.stroke(path.circle(11, 0, 0.1), [style.linewidth.THICK]) c.text(0, 0, r'\Huge{1}', [text.parbox(0), text.valign.middle, text.halign.center]) c.text(5, 0, r'\Huge{2}', [text.parbox(0), text.valign.middle, text.halign.center]) c.text(15, 0, r'\Huge{K}', [text.parbox(0), text.valign.middle, text.halign.center]) c.text(20, 5, r'\Huge{0}', [text.parbox(0), text.valign.middle, text.halign.center]) c.stroke(path.curve(1 / sqrt(2), 1 / sqrt(2), 2, 2, 3, 2, 5 - 1 / sqrt(2), 1 / sqrt(2)), [deco.earrow(size=1)]) c.stroke(path.curve(5 - 1 / sqrt(2), -1 / sqrt(2), 3, -2, 2, -2, 1 / sqrt(2), -1 / sqrt(2)), [deco.earrow(size=1)]) c.stroke(path.curve(1 / sqrt(2), 1 / sqrt(2), 1, 5, 10, 5, 19, 5), [deco.earrow(size=1)]) c.stroke(path.curve(5 + 1 / sqrt(2), 1 / sqrt(2), 7, 5, 12, 5, 19, 5), [deco.earrow(size=1)]) c.stroke(path.line(15 + 1 / sqrt(2), 1 / sqrt(2), 20 - 1 / sqrt(2), 5 - 1 / sqrt(2)), [deco.earrow(size=1)]) c.text(2.5, 2.5, r'\huge{$p_1^2$}', [text.parbox(0), text.valign.middle, text.halign.center]) c.text(2.5, -1, r'\huge{$p_2^1$}', [text.parbox(0), text.valign.middle, text.halign.center]) c.text(2.5, 4, r'\huge{$p_1^0$}', [text.parbox(0), text.valign.middle, text.halign.center]) c.text(8, 3, r'\huge{$p_2^0$}', [text.parbox(0), text.valign.middle, text.halign.center]) c.text(18, 2, r'\huge{$p_K^0$}', [text.parbox(0), text.valign.middle, text.halign.center]) c.writePDFfile("states.pdf")