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 expose(self, widget, event): cr = widget.window.cairo_create() environ["GKS_WSTYPE"] = "142" pc = PyCairoContext.from_address(id(cr)) environ['GKSconid'] = "%lu" % pc.ctx cr.move_to(15, 15) cr.set_font_size(14) cr.show_text("Contour Plot using Gtk ...") seed(0) xd = uniform(-2, 2, 100) yd = uniform(-2, 2, 100) zd = xd * np.exp(-xd**2 - yd**2) gr.setviewport(0.15, 0.95, 0.1, 0.9) gr.setwindow(-2, 2, -2, 2) gr.setspace(-0.5, 0.5, 0, 90) gr.setmarkersize(1) gr.setmarkertype(gr.MARKERTYPE_SOLID_CIRCLE) gr.setcharheight(0.024) gr.settextalign(2, 0) gr.settextfontprec(3, 0) x, y, z = gr.gridit(xd, yd, zd, 200, 200) h = np.linspace(-0.5, 0.5, 20) gr.surface(x, y, z, 5) gr.contour(x, y, h, z, 0) gr.polymarker(xd, yd) gr.axes(0.25, 0.25, -2, -2, 2, 2, 0.01) gr.updatews()
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_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(self, wsviewport=None): if self.xvalues is not None and self.widths is not None: maxidx = np.argmax(self.xvalues) rangex = (self.xvalues.min(), self.xvalues[maxidx] + self.widths[maxidx]) else: rangex = (0.0, 100.0) if self.yvalues is not None: rangey = gr.adjustrange(0.0, self.yvalues.max()) else: rangey = (0.0, 8.0) if wsviewport is None: gr.setwsviewport(0, self.mwidth, 0, self.mheight) else: gr.setwsviewport(*wsviewport) gr.setwswindow(0, self.sizex, 0, self.sizey) gr.setviewport(0.075 * self.sizex, 0.95 * self.sizex, 0.075 * self.sizey, 0.95 * self.sizey) gr.setwindow(rangex[0], rangex[1], rangey[0], rangey[1]) gr.setcharheight(0.012) gr.setfillintstyle(1) gr.setfillcolorind(0) gr.fillrect(rangex[0], rangex[1], rangey[0], rangey[1]) if self.xvalues is not None and self.yvalues is not None \ and self.widths is not None: gr.setfillintstyle(1) gr.setfillcolorind(2) for i in range(self.xvalues.size): gr.fillrect(self.xvalues[i], self.xvalues[i] + self.widths[i] * 0.8, 0.0, self.yvalues[i]) else: gr.text(0.45 * self.sizex, 0.5 * self.sizey, "no data") gr.setlinecolorind(1) xtick = floor(0.02 * (rangex[1] - rangey[0]) * 100.0) / 100.0 ytick = floor(0.04 * (rangey[1] - rangey[0]) * 50.0) / 50.0 gr.axes(xtick, ytick, rangex[0], rangey[0], 10, 5, 0.0075) gr.axes(xtick, ytick, rangex[1], rangey[1], -10, -5, -0.0075) if self.title is not None: gr.text(0.8 * self.sizex, 0.9 * self.sizey, self.title)
def draw(self): if not self._draw_graphics: return x = range(0, 128) y = range(0, 128) z = readfile(os.path.join(os.path.dirname(os.path.realpath(__file__)), "kws.dat"), separator="$") zrange = max(z) - min(z) h = [min(z) + i * 0.025 * zrange for i in range(0, 40)] gr.setviewport(0.075, 0.95, 0.075, 0.95) gr.setwindow(1, 128, 1, 128) gr.setspace(min(z), max(z), 0, 90) gr.setcharheight(0.018) gr.setcolormap(-3) gr.surface(x, y, z, 5) gr.contour(x, y, h, z, -1) gr.axes(5, 5, 1, 1, 2, 2, 0.0075)
def draw(self): if not self._draw_graphics: return x = range(0, 128) y = range(0, 128) z = readfile(os.path.join(os.path.dirname(os.path.realpath(__file__)), "kws.dat"), separator='$') zrange = max(z) - min(z) h = [min(z) + i * 0.025 * zrange for i in range(0, 40)] gr.setviewport(0.075, 0.95, 0.075, 0.95) gr.setwindow(1, 128, 1, 128) gr.setspace(min(z), max(z), 0, 90) gr.setcharheight(0.018) gr.setcolormap(-3) gr.surface(x, y, z, 5) gr.contour(x, y, h, z, -1) gr.axes(5, 5, 1, 1, 2, 2, 0.0075)
def _colorbar(off=0.0, colors=256): global _plt gr.savestate() viewport = _plt.kwargs['viewport'] zmin, zmax = _plt.kwargs['zrange'] gr.setwindow(0, 1, zmin, zmax) gr.setviewport(viewport[1] + 0.02 + off, viewport[1] + 0.05 + off, viewport[2], viewport[3]) l = [1000+int(255*i/(colors-1)) for i in range(colors)] gr.cellarray(0, 1, zmax, zmin, 1, colors, l) diag = ((viewport[1] - viewport[0])**2 + (viewport[3] - viewport[2])**2)**0.5 charheight = max(0.016 * diag, 0.012) gr.setcharheight(charheight) if _plt.kwargs['scale'] & gr.OPTION_Z_LOG: gr.setscale(gr.OPTION_Y_LOG) gr.axes(0, 2, 1, zmin, 0, 1, 0.005) else: ztick = 0.5 * gr.tick(zmin, zmax) gr.axes(0, ztick, 1, zmin, 0, 1, 0.005) gr.restorestate()
def draw(self, wsviewport=None): if self.xvalues is not None: rangex = (self.xvalues.min(), self.xvalues.max()) else: rangex = (0, 10) if self.yvalues is not None: rangey = gr.adjustrange(self.yvalues.min(), self.yvalues.max()) else: rangey = (0, 4) if wsviewport is None: gr.setwsviewport(0, self.mwidth, 0, self.mheight) else: gr.setwsviewport(*wsviewport) gr.setwswindow(0, self.sizex, 0, self.sizey) gr.setviewport(0.075 * self.sizex, 0.95 * self.sizex, 0.075 * self.sizey, 0.95 * self.sizey) gr.setwindow(rangex[0], rangex[1], rangey[0], rangey[1]) gr.setcharheight(0.012) gr.setfillintstyle(1) gr.setfillcolorind(0) gr.fillrect(rangex[0], rangex[1], rangey[0], rangey[1]) if self.xvalues is not None and self.yvalues is not None: gr.setlinecolorind(2) gr.polyline(self.xvalues, self.yvalues) else: gr.text(0.4 * self.sizex, 0.5 * self.sizey, "no elements selected") gr.setlinecolorind(1) gr.axes(0.2, 0.2, rangex[0], rangey[0], 5, 5, 0.0075) gr.axes(0.2, 0.2, rangex[1], rangey[1], -5, -5, -0.0075) if self.title is not None: gr.text(0.8 * self.sizex, 0.9 * self.sizey, self.title)
from __future__ import print_function from __future__ import unicode_literals import sys import os import numpy as np import gr sys.path.insert(0, os.path.abspath('../src')) from statistics.rdf import Kernels x = np.linspace(-1.25, 1.25, 500) for name in dir(Kernels): if name.startswith('_') or name == 'bandwidth': continue kernel = getattr(Kernels, name) y = kernel(x) gr.beginprint(name.lower()+'.svg') gr.clearws() gr.setwsviewport(0, 0.1, 0, 0.06) gr.setviewport(0, 1, 0, 1) gr.setwindow(-1.25, 1.25, -0.25, 1.25) gr.grid(0.1, 0.1, 0, 0, 5, 5) gr.axes(0.1, 0.1, 0, 0, 5, 5, -0.01) gr.setlinewidth(2) gr.setlinecolorind(2) gr.polyline(x, y) gr.setlinecolorind(1) gr.setlinewidth(1) gr.updatews() gr.endprint()
""" Create a contour plot of irregular distributed data """ from numpy.random import uniform, seed import numpy as np import gr seed(0) xd = uniform(-2, 2, 100) yd = uniform(-2, 2, 100) zd = xd * np.exp(-xd ** 2 - yd ** 2) gr.setviewport(0.1, 0.95, 0.1, 0.95) gr.setwindow(-2, 2, -2, 2) gr.setspace(-0.5, 0.5, 0, 90) gr.setmarkersize(1) gr.setmarkertype(gr.MARKERTYPE_SOLID_CIRCLE) gr.setcharheight(0.024) gr.settextalign(2, 0) gr.settextfontprec(3, 0) x, y, z = gr.gridit(xd, yd, zd, 200, 200) h = np.linspace(-0.5, 0.5, 20) gr.surface(x, y, z, 5) gr.contour(x, y, h, z, 0) gr.polymarker(xd, yd) gr.axes(0.25, 0.25, -2, -2, 2, 2, 0.01) gr.updatews()
gr.setscale(1) start = time.time() while time.time() - start < 10: try: power = get_spectrum() peakind = signal.find_peaks_cwt(power, numpy.array([5])) except (IOError): continue gr.clearws() gr.setlinewidth(1) gr.setlinecolorind(1) gr.grid(1, 5, 50, 0, 1, 2) gr.axes(1, 5, 50, 0, 1, 2, -0.008) gr.setcharheight(0.020) gr.text(0.15, 0.965, '100Hz') gr.text(0.47, 0.965, '1kHz') gr.text(0.79, 0.965, '10kHz') gr.setlinecolorind(4) gr.polyline(f[1:], power[1:]) for p in peakind: if power[p] > 10: gr.setlinewidth(2) gr.setlinecolorind(2) xe, ye = parabolic(f[p], power, p) print(xe, ye) gr.polyline([xe] * 2, [0, ye]) gr.updatews()
gr.setregenflags(gr.MPL_POSTPONE_UPDATE) for t in range(100): plt.cla() fig = plt.subplot(133) fig.xaxis.set_ticks([-100, 0, 100]) fig.yaxis.set_ticks([]) plt.ylim([0, 1000]) plt.hist(angles[t], 20, normed=0, facecolor='g', alpha=0.5) plt.show() gr.setviewport(0, 0.7, 0, 0.7) gr.setwindow(0.1, 0.9, 0.05, 0.85) mogli.draw(molecules[t], bonds_param=1.15, camera=((60, 0, 0), (0, 0, 0), (0, 1, 0))) gr.settextalign(gr.TEXT_HALIGN_CENTER, gr.TEXT_VALIGN_HALF) gr.text(0.35, 0.7, '700K (%.1f ps) # of bonds: %d' % (t / 10.0, len(angles[t]))) lens.append(len(angles[t])) if t > 0: gr.setwindow(0, 10, 3500, 5000) gr.setviewport(0.1, 0.6, 0.05, 0.1) gr.axes(1, 0, 0, 3500, 2, 0, 0.005) gr.polyline(np.arange(t + 1) / 10.0, lens) gr.updatews()
#!/usr/bin/env python """ Create a contour plot of irregular distributed data """ import numpy as np import gr np.random.seed(0) xd = np.random.uniform(-2, 2, 100) yd = np.random.uniform(-2, 2, 100) zd = xd * np.exp(-xd**2 - yd**2) gr.setviewport(0.1, 0.95, 0.1, 0.95) gr.setwindow(-2, 2, -2, 2) gr.setspace(-0.5, 0.5, 0, 90) gr.setmarkersize(1) gr.setmarkertype(gr.MARKERTYPE_SOLID_CIRCLE) gr.setcharheight(0.024) gr.settextalign(2, 0) gr.settextfontprec(3, 0) x, y, z = gr.gridit(xd, yd, zd, 200, 200) h = np.linspace(-0.5, 0.5, 20) gr.surface(x, y, z, 5) gr.contour(x, y, h, z, 0) gr.polymarker(xd, yd) gr.axes(0.25, 0.25, -2, -2, 2, 2, 0.01) gr.updatews()
gr.setscale(1) start = time.time() while time.time() - start < 10: try: power = get_spectrum() peakind = signal.find_peaks_cwt(power, np.array([5])) except (IOError): continue gr.clearws() gr.setlinewidth(1) gr.setlinecolorind(1) gr.grid(1, 5, 50, 0, 1, 2) gr.axes(1, 5, 50, 0, 1, 2, -0.008) gr.setcharheight(0.020) gr.text(0.15, 0.965, '100Hz') gr.text(0.47, 0.965, '1kHz') gr.text(0.79, 0.965, '10kHz') gr.setlinecolorind(4) gr.polyline(f[1:], power[1:]) for p in peakind: if power[p] > 10: gr.setlinewidth(2) gr.setlinecolorind(2) xe, ye = parabolic(f[p], power, p) print(xe, ye) gr.polyline([xe] * 2, [0, ye]) gr.updatews()