def xtick(self, x, y, svalue, _value): gr.setcharup(-1., 1.) gr.settextalign(gr.TEXT_HALIGN_RIGHT, gr.TEXT_VALIGN_TOP) # We want to pass through the string value, but we are passed a bytes # object by the C layer and gr.text() needs a string. Since it is # encoded using latin1 again, this is the right encoding to use here. gr.text(x, y, svalue.decode('latin1'))
def draw_axes( x_tick_spacing, y_tick_spacing, x_origin, y_origin, x_major=5, y_major=5, x_title="x", y_title="y", ): """ Parameters ---------- x_tick_spacing, y_tick_spacing : float Distance between ticks in data units x_origin, y_origin : float Location of plot origin in data units x_major, y_major : int Every x_major-th tick will be a labeled major tick on the x axis. Same for y. The ticks in between are unlabeled minor ticks. """ gr.setlinewidth(1) gr.axes(x_tick_spacing, y_tick_spacing, x_origin, y_origin, x_major, y_major, -0.01) midway = 0.54 gr.textext(midway, 0.02, "x") gr.setcharup(-1, 0) # Vertical, end-up gr.textext(0.05, midway, "y") gr.setcharup(0, 1) # Back to horizontal
def draw_axes(tick_spacing, xmin, ymin): gr.axes(tick_spacing, tick_spacing, xmin, ymin, 5, 5, -0.01) midway = 0.54 gr.textext(midway, 0.02, 'x') gr.setcharup(-1, 0) # Vertical, end-up gr.textext(0.05, midway, 'y') gr.setcharup(0, 1) # Back to horizontal
def _xtickCallBack(self, x, y, svalue, value): gr.setcharup(1., 1.) gr.settextalign(gr.TEXT_HALIGN_LEFT, gr.TEXT_VALIGN_TOP) gr.text(x, y, "%s (%s)" % (time.strftime("%H:%M:%S", time.localtime(self._startupTime + value)), svalue)) gr.setcharup(0., 1.)
def xtickCallBack(self, x, y, _svalue, value): gr.setcharup(-1., 1.) gr.settextalign(gr.TEXT_HALIGN_RIGHT, gr.TEXT_VALIGN_TOP) dx = .02 timeVal = time.localtime(value) gr.text(x + dx, y - 0.01, time.strftime(DATEFMT, timeVal)) gr.text(x - dx, y - 0.01, time.strftime(TIMEFMT, timeVal)) gr.setcharup(0., 1.)
def xtickCallBack(self, x, y, _svalue, value): gr.setcharup(-1. if self.leftTurnedLegend else 1., 1.) gr.settextalign(gr.TEXT_HALIGN_RIGHT if self.leftTurnedLegend else gr.TEXT_HALIGN_LEFT, gr.TEXT_VALIGN_TOP) dx = .015 timeVal = time.localtime(value) gr.text(x + (dx if self.leftTurnedLegend else -dx), y, time.strftime(DATEFMT, timeVal)) gr.text(x - (dx if self.leftTurnedLegend else -dx), y, time.strftime(TIMEFMT, timeVal)) gr.setcharup(0., 1.)
def xtickCallBack(self, x, y, _svalue, value): gr.setcharup(-1., 1.) gr.settextalign(gr.TEXT_HALIGN_RIGHT, gr.TEXT_VALIGN_TOP) dx = .02 timeVal = localtime(value) if self._showdate: gr.text(x + dx, y - 0.01, strftime(DATEFMT, timeVal)) if self._showsecs: gr.text(x - dx, y - 0.01, strftime(TIMEFMT, timeVal)) else: gr.text(x - dx, y - 0.01, strftime(SHORTTIMEFMT, timeVal)) gr.setcharup(0., 1.)
def _draw_axes(kind, pass_=1): global _plt viewport = _plt.kwargs['viewport'] vp = _plt.kwargs['vp'] x_tick, x_org, x_major_count = _plt.kwargs['xaxis'] y_tick, y_org, y_major_count = _plt.kwargs['yaxis'] gr.setlinecolorind(1) gr.setlinewidth(1) diag = ((viewport[1]-viewport[0])**2 + (viewport[3]-viewport[2])**2)**0.5 charheight = max(0.018 * diag, 0.012) gr.setcharheight(charheight) ticksize = 0.0075 * diag if kind in ('wireframe', 'surface', 'plot3', 'scatter3', 'trisurf'): z_tick, z_org, z_major_count = _plt.kwargs['zaxis'] if pass_ == 1: gr.grid3d(x_tick, 0, z_tick, x_org[0], y_org[0], z_org[0], 2, 0, 2) gr.grid3d(0, y_tick, 0, x_org[1], y_org[0], z_org[0], 0, 2, 0) else: gr.axes3d(x_tick, 0, z_tick, x_org[0], y_org[0], z_org[0], x_major_count, 0, z_major_count, -ticksize) gr.axes3d(0, y_tick, 0, x_org[1], y_org[0], z_org[0], 0, y_major_count, 0, ticksize) else: if kind == 'heatmap': ticksize = -ticksize else: gr.grid(x_tick, y_tick, 0, 0, x_major_count, y_major_count) gr.axes(x_tick, y_tick, x_org[0], y_org[0], x_major_count, y_major_count, ticksize) gr.axes(x_tick, y_tick, x_org[1], y_org[1], -x_major_count, -y_major_count, -ticksize) 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() if kind in ('wireframe', 'surface', 'plot3', 'scatter3', 'trisurf'): x_label = _plt.kwargs.get('xlabel', '') y_label = _plt.kwargs.get('ylabel', '') z_label = _plt.kwargs.get('zlabel', '') gr.titles3d(x_label, y_label, z_label) else: if 'xlabel' in _plt.kwargs: gr.savestate() gr.settextalign(gr.TEXT_HALIGN_CENTER, gr.TEXT_VALIGN_BOTTOM) gr.textext(0.5 * (viewport[0] + viewport[1]), vp[2] + 0.5 * charheight, _plt.kwargs['xlabel']) gr.restorestate() if 'ylabel' in _plt.kwargs: gr.savestate() gr.settextalign(gr.TEXT_HALIGN_CENTER, gr.TEXT_VALIGN_TOP) gr.setcharup(-1, 0) gr.textext(vp[0] + 0.5 * charheight, 0.5 * (viewport[2] + viewport[3]), _plt.kwargs['ylabel']) gr.restorestate()
def _draw_axes(kind, pass_=1): global _plt viewport = _plt.kwargs['viewport'] vp = _plt.kwargs['vp'] x_tick, x_org, x_major_count = _plt.kwargs['xaxis'] y_tick, y_org, y_major_count = _plt.kwargs['yaxis'] gr.setlinecolorind(1) gr.setlinewidth(1) diag = ((viewport[1]-viewport[0])**2 + (viewport[3]-viewport[2])**2)**0.5 charheight = max(0.018 * diag, 0.012) gr.setcharheight(charheight) ticksize = 0.0075 * diag if kind in ('wireframe', 'surface', 'plot3', 'scatter3', 'trisurf'): z_tick, z_org, z_major_count = _plt.kwargs['zaxis'] if pass_ == 1: gr.grid3d(x_tick, 0, z_tick, x_org[0], y_org[1], z_org[0], 2, 0, 2) gr.grid3d(0, y_tick, 0, x_org[0], y_org[1], z_org[0], 0, 2, 0) else: gr.axes3d(x_tick, 0, z_tick, x_org[0], y_org[0], z_org[0], x_major_count, 0, z_major_count, -ticksize) gr.axes3d(0, y_tick, 0, x_org[1], y_org[0], z_org[0], 0, y_major_count, 0, ticksize) else: if kind == 'heatmap': ticksize = -ticksize else: gr.grid(x_tick, y_tick, 0, 0, x_major_count, y_major_count) gr.axes(x_tick, y_tick, x_org[0], y_org[0], x_major_count, y_major_count, ticksize) gr.axes(x_tick, y_tick, x_org[1], y_org[1], -x_major_count, -y_major_count, -ticksize) 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() if kind in ('wireframe', 'surface', 'plot3', 'scatter3', 'trisurf'): x_label = _plt.kwargs.get('xlabel', '') y_label = _plt.kwargs.get('ylabel', '') z_label = _plt.kwargs.get('zlabel', '') gr.titles3d(x_label, y_label, z_label) else: if 'xlabel' in _plt.kwargs: gr.savestate() gr.settextalign(gr.TEXT_HALIGN_CENTER, gr.TEXT_VALIGN_BOTTOM) gr.textext(0.5 * (viewport[0] + viewport[1]), vp[2] + 0.5 * charheight, _plt.kwargs['xlabel']) gr.restorestate() if 'ylabel' in _plt.kwargs: gr.savestate() gr.settextalign(gr.TEXT_HALIGN_CENTER, gr.TEXT_VALIGN_TOP) gr.setcharup(-1, 0) gr.textext(vp[0] + 0.5 * charheight, 0.5 * (viewport[2] + viewport[3]), _plt.kwargs['ylabel']) gr.restorestate()
def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None): if ismath: self._draw_mathtext(gc, x, y, s, prop, angle) else: x, y = gr.wctondc(x, y) fontsize = prop.get_size_in_points() rgba = gc.get_rgb()[:4] color = gr.inqcolorfromrgb(rgba[0], rgba[1], rgba[2]) gr.settransparency(rgba[3]) gr.setcolorrep(color, rgba[0], rgba[1], rgba[2]) gr.setcharheight(fontsize * self.nominal_fontsize) gr.settextcolorind(color) if angle != 0: gr.setcharup(-np.sin(angle * np.pi/180), np.cos(angle * np.pi/180)) else: gr.setcharup(0, 1) gr.text(x, y, s)
def get_text_width_height_descent(self, s, prop, ismath): if ismath == 'TeX': fontsize = prop.get_size_in_points() w, h, d = self.texmanager.get_text_width_height_descent( s, fontsize, renderer=self) return w, h, d if ismath: ox, oy, width, height, descent, fonts, used_characters = \ self.mathtext_parser.parse(s, self.dpi, prop) return width, height, descent # family = prop.get_family() # weight = prop.get_weight() # style = prop.get_style() fontsize = prop.get_size_in_points() gr.setcharheight(fontsize * 0.0013) gr.setcharup(0, 1) (tbx, tby) = gr.inqtextext(0, 0, s) width, height, descent = tbx[1], tby[2], 0 return width, height, descent
def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None): if ismath: self._draw_mathtext(gc, x, y, s, prop, angle) else: x, y = gr.wctondc(x, y) s = s.replace(u'\u2212', '-') fontsize = prop.get_size_in_points() rgba = gc.get_rgb()[:4] color = gr.inqcolorfromrgb(rgba[0], rgba[1], rgba[2]) gr.settransparency(rgba[3]) gr.setcolorrep(color, rgba[0], rgba[1], rgba[2]) gr.setcharheight(fontsize * 0.0013) gr.settextcolorind(color) if angle != 0: gr.setcharup(-np.sin(angle * np.pi/180), np.cos(angle * np.pi/180)) else: gr.setcharup(0, 1) gr.text(x, y, s)
def get_text_width_height_descent(self, s, prop, ismath): if ismath == 'TeX': fontsize = prop.get_size_in_points() w, h, d = self.texmanager.get_text_width_height_descent( s, fontsize, renderer=self) return w, h, d if ismath: ox, oy, width, height, descent, fonts, used_characters = \ self.mathtext_parser.parse(s, self.dpi, prop) return width, height, descent # family = prop.get_family() # weight = prop.get_weight() # style = prop.get_style() fontsize = prop.get_size_in_points() gr.setcharheight(fontsize * self.nominal_fontsize) gr.setcharup(0, 1) (tbx, tby) = gr.inqtextext(0, 0, s) width, height, descent = tbx[1], tby[2], 0.2 * tby[2] return width, height, descent
def get_text_width_height_descent(self, s, prop, ismath): if ismath == 'TeX': fontsize = prop.get_size_in_points() w, h, d = self.texmanager.get_text_width_height_descent( s, fontsize, renderer=self) return w, h, d if ismath: ox, oy, width, height, descent, fonts, used_characters = \ self.mathtext_parser.parse(s, self.dpi, prop) return width, height, descent # family = prop.get_family() # weight = prop.get_weight() # style = prop.get_style() s = s.replace(u'\u2212', '-').encode("latin-1") fontsize = prop.get_size_in_points() gr.setcharheight(fontsize * 0.0013) gr.setcharup(0, 1) (tbx, tby) = gr.inqtextext(0, 0, s) width, height, descent = tbx[1], tby[2], 0 return width, height, descent
# 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()
Multiline text example """ import gr from math import sin, cos, pi import time hor_align = {'Left':1, 'Center':2, 'Right':3} vert_align = {'Top':1, 'Cap':2, 'Half':3, 'Base':4, 'Bottom':5} gr.selntran(0) gr.setcharheight(0.024) for angle in range(361): gr.setcharup(sin(-angle * pi/180), cos(-angle * pi/180)) gr.setmarkertype(2) gr.clearws() for halign in hor_align: for valign in vert_align: gr.settextalign(hor_align[halign], vert_align[valign]) x = -0.1 + hor_align[halign] * 0.3; y = 1.1 - vert_align[valign] * 0.2; s = halign + '\n' + valign + '\n' + 'third line' gr.polymarker([x], [y]) gr.text(x, y, s) tbx, tby = gr.inqtext(x, y, s) gr.fillarea(tbx, tby) gr.updatews()