def draw_logo_legacy(palette: Palette, file_name: str = 'example.png', add_svg_elements: bool = False, rects_max_n: int = 2, poly_max_n: int = 4, color_style: str = 'smooth', sharpen: float = 0.5, image_size: tuple = (512, 512)): # Create svg surface with cairo.SVGSurface(file_name, image_size[0], image_size[1]) as surface: ctx = cairo.Context(surface) ctx.scale(image_size[0], image_size[1]) """ctx.rectangle(0, 0, 1, 1) c = palette.next() bg = c ctx.set_source_rgb(*c) ctx.fill()""" r = [ cairo.LINE_JOIN_MITER, cairo.LINE_JOIN_BEVEL, cairo.LINE_JOIN_ROUND ] ctx.set_line_join(random.choice(r)) for _ in range(rects_max_n): x, y = random.random(), random.random() ctx.rectangle(x, y, random.random(), random.random()) c = palette.next() ctx.set_source_rgb(*c) ctx.fill() # for _ in range(random.randint(poly_max_n // 2, poly_max_n)): points = [(random.random(), random.random()) for _ in range(random.randint(3, 5))] if random.random() < 0.5: ctx.curve_to(*np.array(points[:3]).flatten()) else: ctx.move_to(*points[0]) for point in points[1:]: ctx.line_to(*point) ctx.close_path() if random.random() < 0.7: ctx.set_source_rgb(*palette.next()) else: gr = GRADIENTS[random.randint(0, len(GRADIENTS))] lg1 = cairo.LinearGradient(*points[0], *points[-1]) lg1.add_color_stop_rgb(0, *map(lambda x: x / 255, gr[0])) lg1.add_color_stop_rgb(0.5, *map(lambda x: x / 255, gr[1])) ctx.set_source(lg1) ctx.set_line_width(random.randint(1, 10) / 100) if random.random() < 0.6: ctx.stroke() else: ctx.fill() Context = cairo.Context(surface) Context.set_font_size(100) Context.select_font_face(random.choice(FONTS), cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_NORMAL) text_x, text_y = random.randint(0, image_size[0] // 4), random.randint( 0 + 200, image_size[1] - 200) Context.move_to(text_x, text_y) Context.set_source_rgb(0, 0, 0) Context.text_path("NTI") svg = vectorize(public_id='yd0cnq0dxzgjscg1afct') path = parse_svg_paths(svg)[-1][9:-3] Context.append_path(cairo.Path(path)) Context.stroke() # Fill text if random.random() > 0.7: gr = GRADIENTS[random.randint(0, len(GRADIENTS))] lg1 = cairo.LinearGradient(text_x, text_y, 500, 500) lg1.add_color_stop_rgb(0, *map(lambda x: x / 255, gr[0])) lg1.add_color_stop_rgb(0.5, *map(lambda x: x / 255, gr[1])) Context.set_source(lg1) else: Context.set_source_rgb(0, 0, 0) if random.random() < 0.6: Context.fill() else: Context.stroke() if add_svg_elements: doc = ss.Document() layout1 = ss.HBoxLayout() layout1.setSpacing(-image_size[0]) layout1.addSVG(file_name, alignment=ss.AlignHCenter | ss.AlignVCenter) for _ in range(1): el = random.choice(ELEMENTS) layout1.addSVG(el, alignment=ss.AlignHCenter | ss.AlignVCenter) doc.setLayout(layout1) doc.save(file_name)
# Spearman correlation argsS = "-in {} --whatToPlot scatterplot --labels {} {} --corMethod {} -o {}".format( out_fileB, lb1, lb2, 'spearman', out_figS).split() pltCorr.main(argsS) #~ # Combine two fugures in one file with svg_stack utility #~ doc = ss.Document() #~ # layout = ss.VBoxLayout() #~ layout = ss.HBoxLayout() #~ layout.addSVG(out_figP,alignment=ss.AlignTop|ss.AlignHCenter) #~ layout.addSVG(out_figS,alignment=ss.AlignCenter) #~ doc.setLayout(layout) #~ doc.save(out_fig) # Combine all fugures in one file with svg_stack utility docAll = ss.Document() layoutA = ss.VBoxLayout() for sample_name in samples: #~ out_fig = bw + sample_name + '_replCorr' + '.svg' out_figS = bw + sample_name + '_replCorr_spear' + '.svg' layoutA.addSVG(out_figS, alignment=ss.AlignCenter) docAll.setLayout(layoutA) docAll.save(bw + sample_name[:4] + '_all_replCorr' + '.svg')
plt.setp(ax3.get_xticklabels(), visible=True) ticklabelpad = mpl.rcParams['xtick.major.pad'] ax3.annotate(r'($\mu$s)', xy=(1,-0.1), xytext=(-5, -ticklabelpad), ha='left', va='top', xycoords='axes fraction', textcoords='offset points') #plt.xlabel(r'time ($\mu$s)') #plt.show() # Saving out the figure fig.savefig('Fig_TDE_case_' + cases_list[case_index] + '.pdf') fig.savefig('Fig_TDE_case_' + cases_list[case_index] + '.svg') doc = ss.Document() layout1 = ss.HBoxLayout() layout1.addSVG('Fig_TDE_case_A.svg',alignment=ss.AlignTop|ss.AlignHCenter) layout1.addSVG('Fig_TDE_case_B.svg',alignment=ss.AlignTop|ss.AlignHCenter) layout1.addSVG('Fig_TDE_case_C.svg',alignment=ss.AlignTop|ss.AlignHCenter) layout1.addSVG('Fig_TDE_case_D.svg',alignment=ss.AlignTop|ss.AlignHCenter) doc.setLayout(layout1) doc.save('row_0.svg') doc = ss.Document() layout2 = ss.HBoxLayout() layout2.addSVG('Fig_TDE_case_E.svg',alignment=ss.AlignBottom|ss.AlignHCenter)
def DrawSvg(self, topOutDir: str = "images", subOutDir: str = datetime.now().strftime('%Y%m%d_%H%M%S'), outFileName: str = "tmp.svg", svgInDir: str = "svg", svgSz: int = 45) -> None: # build a dict of {row: [Plmt]} rowDict = {} for plmt in self.board.Placements(): row = plmt.pstn[1] if row not in rowDict: rowDict[row] = [] rowDict[row].append(plmt) rows = sorted(rowDict.keys()) minRow = rows[0] maxRow = rows[-1] hlayouts = [] for row in rows: if row in rowDict: # for each row, sort [Piece] by col r = sorted(rowDict[row], key=lambda p: p.pstn[0]) minCol = r[0].pstn[0] maxCol = r[-1].pstn[0] # if first col isn't = bounding box minx, prepend empty piece if minCol > self.board.Box()[0][0]: r = [Plmt(Piece.X, (self.board.Box()[0][0], row))] + r # if last col isn't = bounding box maxx, append empty piece if maxCol < self.board.Box()[1][0]: r = [Plmt(Piece.X, (self.board.Box()[1][0], row))] + r else: # if no pieces in row, make a layout bookmarked by blanks r = [ Plmt(Piece.X, (self.board.Box()[0][0], row)), Plmt(Piece.X, (self.board.Box()[1][0], row)), ] # put first item into hlayout. save last piece lastLyt = ss.HBoxLayout() lastLyt.addSVG( f"{svgInDir}/{r[0].clr.name.lower()}/{r[0].piece.name}.svg", alignment=ss.AlignLeft) lastPlmt = r[0] for plmt in r[1:]: # for each piece, create new hlayout, add last layout w/ proper spacing lyt = ss.HBoxLayout() lyt.setSpacing(svgSz * (plmt.pstn[0] - lastPlmt.pstn[0] - 1)) lyt.addLayout(lastLyt) lyt.addSVG( f"{svgInDir}/{plmt.clr.name.lower()}/{plmt.piece.name}.svg", alignment=ss.AlignLeft) lastLyt, lastPlmt = lyt, plmt hlayouts.append(lastLyt) # for each hlayout, append into vlayout vlayout = ss.VBoxLayout() for hlayout in hlayouts: vlayout.addLayout(hlayout) # make doc and save to outdir doc = ss.Document() doc.setLayout(vlayout) loc = f"{topOutDir}/{subOutDir}/{outFileName}" os.makedirs(os.path.dirname(loc), exist_ok=True) doc.save(loc)