Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
"""
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.setlinecolorind(1)
gr.setlinewidth(2)
gr.polyline(2, x, y) # draw rod
gr.setmarkersize(5)
gr.setmarkertype(gr.MARKERTYPE_SOLID_CIRCLE)
gr.setmarkercolorind(86)
gr.polymarker(1, [x[1]], [y[1]]) # draw bob
gr.setlinecolorind(4)
V = 0.05 * omega # show angular velocity
gr.drawarrow(x[1], y[1], x[1] + V*cos(theta), y[1] + V*sin(theta))
gr.setlinecolorind(2)
A = 0.05 * acceleration # show angular acceleration
gr.drawarrow(x[1], y[1], x[1] + A*sin(theta), y[1] + A*cos(theta))
gr.settextfontprec(2, gr.TEXT_PRECISION_STRING)
gr.setcharheight(0.032)
gr.settextcolorind(1)
gr.textext(0.05, 0.95, 'Damped Pendulum')
gr.setcharheight(0.040)
gr.mathtex(0.4, 0.22, '\\omega=\\dot{\\theta}')
gr.mathtex(0.4, 0.1, '\\dot{\\omega}=-\\gamma\\omega-\\frac{g}{l}sin(\\theta)')
gr.setcharheight(0.028)
gr.textext(0.05, 0.22, 't:%7.2f' % t)
gr.textext(0.05, 0.16, '\\theta:%7.2f' % (theta / pi * 180))
gr.settextcolorind(4)
gr.textext(0.05, 0.10, '\\omega:%7.2f' % omega)
gr.settextcolorind(2)
gr.textext(0.05, 0.04, 'y_{A}:%6.2f' % acceleration)
gr.updatews()
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()
gr.setlinecolorind(1)
gr.setlinewidth(2)
gr.polyline(x, y) # draw rod
gr.setmarkersize(5)
gr.setmarkertype(gr.MARKERTYPE_SOLID_CIRCLE)
gr.setmarkercolorind(86)
gr.polymarker([x[1]], [y[1]]) # draw bob
gr.setlinecolorind(4)
V = 0.05 * omega # show angular velocity
gr.drawarrow(x[1], y[1], x[1] + V * np.cos(theta), y[1] + V * np.sin(theta))
gr.setlinecolorind(2)
A = 0.05 * acceleration # show angular acceleration
gr.drawarrow(x[1], y[1], x[1] + A * np.sin(theta), y[1] + A * np.cos(theta))
gr.settextfontprec(2, gr.TEXT_PRECISION_STRING)
gr.setcharheight(0.032)
gr.settextcolorind(1)
gr.textext(0.05, 0.95, 'Damped Pendulum')
gr.setcharheight(0.040)
gr.mathtex(0.4, 0.22, '\\omega=\\dot{\\theta}')
gr.mathtex(0.4, 0.1, '\\dot{\\omega}=-\\gamma\\omega-\\frac{g}{l}sin(\\theta)')
gr.setcharheight(0.028)
gr.textext(0.05, 0.22, 't:%7.2f' % t)
gr.textext(0.05, 0.16, '\\theta:%7.2f' % (theta / np.pi * 180))
gr.settextcolorind(4)
gr.textext(0.05, 0.10, '\\omega:%7.2f' % omega)
gr.settextcolorind(2)
gr.textext(0.05, 0.04, 'y_{A}:%6.2f' % acceleration)
gr.updatews()
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.encode("latin-1"))
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)
else:
xtick = majorx = 1
if scale & gr.OPTION_Y_LOG == 0:
majory = 5
ytick = gr.tick(ymin, ymax) /majory
else:
ytick = majory = 1
gr.setviewport(viewport[0], viewport[1], viewport[2], viewport[3])
gr.setwindow(xmin, xmax, ymin, ymax)
gr.setscale(scale)
if bgcolor != 0:
gr.setfillintstyle(1)
gr.setfillcolorind(bgcolor)
gr.fillrect(xmin, xmax, ymin, ymax)
charheight = 0.024 * (viewport[3] - viewport[2])
gr.setcharheight(charheight)
gr.axes(xtick, ytick, xmin, ymin, majorx, majory, 0.01)
gr.axes(xtick, ytick, xmax, ymax, -majorx, -majory, -0.01)
if grid:
gr.grid(xtick, ytick, xmax, ymax, majorx, majory)
gr.setlinetype(linetype)
gr.polyline(n, x, y)
if markertype != gr.MARKERTYPE_DOT:
gr.setmarkertype(markertype)
gr.polymarker(n, x, y)
if update:
gr.updatews()
self._axes.getWindow())
coord.setWCforPlotAxes(self.x, self.y, self._axes)
p0 = coord.getNDC()
x, y = p0.x, p0.y
charHeight = self.charheight * self._axes.sizey
window = gr.inqwindow()
# set viewport and window to NDC to allow 'line-drawing'
# in all regions and in NDC coordinates
# if hideviewport is False.
gr.setviewport(0, self._axes.sizex, 0, self._axes.sizey)
gr.setwindow(0, self._axes.sizex, 0, self._axes.sizey)
else:
x, y = self.x, self.y
charHeight = self.charheight
gr.setcharheight(charHeight)
gr.setcharup(*self.charup)
gr.settextalign(self.halign, self.valign)
gr.settextpath(self.textpath)
gr.setcharexpan(self.charexpan)
if self._font is not None and self._precision is not None:
gr.settextfontprec(self._font, self._precision)
gr.settextcolorind(self.textcolor)
if self._axes:
tbx, tby = gr.inqtext(x, y, self.text)
tbxmin, tbxmax = min(tbx), max(tbx)
tbymin, tbymax = min(tby), max(tby)
xmin, xmax, ymin, ymax = self._axes.viewportscaled
if (tbxmin < xmin or tbxmax > xmax
or tbymin < ymin or tbymax > ymax):
isInViewport = False
x = list(range(5))
y = list(range(5))
nominalWindowHeight = 500.0
pointSize = ( 8, 9, 10, 11, 12, 14, 18, 24, 36 )
s = "i\\hbar\\frac{\\partial\\psi}{\\partial t} = \\frac{\\hbar^2}{2m}\\nabla^2\\psi + V(\\mathbf{r})\\psi"
x = 0.9
y = 0.9;
gr.settextalign(3, 3)
for i in range(8):
gr.setcharheight(pointSize[i] / nominalWindowHeight)
gr.mathtex(x, y, s)
y -= 4 * pointSize[i] / nominalWindowHeight
gr.setcharheight(0.1)
gr.mathtex(0.9, 0.05, "Hello World!")
gr.settextcolorind(8)
gr.text(0.9, 0.05, "Hello World!")
gr.updatews()
gr3.setcameraprojectionparameters(45, 1, 200)
gr3.cameralookat(10*math.cos(-rx*math.pi/2), 10*math.sin(-rx*math.pi/2), 0, 0, 0, 0, 0, 0, 1)