def _math_tex_to_qpixmap(math_tex: str, fs: int): # set up a mpl figure instance fig = Figure() fig.patch.set_facecolor('none') fig.set_canvas(FigureCanvasAgg(fig)) renderer = fig.canvas.get_renderer() # plot the math_tex expression ax = fig.add_axes([0, 0, 1, 1]) ax.axis('off') ax.patch.set_facecolor('none') t = ax.text(0, 0, math_tex, ha='left', va='bottom', fontsize=fs) # fit figure size to text artist f_width, f_height = fig.get_size_inches() fig_bbox = fig.get_window_extent(renderer) text_bbox = t.get_window_extent(renderer) tight_fwidth = text_bbox.width * f_width / fig_bbox.width tight_fheight = text_bbox.height * f_height / fig_bbox.height fig.set_size_inches(tight_fwidth, tight_fheight) # convert mpl figure to QPixmap buf, size = fig.canvas.print_to_buffer() return QPixmap(QImage.rgbSwapped(QImage(buf, size[0], size[1], QImage.Format_ARGB32)))
class PlotCanvas(FigureCanvas): def __init__(self, parent=None, width=5, height=4, dpi=100): print(width, height) self.fig = Figure(figsize=(width, height), dpi=dpi) self.parent = parent self.axes = self.fig.add_subplot(111) FigureCanvas.__init__(self, self.fig) self.setParent(parent) def func(self, pct, allvals): absolute = float(pct / 100. * np.sum(allvals)) return "{:.1f}%\n{:.2f} ".format(pct, absolute) def update_figure(self, labels, fracs, title, text): self.axes.clear() self.axes.set_title(title) self.axes.pie(x=fracs, labels=labels, autopct=lambda pct: self.func(pct, fracs), textprops=dict(color="#000000"), startangle=90) #print(self.sizeHint().h) self.axes.text(-(self.fig.get_size_inches()[0] / 3), (self.fig.get_size_inches()[1]) / 5, text, horizontalalignment='left', color='green', fontsize=12) self.draw() def updateLine(self, scale, dataList): self.axes.clear() for line in dataList: self.axes.plot(scale, line[1], label=line[0], marker='.') self.axes.set(xlabel='Data', ylabel='Value', title='最近30日') self.axes.grid(linestyle='--', linewidth=1) self.axes.legend() self.draw()