Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
#!/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.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()
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 drawGR(self):
if self.visible:
gr.setspace(min(self.z), max(self.z), 0, 90)
gr.contour(self.x, self.y, self.h, self.z, self.majorh)
def drawGR(self):
if self.visible:
gr.setspace(min(self.z), max(self.z), 0, 90)
gr.contour(self.x, self.y, self.h, self.z, self.majorh)
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
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 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)
ytitle = '',
ztitle = ''):
gr.clearws()
xmin, ymin = (1, 1)
xmax, ymax = _guessdimension(len(z))[0]
xtick = gr.tick(xmin, xmax) / 5
ytick = gr.tick(ymin, ymax) / 5
x = range(1, xmax + 1)
y = range(1, ymax + 1)
zmin = min(z)
zmax = max(z)
zmin, zmax = gr.adjustrange(zmin, zmax)
ztick = gr.tick(zmin, zmax) / 5
gr.setviewport(viewport[0], viewport[1], viewport[2], viewport[3])
gr.setwindow(xmin, xmax, ymin, ymax)
gr.setspace(zmin, zmax, rotation, tilt)
charheight = 0.024 * (viewport[3] - viewport[2])
gr.setcharheight(charheight)
if rotation != 0 or tilt != 90:
gr.axes3d(xtick, 0, ztick, xmin, ymin, zmin, 5, 0, 5, -0.01)
gr.axes3d(0, ytick, 0, xmax, ymin, zmin, 0, 5, 0, 0.01)
gr.setcolormap(colormap)
gr.surface(xmax, ymax, x, y, z, option)
if contours:
gr.contour(xmax, ymax, 0, x, y, range(1), z, 0)
if rotation == 0 and tilt == 90:
gr.axes(xtick, ytick, xmin, ymin, 5, 5, -0.01)
if xtitle != '' or ytitle != '' or ztitle != '':
gr.titles3d(xtitle, ytitle, ztitle)
gr.updatews()