def plot_domain(color_func, f, re=(-1, 1), im=(-1, 1), N=100, n=15): w = func_vals(f, re, im, N) domc = color_func(w, n) * 255 width, height = domc.shape[:2] domc = np.append(domc, np.ones((width, height, 1)) * 255, axis=2) domc = domc.astype(np.uint8) domc = domc.view('<i4') gr.clearws() gr.setviewport(0, 1, 0, 1) gr.drawimage(0, 1, 0, 1, width, height, domc, model=gr.MODEL_HSV) gr.updatews()
def plot_domain(color_func, f, re=[-1, 1], im=[-1, 1], N=100, n=15): w = func_vals(f, re, im, N) domc = color_func(w, n) * 255 width, height = domc.shape[:2] domc = np.append(domc, np.ones((width, height, 1)) * 255, axis=2) domc = domc.astype(np.uint8) domc = domc.view("<i4") gr.clearws() gr.setviewport(0, 1, 0, 1) gr.drawimage(0, 1, 0, 1, width, height, domc, model=gr.MODEL_HSV) gr.updatews()
def _plot_img(I): global _plt if isinstance(I, basestring): width, height, data = gr.readimage(I) if width == 0 or height == 0: return else: I = np.array(I) width, height = I.shape data = np.array(1000+(1.0*I - I.min()) / I.ptp() * 255, np.int32) if _plt.kwargs['clear']: gr.clearws() if not _plt.kwargs['ax']: _set_viewport('line', _plt.kwargs['subplot']) viewport = _plt.kwargs['viewport'] vp = _plt.kwargs['vp'] if width * (viewport[3] - viewport[2]) < height * (viewport[1] - viewport[0]): w = width / height * (viewport[3] - viewport[2]) x_min = max(0.5 * (viewport[0] + viewport[1] - w), viewport[0]) x_max = min(0.5 * (viewport[0] + viewport[1] + w), viewport[1]) y_min = viewport[2] y_max = viewport[3] else: h = height / width * (viewport[1] - viewport[0]) x_min = viewport[0] x_max = viewport[1] y_min = max(0.5 * (viewport[3] + viewport[2] - h), viewport[2]) y_max = min(0.5 * (viewport[3] + viewport[2] + h), viewport[3]) if 'cmap' in _plt.kwargs: warnings.warn('The parameter "cmap" has been replaced by "colormap". The value of "cmap" will be ignored.', stacklevel=3) colormap = _plt.kwargs.get('colormap', gr.COLORMAP_VIRIDIS) if colormap is not None: gr.setcolormap(colormap) gr.selntran(0) if isinstance(I, basestring): gr.drawimage(x_min, x_max, y_min, y_max, width, height, data) else: gr.cellarray(x_min, x_max, y_min, y_max, width, height, data) if 'title' in _plt.kwargs: gr.savestate() gr.settextalign(gr.TEXT_HALIGN_CENTER, gr.TEXT_VALIGN_TOP) gr.textext(0.5 * (viewport[0] + viewport[1]), vp[3], _plt.kwargs['title']) gr.restorestate() gr.selntran(1)
def _plot_img(I): global _plt if isinstance(I, basestring): width, height, data = gr.readimage(I) if width == 0 or height == 0: return else: I = np.array(I) width, height = I.shape data = np.array(1000 + (1.0 * I - I.min()) / I.ptp() * 255, np.int32) if _plt.kwargs['clear']: gr.clearws() if not _plt.kwargs['ax']: _set_viewport('line', _plt.kwargs['subplot']) viewport = _plt.kwargs['viewport'] vp = _plt.kwargs['vp'] if width * (viewport[3] - viewport[2]) < height * (viewport[1] - viewport[0]): w = width / height * (viewport[3] - viewport[2]) x_min = max(0.5 * (viewport[0] + viewport[1] - w), viewport[0]) x_max = min(0.5 * (viewport[0] + viewport[1] + w), viewport[1]) y_min = viewport[2] y_max = viewport[3] else: h = height / width * (viewport[1] - viewport[0]) x_min = viewport[0] x_max = viewport[1] y_min = max(0.5 * (viewport[3] + viewport[2] - h), viewport[2]) y_max = min(0.5 * (viewport[3] + viewport[2] + h), viewport[3]) gr.setcolormap(_plt.kwargs.get('cmap', 1)) gr.selntran(0) if isinstance(I, basestring): gr.drawimage(x_min, x_max, y_min, y_max, width, height, data) else: gr.cellarray(x_min, x_max, y_min, y_max, width, height, data) if 'title' in _plt.kwargs: gr.savestate() gr.settextalign(gr.TEXT_HALIGN_CENTER, gr.TEXT_VALIGN_TOP) gr.textext(0.5 * (viewport[0] + viewport[1]), vp[3], _plt.kwargs['title']) gr.restorestate() gr.selntran(1)
def _plot_img(I): global _plt if isinstance(I, basestring): width, height, data = gr.readimage(I) if width == 0 or height == 0: return else: I = np.array(I) width, height = I.shape data = np.array(1000+(1.0*I - I.min()) / I.ptp() * 255, np.int32) if _plt.kwargs['clear']: gr.clearws() if not _plt.kwargs['ax']: _set_viewport('line', _plt.kwargs['subplot']) viewport = _plt.kwargs['viewport'] vp = _plt.kwargs['vp'] if width * (viewport[3] - viewport[2]) < height * (viewport[1] - viewport[0]): w = width / height * (viewport[3] - viewport[2]) x_min = max(0.5 * (viewport[0] + viewport[1] - w), viewport[0]) x_max = min(0.5 * (viewport[0] + viewport[1] + w), viewport[1]) y_min = viewport[2] y_max = viewport[3] else: h = height / width * (viewport[1] - viewport[0]) x_min = viewport[0] x_max = viewport[1] y_min = max(0.5 * (viewport[3] + viewport[2] - h), viewport[2]) y_max = min(0.5 * (viewport[3] + viewport[2] + h), viewport[3]) gr.setcolormap(_plt.kwargs.get('cmap', 1)) gr.selntran(0) if isinstance(I, basestring): gr.drawimage(x_min, x_max, y_min, y_max, width, height, data) else: gr.cellarray(x_min, x_max, y_min, y_max, width, height, data) if 'title' in _plt.kwargs: gr.savestate() gr.settextalign(gr.TEXT_HALIGN_CENTER, gr.TEXT_VALIGN_TOP) gr.textext(0.5 * (viewport[0] + viewport[1]), vp[3], _plt.kwargs['title']) gr.restorestate() gr.selntran(1)
def draw_image(self, gc, x, y, im): if hasattr(im, 'as_rgba_str'): h, w, s = im.as_rgba_str() img = np.fromstring(s, np.uint32) img.shape = (h, w) elif len(im.shape) == 3 and im.shape[2] == 4 and im.dtype == np.uint8: img = im.view(np.uint32) img.shape = im.shape[:2] h, w = img.shape else: type_info = repr(type(im)) if hasattr(im, 'shape'): type_info += ' shape=' + repr(im.shape) if hasattr(im, 'dtype'): type_info += ' dtype=' + repr(im.dtype) warnings.warn('Unsupported image type ({}). Please report this at https://github.com/sciapp/python-gr/issues'.format(type_info)) return gr.drawimage(x, x + w, y + h, y, w, h, img)
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 draw_mathtext(self, x, y, angle, Z): h, w = Z.shape img = np.zeros((h, w), np.uint32) for i in range(h): for j in range(w): img[i, j] = (255 - Z[i, j]) << 24 a = int(angle) if a == 90: gr.drawimage(x - h, x, y, y + w, h, w, np.resize(np.rot90(img, 1), (h, w))) elif a == 180: gr.drawimage(x - w, x, y - h, y, w, h, np.rot90(img, 2)) elif a == 270: gr.drawimage(x, x + h, y - w, y, h, w, np.resize(np.rot90(img, 3), (h, w))) else: gr.drawimage(x, x + w, y, y + h, w, h, img)
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 draw_image(self, gc, x, y, im): h, w, s = im.as_rgba_str() img = np.fromstring(s, np.uint32) img.shape = (h, w) gr.drawimage(x, x + w, y + h, y, w, h, img)
def _plot_data(**kwargs): global _plt _plt.kwargs.update(kwargs) if not _plt.args: return kind = _plt.kwargs.get('kind', 'line') if _plt.kwargs['clear']: gr.clearws() if kind in ('imshow', 'isosurface'): _set_viewport(kind, _plt.kwargs['subplot']) elif not _plt.kwargs['ax']: _set_viewport(kind, _plt.kwargs['subplot']) _set_window(kind) if kind == 'polar': _draw_polar_axes() else: _draw_axes(kind) if 'cmap' in _plt.kwargs: warnings.warn('The parameter "cmap" has been replaced by "colormap". The value of "cmap" will be ignored.', stacklevel=3) colormap = _plt.kwargs.get('colormap', gr.COLORMAP_VIRIDIS) if colormap is not None: gr.setcolormap(colormap) gr.uselinespec(" ") for x, y, z, c, spec in _plt.args: gr.savestate() if 'alpha' in _plt.kwargs: gr.settransparency(_plt.kwargs['alpha']) if kind == 'line': mask = gr.uselinespec(spec) if mask in (0, 1, 3, 4, 5): gr.polyline(x, y) if mask & 2: gr.polymarker(x, y) elif kind == 'scatter': gr.setmarkertype(gr.MARKERTYPE_SOLID_CIRCLE) if z is not None or c is not None: if c is not None: c_min = c.min() c_ptp = c.ptp() for i in range(len(x)): if z is not None: gr.setmarkersize(z[i] / 100.0) if c is not None: c_index = 1000 + int(255 * (c[i]-c_min)/c_ptp) gr.setmarkercolorind(c_index) gr.polymarker([x[i]], [y[i]]) else: gr.polymarker(x, y) elif kind == 'stem': gr.setlinecolorind(1) gr.polyline(_plt.kwargs['window'][:2], [0, 0]) gr.setmarkertype(gr.MARKERTYPE_SOLID_CIRCLE) gr.uselinespec(spec) for xi, yi in zip(x, y): gr.polyline([xi, xi], [0, yi]) gr.polymarker(x, y) elif kind == 'hist': y_min = _plt.kwargs['window'][2] for i in range(1, len(y)+1): gr.setfillcolorind(989) gr.setfillintstyle(gr.INTSTYLE_SOLID) gr.fillrect(x[i-1], x[i], y_min, y[i-1]) gr.setfillcolorind(1) gr.setfillintstyle(gr.INTSTYLE_HOLLOW) gr.fillrect(x[i-1], x[i], y_min, y[i-1]) elif kind == 'contour': z_min, z_max = _plt.kwargs['zrange'] gr.setspace(z_min, z_max, 0, 90) h = [z_min + i/19*(z_max-z_min) for i in range(20)] if x.shape == y.shape == z.shape: x, y, z = gr.gridit(x, y, z, 200, 200) z.shape = np.prod(z.shape) gr.contour(x, y, h, z, 1000) _colorbar(0, 20) elif kind == 'contourf': z_min, z_max = _plt.kwargs['zrange'] gr.setspace(z_min, z_max, 0, 90) scale = _plt.kwargs['scale'] gr.setscale(scale) if x.shape == y.shape == z.shape: x, y, z = gr.gridit(x, y, z, 200, 200) z.shape = (200, 200) gr.surface(x, y, z, gr.OPTION_CELL_ARRAY) _colorbar() elif kind == 'hexbin': nbins = _plt.kwargs.get('nbins', 40) cntmax = gr.hexbin(x, y, nbins) if cntmax > 0: _plt.kwargs['zrange'] = (0, cntmax) _colorbar() elif kind == 'heatmap': x_min, x_max, y_min, y_max = _plt.kwargs['window'] width, height = z.shape cmap = _colormap() icmap = np.zeros(256, np.uint32) for i in range(256): r, g, b, a = cmap[i] icmap[i] = (int(r*255) << 0) + (int(g*255) << 8) + (int(b*255) << 16) + (int(a*255) << 24) z_min, z_max = _plt.kwargs.get('zlim', (np.min(z), np.max(z))) if z_max < z_min: z_max, z_min = z_min, z_max if z_max > z_min: data = (z - z_min) / (z_max - z_min) * 255 else: data = np.zeros((width, height)) rgba = np.zeros((width, height), np.uint32) for x in range(width): for y in range(height): rgba[x, y] = icmap[int(data[x, y])] gr.drawimage(x_min, x_max, y_min, y_max, width, height, rgba) _colorbar() elif kind == 'wireframe': if x.shape == y.shape == z.shape: x, y, z = gr.gridit(x, y, z, 50, 50) gr.setfillcolorind(0) z.shape = np.prod(z.shape) gr.surface(x, y, z, gr.OPTION_FILLED_MESH) _draw_axes(kind, 2) elif kind == 'surface': if x.shape == y.shape == z.shape: x, y, z = gr.gridit(x, y, z, 200, 200) z.shape = np.prod(z.shape) if _plt.kwargs.get('accelerate', True): gr3.clear() gr3.surface(x, y, z, gr.OPTION_COLORED_MESH) else: gr.surface(x, y, z, gr.OPTION_COLORED_MESH) _draw_axes(kind, 2) _colorbar(0.05) elif kind == 'plot3': gr.polyline3d(x, y, z) _draw_axes(kind, 2) elif kind == 'scatter3': gr.polymarker3d(x, y, z) _draw_axes(kind, 2) elif kind == 'imshow': _plot_img(z) elif kind == 'isosurface': _plot_iso(z) elif kind == 'polar': gr.uselinespec(spec) _plot_polar(x, y) elif kind == 'trisurf': gr.trisurface(x, y, z) _draw_axes(kind, 2) _colorbar(0.05) elif kind == 'tricont': zmin, zmax = _plt.kwargs['zrange'] levels = np.linspace(zmin, zmax, 20) gr.tricontour(x, y, z, levels) gr.restorestate() if kind in ('line', 'scatter', 'stem') and 'labels' in _plt.kwargs: _draw_legend() if _plt.kwargs['update']: gr.updatews() if gr.isinline(): return gr.show()
def _plot_data(**kwargs): global _plt _plt.kwargs.update(kwargs) if not _plt.args: return kind = _plt.kwargs.get('kind', 'line') if _plt.kwargs['clear']: gr.clearws() if kind in ('imshow', 'isosurface'): _set_viewport(kind, _plt.kwargs['subplot']) elif not _plt.kwargs['ax']: _set_viewport(kind, _plt.kwargs['subplot']) _set_window(kind) if kind == 'polar': _draw_polar_axes() else: _draw_axes(kind) gr.setcolormap(_plt.kwargs.get('colormap', gr.COLORMAP_COOLWARM)) gr.uselinespec(" ") for x, y, z, c, spec in _plt.args: gr.savestate() if 'alpha' in _plt.kwargs: gr.settransparency(_plt.kwargs['alpha']) if kind == 'line': mask = gr.uselinespec(spec) if mask in (0, 1, 3, 4, 5): gr.polyline(x, y) if mask & 2: gr.polymarker(x, y) elif kind == 'scatter': gr.setmarkertype(gr.MARKERTYPE_SOLID_CIRCLE) if z is not None or c is not None: if c is not None: c_min = c.min() c_ptp = c.ptp() for i in range(len(x)): if z is not None: gr.setmarkersize(z[i] / 100.0) if c is not None: c_index = 1000 + int(255 * (c[i]-c_min)/c_ptp) gr.setmarkercolorind(c_index) gr.polymarker([x[i]], [y[i]]) else: gr.polymarker(x, y) elif kind == 'stem': gr.setlinecolorind(1) gr.polyline(_plt.kwargs['window'][:2], [0, 0]) gr.setmarkertype(gr.MARKERTYPE_SOLID_CIRCLE) gr.uselinespec(spec) for xi, yi in zip(x, y): gr.polyline([xi, xi], [0, yi]) gr.polymarker(x, y) elif kind == 'hist': y_min = _plt.kwargs['window'][2] for i in range(1, len(y)+1): gr.setfillcolorind(989) gr.setfillintstyle(gr.INTSTYLE_SOLID) gr.fillrect(x[i-1], x[i], y_min, y[i-1]) gr.setfillcolorind(1) gr.setfillintstyle(gr.INTSTYLE_HOLLOW) gr.fillrect(x[i-1], x[i], y_min, y[i-1]) elif kind == 'contour': z_min, z_max = _plt.kwargs['zrange'] gr.setspace(z_min, z_max, 0, 90) h = [z_min + i/19*(z_max-z_min) for i in range(20)] if x.shape == y.shape == z.shape: x, y, z = gr.gridit(x, y, z, 200, 200) z.shape = np.prod(z.shape) gr.contour(x, y, h, z, 1000) _colorbar(0, 20) elif kind == 'contourf': z_min, z_max = _plt.kwargs['zrange'] gr.setspace(z_min, z_max, 0, 90) if x.shape == y.shape == z.shape: x, y, z = gr.gridit(x, y, z, 200, 200) z.shape = (200, 200) if _plt.kwargs['scale'] & gr.OPTION_Z_LOG != 0: z = np.log(z) gr.surface(x, y, z, gr.OPTION_CELL_ARRAY) _colorbar() elif kind == 'hexbin': nbins = _plt.kwargs.get('nbins', 40) cntmax = gr.hexbin(x, y, nbins) if cntmax > 0: _plt.kwargs['zrange'] = (0, cntmax) _colorbar() elif kind == 'heatmap': x_min, x_max, y_min, y_max = _plt.kwargs['window'] width, height = z.shape cmap = _colormap() icmap = np.zeros(256, np.uint32) for i in range(256): r, g, b, a = cmap[i] icmap[i] = (int(r*255) << 0) + (int(g*255) << 8) + (int(b*255) << 16) + (int(a*255) << 24) z_range = np.ptp(z) if z_range > 0: data = (z - np.min(z)) / z_range * 255 else: data = np.zeros((width, height)) rgba = np.zeros((width, height), np.uint32) for x in range(width): for y in range(height): rgba[x, y] = icmap[int(data[x, y])] gr.drawimage(x_min, x_max, y_min, y_max, width, height, rgba) _colorbar() elif kind == 'wireframe': if x.shape == y.shape == z.shape: x, y, z = gr.gridit(x, y, z, 50, 50) gr.setfillcolorind(0) z.shape = np.prod(z.shape) gr.surface(x, y, z, gr.OPTION_FILLED_MESH) _draw_axes(kind, 2) elif kind == 'surface': if x.shape == y.shape == z.shape: x, y, z = gr.gridit(x, y, z, 200, 200) z.shape = np.prod(z.shape) if _plt.kwargs.get('accelerate', True): gr3.clear() gr3.surface(x, y, z, gr.OPTION_COLORED_MESH) else: gr.surface(x, y, z, gr.OPTION_COLORED_MESH) _draw_axes(kind, 2) _colorbar(0.05) elif kind == 'plot3': gr.polyline3d(x, y, z) _draw_axes(kind, 2) elif kind == 'scatter3': gr.polymarker3d(x, y, z) _draw_axes(kind, 2) elif kind == 'imshow': _plot_img(z) elif kind == 'isosurface': _plot_iso(z) elif kind == 'polar': gr.uselinespec(spec) _plot_polar(x, y) elif kind == 'trisurf': gr.trisurface(x, y, z) _draw_axes(kind, 2) _colorbar(0.05) gr.restorestate() if kind in ('line', 'scatter', 'stem') and 'labels' in _plt.kwargs: _draw_legend() if _plt.kwargs['update']: gr.updatews() if gr.isinline(): return gr.show()