def _set_viewport(kind, subplot): global _plt metric_width, metric_height, pixel_width, pixel_height = gr.inqdspsize() if 'figsize' in _plt.kwargs: horizontal_pixels_per_inch = pixel_width * 0.0254 / metric_width vertical_pixels_per_inch = pixel_height * 0.0254 / metric_height width = _plt.kwargs['figsize'][0] * horizontal_pixels_per_inch height = _plt.kwargs['figsize'][1] * vertical_pixels_per_inch else: width, height = _plt.kwargs['size'] viewport = [0, 0, 0, 0] vp = subplot[:] if width > height: aspect_ratio = height/width metric_size = metric_width * width / pixel_width gr.setwsviewport(0, metric_size, 0, metric_size*aspect_ratio) gr.setwswindow(0, 1, 0, aspect_ratio) vp[2] *= aspect_ratio vp[3] *= aspect_ratio else: aspect_ratio = width/ height metric_size = metric_height * height / pixel_height gr.setwsviewport(0, metric_size * aspect_ratio, 0, metric_size) gr.setwswindow(0, aspect_ratio, 0, 1) vp[0] *= aspect_ratio vp[1] *= aspect_ratio viewport[0] = vp[0] + 0.125 * (vp[1]-vp[0]) viewport[1] = vp[0] + 0.925 * (vp[1]-vp[0]) viewport[2] = vp[2] + 0.125 * (vp[3]-vp[2]) viewport[3] = vp[2] + 0.925 * (vp[3]-vp[2]) if width > height: viewport[2] += (1 - (subplot[3] - subplot[2])**2) * 0.02 if kind in ('wireframe', 'surface', 'plot3', 'scatter3'): viewport[1] -= 0.0525 if kind in ('contour', 'contourf', 'surface'): viewport[1] -= 0.1 gr.setviewport(*viewport) _plt.kwargs['viewport'] = viewport _plt.kwargs['vp'] = vp _plt.kwargs['ratio'] = aspect_ratio if 'backgroundcolor' in _plt.kwargs: gr.savestate() gr.selntran(0) gr.setfillintstyle(gr.INTSTYLE_SOLID) gr.setfillcolorind(_plt.kwargs['backgroundcolor']) if width > height: gr.fillrect(subplot[0], subplot[1], subplot[2] * aspect_ratio, subplot[3] * aspect_ratio) else: gr.fillrect(subplot[0] * aspect_ratio, subplot[1] * aspect_ratio, subplot[2], subplot[3]) gr.selntran(1) gr.restorestate()
def __init__(self, dpi): self.dpi = dpi self.width = 640.0 * dpi / 80 self.height = 480.0 * dpi / 80 mwidth, mheight, width, height = gr.inqdspsize() if (width / (mwidth / 0.0256) < 200): mwidth *= self.width / width gr.setwsviewport(0, mwidth, 0, mwidth * 0.75) else: gr.setwsviewport(0, 0.192, 0, 0.144) gr.setwswindow(0, 1, 0, 0.75) gr.setviewport(0, 1, 0, 0.75) gr.setwindow(0, self.width, 0, self.height) self.mathtext_parser = MathTextParser('agg') self.texmanager = TexManager()
def configure(self): aspect_ratio = self.width / self.height if aspect_ratio > 1: rect = np.array([0, 1, 0, 1.0 / aspect_ratio]) self.size = self.width else: rect = np.array([0, aspect_ratio, 0, 1]) self.size = self.height mwidth, mheight, width, height = gr.inqdspsize() if width / (mwidth / 0.0256) < 200: mwidth *= self.width / width gr.setwsviewport(*rect * mwidth) else: gr.setwsviewport(*rect * 0.192) gr.setwswindow(*rect) gr.setviewport(*rect) gr.setwindow(0, self.width, 0, self.height)
def write(self, image, device_pixel_ratio=1): height, width = image.shape[:2] gr.clearws() if width > height: xmax = 1.0 ymax = 1.0*height/width else: xmax = 1.0*width/height ymax = 1.0 metric_width, metric_height, pixel_width, pixel_height = gr.inqdspsize() meter_per_horizontal_pixel = metric_width/pixel_width meter_per_vertical_pixel = metric_height/pixel_height gr.setwsviewport(0, meter_per_horizontal_pixel*width*device_pixel_ratio, 0, meter_per_vertical_pixel*height*device_pixel_ratio) gr.setwswindow(0, xmax, 0, ymax) gr.setviewport(0, xmax, 0, ymax) gr.setwindow(0, xmax, 0, ymax) gr.drawimage(0, xmax, 0, ymax, width*device_pixel_ratio, height*device_pixel_ratio, image.view('uint32')) gr.updatews()
def write(self, image, device_pixel_ratio=1): height, width = image.shape[:2] gr.clearws() if width > height: xmax = 1.0 ymax = 1.0 * height / width else: xmax = 1.0 * width / height ymax = 1.0 metric_width, metric_height, pixel_width, pixel_height = gr.inqdspsize() meter_per_horizontal_pixel = metric_width / pixel_width meter_per_vertical_pixel = metric_height / pixel_height gr.setwsviewport( 0, meter_per_horizontal_pixel * width * device_pixel_ratio, 0, meter_per_vertical_pixel * height * device_pixel_ratio, ) gr.setwswindow(0, xmax, 0, ymax) gr.setviewport(0, xmax, 0, ymax) gr.setwindow(0, xmax, 0, ymax) gr.drawimage(0, xmax, 0, ymax, width * device_pixel_ratio, height * device_pixel_ratio, image.view("uint32")) gr.updatews()
def _set_viewport(kind, subplot): global _plt metric_width, metric_height, pixel_width, pixel_height = gr.inqdspsize() if 'figsize' in _plt.kwargs: horizontal_pixels_per_inch = pixel_width * 0.0254 / metric_width vertical_pixels_per_inch = pixel_height * 0.0254 / metric_height width = _plt.kwargs['figsize'][0] * horizontal_pixels_per_inch height = _plt.kwargs['figsize'][1] * vertical_pixels_per_inch else: dpi = pixel_width / metric_width * 0.0254 if dpi > 200: width, height = tuple(x * dpi / 100 for x in _plt.kwargs['size']) else: width, height = _plt.kwargs['size'] viewport = [0, 0, 0, 0] vp = subplot[:] if width > height: aspect_ratio = height/width metric_size = metric_width * width / pixel_width gr.setwsviewport(0, metric_size, 0, metric_size*aspect_ratio) gr.setwswindow(0, 1, 0, aspect_ratio) vp[2] *= aspect_ratio vp[3] *= aspect_ratio else: aspect_ratio = width/ height metric_size = metric_height * height / pixel_height gr.setwsviewport(0, metric_size * aspect_ratio, 0, metric_size) gr.setwswindow(0, aspect_ratio, 0, 1) vp[0] *= aspect_ratio vp[1] *= aspect_ratio viewport[0] = vp[0] + 0.125 * (vp[1]-vp[0]) viewport[1] = vp[0] + 0.925 * (vp[1]-vp[0]) viewport[2] = vp[2] + 0.125 * (vp[3]-vp[2]) viewport[3] = vp[2] + 0.925 * (vp[3]-vp[2]) if width > height: viewport[2] += (1 - (subplot[3] - subplot[2])**2) * 0.02 if kind in ('wireframe', 'surface', 'plot3', 'scatter3', 'trisurf'): viewport[1] -= 0.0525 if kind in ('contour', 'contourf', 'surface', 'trisurf', 'heatmap', 'hexbin'): viewport[1] -= 0.1 gr.setviewport(*viewport) _plt.kwargs['viewport'] = viewport _plt.kwargs['vp'] = vp _plt.kwargs['ratio'] = aspect_ratio if 'backgroundcolor' in _plt.kwargs: gr.savestate() gr.selntran(0) gr.setfillintstyle(gr.INTSTYLE_SOLID) gr.setfillcolorind(_plt.kwargs['backgroundcolor']) if width > height: gr.fillrect(subplot[0], subplot[1], subplot[2] * aspect_ratio, subplot[3] * aspect_ratio) else: gr.fillrect(subplot[0] * aspect_ratio, subplot[1] * aspect_ratio, subplot[2], subplot[3]) gr.selntran(1) gr.restorestate() if kind == 'polar': x_min, x_max, y_min, y_max = viewport x_center = 0.5 * (x_min + x_max) y_center = 0.5 * (y_min + y_max) r = 0.5 * min(x_max - x_min, y_max - y_min) gr.setviewport(x_center - r, x_center + r, y_center - r, y_center + r)