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 _draw_legend(): global _plt viewport = _plt.kwargs['viewport'] num_labels = len(_plt.kwargs['labels']) location = _plt.kwargs.get('location', 1) gr.savestate() gr.selntran(0) gr.setscale(0) w = 0 for label in _plt.kwargs['labels']: tbx, tby = gr.inqtextext(0, 0, label) w = max(w, tbx[2]) num_lines = len(_plt.args) h = (num_lines + 1) * 0.03 if location in (8, 9, 10): px = 0.5 * (viewport[0] + viewport[1] - w) elif location in (2, 3, 6): px = viewport[0] + 0.11 else: px = viewport[1] - 0.05 - w if location in (5, 6, 7, 10): py = 0.5 * (viewport[2] + viewport[3] + h) - 0.03 elif location in (3, 4, 8): py = viewport[2] + h else: py = viewport[3] - 0.06 gr.setfillintstyle(gr.INTSTYLE_SOLID) gr.setfillcolorind(0) gr.fillrect(px - 0.08, px + w + 0.02, py + 0.03, py - 0.03 * num_lines) gr.setlinetype(gr.LINETYPE_SOLID) gr.setlinecolorind(1) gr.setlinewidth(1) gr.drawrect(px - 0.08, px + w + 0.02, py + 0.03, py - 0.03 * num_lines) i = 0 gr.uselinespec(" ") for (x, y, z, c, spec) in _plt.args: gr.savestate() mask = gr.uselinespec(spec) if mask in (0, 1, 3, 4, 5): gr.polyline([px - 0.07, px - 0.01], [py, py]) if mask & 2: gr.polymarker([px - 0.06, px - 0.02], [py, py]) gr.restorestate() gr.settextalign(gr.TEXT_HALIGN_LEFT, gr.TEXT_VALIGN_HALF) if i < num_labels: gr.textext(px, py, _plt.kwargs['labels'][i]) i += 1 py -= 0.03 gr.selntran(1) gr.restorestate()
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_iso(v): global _plt viewport = _plt.kwargs['viewport'] if viewport[3] - viewport[2] < viewport[1] - viewport[0]: width = viewport[3] - viewport[2] center_x = 0.5 * (viewport[0] + viewport[1]) x_min = max(center_x - 0.5 * width, viewport[0]) x_max = min(center_x + 0.5 * width, viewport[1]) y_min = viewport[2] y_max = viewport[3] else: height = viewport[1] - viewport[0] center_y = 0.5 * (viewport[2] + viewport[3]) x_min = viewport[0] x_max = viewport[1] y_min = max(center_y - 0.5 * height, viewport[2]) y_max = min(center_y + 0.5 * height, viewport[3]) gr.selntran(0) usable_vs = v[np.abs(v) != np.inf] if np.prod(usable_vs.shape) == 0: return v_max = usable_vs.max() v_min = usable_vs.min() if v_min == v_max: return uint16_max = np.iinfo(np.uint16).max v = (np.clip(v, v_min, v_max) - v_min) / (v_max - v_min) * uint16_max values = v.astype(np.uint16) nx, ny, nz = v.shape isovalue = _plt.kwargs.get('isovalue', 0.5) rotation = np.radians(_plt.kwargs.get('rotation', 40)) tilt = np.radians(_plt.kwargs.get('tilt', 70)) gr3.clear() mesh = gr3.createisosurfacemesh(values, (2 / (nx - 1), 2 / (ny - 1), 2 / (nz - 1)), (-1., -1., -1.), int(isovalue * uint16_max)) color = _plt.kwargs.get('color', (0.0, 0.5, 0.8)) gr3.setbackgroundcolor(1, 1, 1, 0) gr3.drawmesh(mesh, 1, (0, 0, 0), (0, 0, 1), (0, 1, 0), color, (1, 1, 1)) r = 2.5 gr3.cameralookat(r * np.sin(tilt) * np.sin(rotation), r * np.cos(tilt), r * np.sin(tilt) * np.cos(rotation), 0, 0, 0, 0, 1, 0) gr3.drawimage(x_min, x_max, y_min, y_max, 500, 500, gr3.GR3_Drawable.GR3_DRAWABLE_GKS) gr3.deletemesh(mesh) 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_iso(v): global _plt viewport = _plt.kwargs['viewport'] if viewport[3] - viewport[2] < viewport[1] - viewport[0]: width = viewport[3] - viewport[2] center_x = 0.5 * (viewport[0] + viewport[1]) x_min = max(center_x - 0.5 * width, viewport[0]) x_max = min(center_x + 0.5 * width, viewport[1]) y_min = viewport[2] y_max = viewport[3] else: height = viewport[1] - viewport[0] center_y = 0.5 * (viewport[2] + viewport[3]) x_min = viewport[0] x_max = viewport[1] y_min = max(center_y - 0.5 * height, viewport[2]) y_max = min(center_y + 0.5 * height, viewport[3]) gr.selntran(0) usable_vs = v[np.abs(v) != np.inf] if np.prod(usable_vs.shape) == 0: return v_max = usable_vs.max() v_min = usable_vs.min() if v_min == v_max: return uint16_max = np.iinfo(np.uint16).max v = (np.clip(v, v_min, v_max) - v_min) / (v_max - v_min) * uint16_max values = v.astype(np.uint16) nx, ny, nz = v.shape isovalue = _plt.kwargs.get('isovalue', 0.5) rotation = np.radians(_plt.kwargs.get('rotation', 40)) tilt = np.radians(_plt.kwargs.get('tilt', 70)) gr3.clear() mesh = gr3.createisosurfacemesh(values, (2/(nx-1), 2/(ny-1), 2/(nz-1)), (-1., -1., -1.), int(isovalue * uint16_max)) color = _plt.kwargs.get('color', (0.0, 0.5, 0.8)) gr3.setbackgroundcolor(1, 1, 1, 0) gr3.drawmesh(mesh, 1, (0, 0, 0), (0, 0, 1), (0, 1, 0), color, (1, 1, 1)) r = 2.5 gr3.cameralookat(r*np.sin(tilt)*np.sin(rotation), r*np.cos(tilt), r*np.sin(tilt)*np.cos(rotation), 0, 0, 0, 0, 1, 0) gr3.drawimage(x_min, x_max, y_min, y_max, 500, 500, gr3.GR3_Drawable.GR3_DRAWABLE_GKS) gr3.deletemesh(mesh) gr.selntran(1)
def _draw_legend(): global _plt viewport = _plt.kwargs['viewport'] num_labels = len(_plt.kwargs['labels']) gr.savestate() gr.selntran(0) gr.setscale(0) w = 0 for label in _plt.kwargs['labels']: tbx, tby = gr.inqtextext(0, 0, label) w = max(w, tbx[2]) px = viewport[1] - 0.05 - w py = viewport[3] - 0.06 gr.setfillintstyle(gr.INTSTYLE_SOLID) gr.setfillcolorind(0) gr.fillrect(px - 0.08, px + w + 0.02, py + 0.03, py - 0.03 * num_labels) gr.setlinetype(1) gr.setlinecolorind(1) gr.setlinewidth(1) gr.drawrect(px - 0.08, px + w + 0.02, py + 0.03, py - 0.03 * num_labels) i = 0 gr.uselinespec(" ") for (x, y, z, c, spec) in _plt.args: gr.savestate() mask = gr.uselinespec(spec) if mask in (0, 1, 3, 4, 5): gr.polyline([px - 0.07, px - 0.01], [py, py]) if mask & 2: gr.polymarker([px - 0.06, px - 0.02], [py, py]) gr.restorestate() gr.settextalign(gr.TEXT_HALIGN_LEFT, gr.TEXT_VALIGN_HALF) if i < num_labels: gr.textext(px, py, _plt.kwargs['labels'][i]) i += 1 py -= 0.03 gr.selntran(1) gr.restorestate()
#!/usr/bin/env/python # -*- coding: utf-8 -*- import gr gr.selntran(0) gr.settextfontprec(2, 0) gr.setcharheight(0.024) gr.text(0.05, 0.85, ' !"#$%&\'()*+,-./') gr.text(0.05, 0.80, '0123456789:;<=>?') gr.text(0.05, 0.75, '@ABCDEFGHIJKLMNO') gr.text(0.05, 0.70, 'PQRSTUVWXYZ[\]^_') gr.text(0.05, 0.65, '`abcdefghijklmno') gr.text(0.05, 0.60, '"pqrstuvwxyz{|}~') gr.text(0.5, 0.85, ' ¡¢£¤¥¦§¨©ª«¬®¯') gr.text(0.5, 0.80, '°±²³´µ¶·¸¹º»¼½¾¿') gr.text(0.5, 0.75, 'ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏ') gr.text(0.5, 0.70, 'ÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞß') gr.text(0.5, 0.65, 'àáâãäåæçèéêëìíîï') gr.text(0.5, 0.60, 'ðñòóôõö÷øùúûüýþÿ') gr.updatews()
# coding: utf8 from gr import selntran, settextfontprec, settextalign, setcharheight, clearws, setcharup, text, inqtext, fillarea, updatews from math import pi, sin, cos from numpy import linspace s = 'Using inline math $\\frac{2hc^2}{\\lambda^5} \\frac{1}{e^{\\frac{hc}{\\lambda k_B T}} - 1}$ in GR text\nmixed with raw strings ' + r'$- \frac{{\hbar ^2}}{{2m}}\frac{{\partial ^2 \psi (x,t)}}{{\partial x^2 }} + U(x)\psi (x,t) = i\hbar \frac{{\partial \psi (x,t)}}{{\partial t}}$' + '\n– with line breaks\nand UTF-8 characters (ħπ),\nand rendered using GR\'s text attributes' selntran(0) settextfontprec(232, 3) settextalign(2, 3) setcharheight(0.02) for phi in linspace(0, 2 * pi, 360): clearws() setcharup(sin(phi), cos(phi)) text(0.5, 0.5, s) tbx, tby = inqtext(0.5, 0.5, s) fillarea(tbx, tby) 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)