Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from math import*
- from turtle import*
- from time import*
- def graph(curves, c1=-575.,c2=575.,d1=-333.,d2=340.,scalingXY=(15,10,'x','y'),rounding=0, background='white', split_h=(True,15,"red"), split_v=(True,10,"red"), lower=None, upper=None, go=goto, message=True):
- #curves=list of curves to graph - form of each: [(color, width),*set of points]
- #scalingXY[0] - (units to scale x; units to scale y; name of Ox axis; name of Oy axis)
- #rounding - precision of scaling
- #background - color of background
- #split_h[0] - (is horizontal gridliness used; split_h[1] - units per board to grid; color of grid)
- #lower - position of left lower corner bounding graph
- #upper - position of right upper corner bounding graph
- #message - are we printing time graph function takes
- '''update: if we have a curve with a stream of data (not (x1,y1), (x2,y2), (x3,y3)... but x1, x2, x3) we assume x1,x2,x3,...=1,2,3,... and y1,y2,y3,... is that stream'''
- colormode(255)
- if message:
- print '2d preview...',
- t=time()
- reset()
- tracer(0)
- C,D=c2-c1,d2-d1
- for j in range(len(curves)):
- if str(type(curves[j][1]))<>"<type 'tuple'>":
- for i in range(len(curves[j])-1): curves[j][i+1]=(i,curves[j][i+1])
- if lower==None: x1,y1=float(min([min([el[0] for el in n[1:]]) for n in curves])),float(min([min([el[1] for el in n[1:]]) for n in curves]))
- else: x1,y1=lower[0],lower[1]
- if upper==None: x2,y2=float(max([max([el[0] for el in n[1:]]) for n in curves])),float(max([max([el[1] for el in n[1:]]) for n in curves]))
- else: x2,y2=upper[0],upper[1]
- #if y1==y2: y1,y2=y1-0.5,y2+0.5
- difx,dify=x2-x1,y2-y1
- bgcolor(background)
- #responsible for drawing a grid
- def improved_int(a):
- if a>0: return(int(a))
- else: return int(a)-1
- if split_h[0]:
- pencolor(split_h[2])
- k=C/difx*(-x1-difx/split_h[1]*improved_int(-x1*split_h[1]/difx)) #a coordinate to begin gridliness
- special_line=improved_int(-x1*split_h[1]/difx)/float(split_h[1])
- for n in [float(i)/split_h[1] for i in range(split_h[1]+1)]: #each vertical strip
- up()
- go(c1+k+C*n,d1)
- down()
- '''draws a special line and labels axis on it'''
- if n==special_line:
- pensize(3)
- for N in [float(i)/scalingXY[1] for i in range(scalingXY[1])]:
- go(c1+k+C*n,d1+k+D*N)
- write(str(int(round(y1+k*dify/D+N*dify,rounding))), align='right', font=("System", 12, "normal"))
- dot(5)
- go(c1+k+C*n,d2-D*(1./scalingXY[1])/3.) #let's write y
- up()
- left(90)
- shape('classic')
- stamp()
- go(c1+k+C*n+4,d2-D*(1./scalingXY[1])/3.-4)
- write(str(scalingXY[3]), align='left', font=("Arial", 14, "normal"))
- pensize(1)
- else: go(c1+k+C*n,d2)
- if split_v[0]:
- pencolor(split_v[2])
- k=D/dify*(-y1-dify/split_v[1]*improved_int(-y1*split_v[1]/dify)) #a coordinate to begin gridliness
- special_line=improved_int(-y1*split_v[1]/dify)/float(split_v[1])
- for n in [float(i)/split_v[1] for i in range(split_v[1]+1)]: #each vertical strip
- up()
- go(c1,d1+k+D*n)
- down()
- if n==special_line:
- pensize(3)
- for N in [float(i)/scalingXY[0] for i in range(scalingXY[0])]:
- go(c1+k+C*N,d1+k+D*n)
- write(str(int(round(x1+k*difx/C+N*difx,rounding))), align='right', font=("System", 12, "normal"))
- dot(5)
- go(c2-C*(1./scalingXY[0])/3.,d1+k+D*n) #let's write x
- up()
- right(90)
- shape('classic')
- stamp()
- write(str(scalingXY[2]), align='left', font=("Arial", 14, "normal"))
- pensize(1)
- else: go(c2,d1+k+D*n)
- #responsible for drawing a curves
- update()
- for m in curves:
- tracer(0)
- pencolor(m[0][0])
- pensize(m[0][1])
- up()
- go(c1+C*(m[1][0]-x1)/difx,d1+D*(m[1][1]-y1)/dify)
- down()
- for n in m[2:]: go(c1+C*(n[0]-x1)/difx,d1+D*(n[1]-y1)/dify)
- update()
- pencolor("black")
- pensize(1)
- up()
- #responsible for marking axis - old marking of graph v1.0
- '''
- go(c1,d1)
- down()
- k=C/difx*(-x1-difx/scalingXY[0]*improved_int(-x1*scalingXY[0]/difx))
- for n in [float(i)/scalingXY[0] for i in range(scalingXY[0])]:
- go(c1+k+C*n,d1)
- write(str(round(x1+k*difx/C+n*difx,rounding)))
- dot(5)
- go(c1+k+C*(n+.5/scalingXY[0]),d1)
- write(str(scalingXY[2]))
- pensize(1)
- up()
- go(c1,d1)
- down()
- k=D/dify*(-y1-dify/scalingXY[1]*improved_int(-y1*scalingXY[1]/dify))
- for n in [float(i)/scalingXY[1] for i in range(scalingXY[1])]:
- go(c1,d1+k+D*n)
- write(str(round(y1+k*dify/D+n*dify,rounding)))
- dot(5)
- go(c1,d1+k+D*(n+.5/scalingXY[1]))
- write(str(scalingXY[3]))'''
- if message: print time()-t
- hideturtle()
- update()
- exitonclick()
- def lim(n):
- k=1
- for i in xrange(2,n+1): k*=i**(1./n)
- return k/(n**0.87)
- graph([[('purple',5)]+[lim(i) for i in range(1,4000)]])
- f=[1.*ord(n) for n in open('C:\Users\Vartotojas\Desktop\Drums\Mid Conga0001.wav').read()]
- graph([[("blue",5)]+f, [("green",7)]+[(500*cos(3*t/1000.),200*sin(3*t/1000.)) for t in range(0,6000)]])
Add Comment
Please, Sign In to add comment