def draw(self, event): self.bm = wx.EmptyBitmap(self.GetSizeTuple()[0], self.GetSizeTuple()[1]) dc = wx.MemoryDC(self.bm) os.environ['GKSconid'] = "%x!%x" % (get_address(repr(self)), get_address(repr(dc))) dc.SetBackground(wx.WHITE_BRUSH) dc.Clear() dc.DrawText("Surface Plot using wxWidgets ...", 15, 15) x = range(1, 481) y = range(1, 481) w, h, d = gr.readimage( os.path.join(os.path.dirname(os.path.realpath(__file__)), 'surf.png')) z = map(lambda x: x & 0xff, d) gr.setviewport(0, 1, 0, 1) gr.setwindow(1, 480, 1, 480) gr.setspace(1, 1000, 30, 80) gr.setcolormap(3) gr.surface(x, y, z, 6) gr.contour(x, y, range(1), z, 0) gr.updatews() self.Refresh() event.Skip()
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(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)
return abs(np.fft.fft(amplitudes / 32768.0))[:SAMPLES/2] spectrum = np.zeros((256, 256), dtype=float) t = -255 dt = float(SAMPLES) / FS df = FS / float(SAMPLES) / 2 / 2 start = time.time() while time.time() - start < 10: try: power = get_spectrum() except (IOError): continue gr.clearws() spectrum[:, 255] = power[:256] spectrum = np.roll(spectrum, 1) gr.setcolormap(-113) gr.setviewport(0.05, 0.95, 0.1, 1) gr.setwindow(t * dt, (t + 255) * dt, 0, df) gr.setscale(gr.OPTION_FLIP_X) gr.setspace(0, 200, 30, 80) gr3.surface((t + np.arange(256)) * dt, np.linspace(0, df, 256), spectrum, 4) gr.setscale(0) gr.axes3d(0.2, 0.2, 0, (t + 255) * dt, 0, 0, 5, 5, 0, -0.01) gr.titles3d('t [s]', 'f [kHz]', '') gr.updatews() t += 1
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) _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)): gr.setfillcolorind(989) gr.setfillintstyle(gr.INTSTYLE_SOLID) gr.fillrect(x[i-1], x[i], y_min, y[i]) gr.setfillcolorind(1) gr.setfillintstyle(gr.INTSTYLE_HOLLOW) gr.fillrect(x[i-1], x[i], y_min, y[i]) 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': 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) width, height = z.shape data = np.array(1000+(z-z.min()) / z.ptp() * 255, np.int32) x_min, x_max = _plt.kwargs['xrange'] y_min, y_max = _plt.kwargs['yrange'] gr.cellarray(x_min, x_max, y_max, y_min, width, height, data) _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.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) 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 _set_window(kind): global _plt scale = 0 scale |= gr.OPTION_X_LOG if _plt.kwargs.get('xlog', False) else 0 scale |= gr.OPTION_Y_LOG if _plt.kwargs.get('ylog', False) else 0 scale |= gr.OPTION_Z_LOG if _plt.kwargs.get('zlog', False) else 0 scale |= gr.OPTION_FLIP_X if _plt.kwargs.get('xflip', False) else 0 scale |= gr.OPTION_FLIP_Y if _plt.kwargs.get('yflip', False) else 0 scale |= gr.OPTION_FLIP_Z if _plt.kwargs.get('zflip', False) else 0 _minmax() if kind in ('wireframe', 'surface', 'plot3', 'scatter3'): major_count = 2 else: major_count = 5 x_min, x_max = _plt.kwargs['xrange'] if not scale & gr.OPTION_X_LOG: x_min, x_max = gr.adjustlimits(x_min, x_max) x_major_count = major_count x_tick = gr.tick(x_min, x_max) / x_major_count else: x_tick = x_major_count = 1 if not scale & gr.OPTION_FLIP_X: xorg = (x_min, x_max) else: xorg = (x_max, x_min) _plt.kwargs['xaxis'] = x_tick, xorg, x_major_count y_min, y_max = _plt.kwargs['yrange'] if kind in ('hist', 'stem') and 'ylim' not in _plt.kwargs: y_min = 0 if not scale & gr.OPTION_Y_LOG: y_min, y_max = gr.adjustlimits(y_min, y_max) y_major_count = major_count y_tick = gr.tick(y_min, y_max) / y_major_count else: y_tick = y_major_count = 1 if not scale & gr.OPTION_FLIP_Y: yorg = (y_min, y_max) else: yorg = (y_max, y_min) _plt.kwargs['yaxis'] = y_tick, yorg, y_major_count _plt.kwargs['window'] = (x_min, x_max, y_min, y_max) gr.setwindow(x_min, x_max, y_min, y_max) if kind in ('wireframe', 'surface', 'plot3', 'scatter3'): z_min, z_max = _plt.kwargs['zrange'] if not scale & gr.OPTION_Z_LOG: z_min, z_max = gr.adjustlimits(z_min, z_max) z_major_count = major_count z_tick = gr.tick(z_min, z_max) / z_major_count else: z_tick = z_major_count = 1 if not scale & gr.OPTION_FLIP_Z: zorg = (z_min, z_max) else: zorg = (z_max, z_min) _plt.kwargs['zaxis'] = z_tick, zorg, z_major_count rotation = _plt.kwargs.get('rotation', 40) tilt = _plt.kwargs.get('tilt', 70) gr.setspace(z_min, z_max, rotation, tilt) _plt.kwargs['scale'] = scale gr.setscale(scale)
#!/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()
stream = pa.open(format=pa.get_format_from_width(wf.getsampwidth()), channels=wf.getnchannels(), rate=wf.getframerate(), output=True) spectrum = np.zeros((256, 64), dtype=float) t = -63 dt = float(SAMPLES) / FS df = FS / float(SAMPLES) / 2 / 2 data = wf.readframes(SAMPLES) while data != '' and len(data) == SAMPLES * wf.getsampwidth(): stream.write(data) amplitudes = np.fromstring(data, dtype=np.short) power = abs(np.fft.fft(amplitudes / 32768.0))[:SAMPLES/2] gr.clearws() spectrum[:, 63] = power[:256] spectrum = np.roll(spectrum, 1) gr.setcolormap(-113) gr.setviewport(0.05, 0.95, 0.1, 1) gr.setwindow(t * dt, (t + 63) * dt, 0, df) gr.setscale(gr.OPTION_FLIP_X) gr.setspace(0, 256, 30, 80) gr3.surface((t + np.arange(64)) * dt, np.linspace(0, df, 256), spectrum, 4) gr.setscale(0) gr.axes3d(0.2, 0.2, 0, (t + 63) * dt, 0, 0, 5, 5, 0, -0.01) gr.titles3d('t [s]', 'f [kHz]', '') gr.updatews() data = wf.readframes(SAMPLES) t += 1
q = np.zeros(width * height).astype(np.complex128) output = calc_fractal(q, min_x, max_x, min_y, max_y, width, height, iters) return output os.environ['PYOPENCL_CTX'] = os.environ.get('PYOPENCL_CTX', '0') x = -0.9223327810370947027656057193752719757635 y = 0.3102598350874576432708737495917724836010 f = 0.5 for i in range(200): start = timer() pixels = create_fractal(x - f, x + f, y - f, y + f, 500, 500, 400) dt = timer() - start print("Mandelbrot created in %f s" % dt) gr.clearws() gr.setviewport(0, 1, 0, 1) gr.setcolormap(113) z = np.resize(pixels, (500, 500)) gr.setwindow(0, 500, 0, 500) gr.setspace(0, 600, 30, 80) gr3.surface(range(500), range(500), z, 3) gr.updatews() f *= 0.9
import gr import gr3 import numpy as np from scipy.spatial import Delaunay # Set up example data radii = np.linspace(0, 1, 20) angles = np.linspace(0, np.pi * 2, 30) points = np.zeros((len(angles), len(radii), 3)) points[:, :, 0] = np.cos(angles).reshape(len(angles), 1) * radii.reshape( 1, len(radii)) points[:, :, 1] = np.sin(angles).reshape(len(angles), 1) * radii.reshape( 1, len(radii)) points[:, :, 2] = -radii.reshape(1, len(radii)) points.shape = (len(angles) * len(radii), 3) points[:, :] = points[:, :] * 0.5 + 0.5 # Perform 2D delaunay triangulation triangles = Delaunay(points[:, :2]).simplices.copy() points = points[triangles] # Set up GR state gr.setcolormap(1) gr.clearws() gr.setspace(points[:, 2].min(), points[:, 2].max(), 0, 0) # Draw using GR3 gr3.drawtrianglesurface(points) gr.updatews()
rate=wf.getframerate(), output=True) spectrum = np.zeros((256, 64), dtype=float) t = -63 dt = float(SAMPLES) / FS df = FS / float(SAMPLES) / 2 / 2 data = wf.readframes(SAMPLES) while data != '' and len(data) == SAMPLES * wf.getsampwidth(): stream.write(data) amplitudes = np.frombuffer(data, dtype=np.short) power = abs(np.fft.fft(amplitudes / 32768.0))[:SAMPLES // 2] gr.clearws() spectrum[:, 63] = power[:256] spectrum = np.roll(spectrum, 1) gr.setcolormap(-113) gr.setviewport(0.05, 0.95, 0.1, 1) gr.setwindow(t * dt, (t + 63) * dt, 0, df) gr.setscale(gr.OPTION_FLIP_X) gr.setspace(0, 256, 30, 80) gr3.surface((t + np.arange(64)) * dt, np.linspace(0, df, 256), spectrum, 4) gr.setscale(0) gr.axes3d(0.2, 0.2, 0, (t + 63) * dt, 0, 0, 5, 5, 0, -0.01) gr.titles3d('t [s]', 'f [kHz]', '') gr.updatews() data = wf.readframes(SAMPLES) t += 1
x = [-2 + i * 0.5 for i in range(0, 29)] y = [-7 + i * 0.5 for i in range(0, 29)] z = list(range(0, 841)) for i in range(0, 29): for j in range(0, 29): r1 = math.sqrt((x[j] - 5)**2 + y[i]**2) r2 = math.sqrt((x[j] + 5)**2 + y[i]**2) z[i * 29 - 1 + j] = (math.exp(math.cos(r1)) + math.exp(math.cos(r2)) - 0.9) * 25 gr.setcharheight(24.0 / 500) gr.settextalign(gr.TEXT_HALIGN_CENTER, gr.TEXT_VALIGN_TOP) gr.textext(0.5, 0.9, "Surface Example") (tbx, tby) = gr.inqtextext(0.5, 0.9, "Surface Example") gr.fillarea(tbx, tby) gr.setwindow(-2, 12, -7, 7) gr.setspace(-80, 200, 45, 70) gr.setcharheight(14.0 / 500) gr.axes3d(1, 0, 20, -2, -7, -80, 2, 0, 2, -0.01) gr.axes3d(0, 1, 0, 12, -7, -80, 0, 2, 0, 0.01) gr.titles3d("X-Axis", "Y-Axis", "Z-Axis") gr.surface(x, y, z, 3) gr.surface(x, y, z, 1) gr.updatews()
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()
""" 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()
output = calc_fractal(q, min_x, max_x, min_y, max_y, width, height, iters) return output if getenv('PYOPENCL_CTX') == None: environ['PYOPENCL_CTX'] = '0' x = -0.9223327810370947027656057193752719757635 y = 0.3102598350874576432708737495917724836010 f = 0.5 for i in range(200): start = timer() pixels = create_fractal(x-f, x+f, y-f, y+f, 500, 500, 400) dt = timer() - start print("Mandelbrot created in %f s" % dt) gr.clearws() gr.setviewport(0, 1, 0, 1) gr.setcolormap(113) z = np.resize(pixels, (500, 500)) gr.setwindow(0, 500, 0, 500) gr.setspace(0, 600, 30, 80) gr3.surface(range(500), range(500), z, 3) gr.updatews() f *= 0.9
def _set_window(kind): global _plt scale = 0 if kind != 'polar': scale |= gr.OPTION_X_LOG if _plt.kwargs.get('xlog', False) else 0 scale |= gr.OPTION_Y_LOG if _plt.kwargs.get('ylog', False) else 0 scale |= gr.OPTION_Z_LOG if _plt.kwargs.get('zlog', False) else 0 scale |= gr.OPTION_FLIP_X if _plt.kwargs.get('xflip', False) else 0 scale |= gr.OPTION_FLIP_Y if _plt.kwargs.get('yflip', False) else 0 scale |= gr.OPTION_FLIP_Z if _plt.kwargs.get('zflip', False) else 0 _minmax() if kind in ('wireframe', 'surface', 'plot3', 'scatter3', 'polar', 'trisurf'): major_count = 2 else: major_count = 5 x_min, x_max = _plt.kwargs['xrange'] if not scale & gr.OPTION_X_LOG: x_min, x_max = gr.adjustlimits(x_min, x_max) x_major_count = major_count x_tick = gr.tick(x_min, x_max) / x_major_count else: x_tick = x_major_count = 1 if not scale & gr.OPTION_FLIP_X: xorg = (x_min, x_max) else: xorg = (x_max, x_min) _plt.kwargs['xaxis'] = x_tick, xorg, x_major_count y_min, y_max = _plt.kwargs['yrange'] if kind in ('hist', 'stem') and 'ylim' not in _plt.kwargs: y_min = 0 if not scale & gr.OPTION_Y_LOG: y_min, y_max = gr.adjustlimits(y_min, y_max) y_major_count = major_count y_tick = gr.tick(y_min, y_max) / y_major_count else: y_tick = y_major_count = 1 if not scale & gr.OPTION_FLIP_Y: yorg = (y_min, y_max) else: yorg = (y_max, y_min) _plt.kwargs['yaxis'] = y_tick, yorg, y_major_count _plt.kwargs['window'] = (x_min, x_max, y_min, y_max) if kind == 'polar': gr.setwindow(-1, 1, -1, 1) else: gr.setwindow(x_min, x_max, y_min, y_max) if kind in ('wireframe', 'surface', 'plot3', 'scatter3', 'trisurf'): z_min, z_max = _plt.kwargs['zrange'] if not scale & gr.OPTION_Z_LOG: z_min, z_max = gr.adjustlimits(z_min, z_max) z_major_count = major_count z_tick = gr.tick(z_min, z_max) / z_major_count else: z_tick = z_major_count = 1 if not scale & gr.OPTION_FLIP_Z: zorg = (z_min, z_max) else: zorg = (z_max, z_min) _plt.kwargs['zaxis'] = z_tick, zorg, z_major_count rotation = _plt.kwargs.get('rotation', 40) tilt = _plt.kwargs.get('tilt', 70) gr.setspace(z_min, z_max, rotation, tilt) _plt.kwargs['scale'] = scale gr.setscale(scale)
spectrum = np.zeros((256, 256), dtype=float) t = -255 dt = float(SAMPLES) / FS df = FS / float(SAMPLES) / 2 / 2 start = time.time() while time.time() - start < 10: try: power = get_spectrum() except (IOError): continue gr.clearws() spectrum[:, 255] = power[:256] spectrum = np.roll(spectrum, 1) gr.setcolormap(-113) gr.setviewport(0.05, 0.95, 0.1, 1) gr.setwindow(t * dt, (t + 255) * dt, 0, df) gr.setscale(gr.OPTION_FLIP_X) gr.setspace(0, 200, 30, 80) gr3.surface((t + np.arange(256)) * dt, np.linspace(0, df, 256), spectrum, 4) gr.setscale(0) gr.axes3d(0.2, 0.2, 0, (t + 255) * dt, 0, 0, 5, 5, 0, -0.01) gr.titles3d('t [s]', 'f [kHz]', '') gr.updatews() t += 1
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()