miklis

graph v2.0

Jun 4th, 2016
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.51 KB | None | 0 0
  1. from math import*
  2. from turtle import*
  3. from time import*
  4.  
  5. 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):
  6. #curves=list of curves to graph - form of each: [(color, width),*set of points]
  7. #scalingXY[0] - (units to scale x; units to scale y; name of Ox axis; name of Oy axis)
  8. #rounding - precision of scaling
  9. #background - color of background
  10. #split_h[0] - (is horizontal gridliness used; split_h[1] - units per board to grid; color of grid)
  11. #lower - position of left lower corner bounding graph
  12. #upper - position of right upper corner bounding graph
  13. #message - are we printing time graph function takes
  14. '''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'''
  15. colormode(255)
  16. if message:
  17. print '2d preview...',
  18. t=time()
  19. reset()
  20. tracer(0)
  21. C,D=c2-c1,d2-d1
  22. for j in range(len(curves)):
  23. if str(type(curves[j][1]))<>"<type 'tuple'>":
  24. for i in range(len(curves[j])-1): curves[j][i+1]=(i,curves[j][i+1])
  25. 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]))
  26. else: x1,y1=lower[0],lower[1]
  27. 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]))
  28. else: x2,y2=upper[0],upper[1]
  29. #if y1==y2: y1,y2=y1-0.5,y2+0.5
  30. difx,dify=x2-x1,y2-y1
  31. bgcolor(background)
  32. #responsible for drawing a grid
  33. def improved_int(a):
  34. if a>0: return(int(a))
  35. else: return int(a)-1
  36. if split_h[0]:
  37. pencolor(split_h[2])
  38. k=C/difx*(-x1-difx/split_h[1]*improved_int(-x1*split_h[1]/difx)) #a coordinate to begin gridliness
  39. special_line=improved_int(-x1*split_h[1]/difx)/float(split_h[1])
  40. for n in [float(i)/split_h[1] for i in range(split_h[1]+1)]: #each vertical strip
  41. up()
  42. go(c1+k+C*n,d1)
  43. down()
  44. '''draws a special line and labels axis on it'''
  45. if n==special_line:
  46. pensize(3)
  47. for N in [float(i)/scalingXY[1] for i in range(scalingXY[1])]:
  48. go(c1+k+C*n,d1+k+D*N)
  49. write(str(int(round(y1+k*dify/D+N*dify,rounding))), align='right', font=("System", 12, "normal"))
  50. dot(5)
  51. go(c1+k+C*n,d2-D*(1./scalingXY[1])/3.) #let's write y
  52. up()
  53. left(90)
  54. shape('classic')
  55. stamp()
  56. go(c1+k+C*n+4,d2-D*(1./scalingXY[1])/3.-4)
  57. write(str(scalingXY[3]), align='left', font=("Arial", 14, "normal"))
  58. pensize(1)
  59. else: go(c1+k+C*n,d2)
  60. if split_v[0]:
  61. pencolor(split_v[2])
  62. k=D/dify*(-y1-dify/split_v[1]*improved_int(-y1*split_v[1]/dify)) #a coordinate to begin gridliness
  63. special_line=improved_int(-y1*split_v[1]/dify)/float(split_v[1])
  64. for n in [float(i)/split_v[1] for i in range(split_v[1]+1)]: #each vertical strip
  65. up()
  66. go(c1,d1+k+D*n)
  67. down()
  68. if n==special_line:
  69. pensize(3)
  70. for N in [float(i)/scalingXY[0] for i in range(scalingXY[0])]:
  71. go(c1+k+C*N,d1+k+D*n)
  72. write(str(int(round(x1+k*difx/C+N*difx,rounding))), align='right', font=("System", 12, "normal"))
  73. dot(5)
  74. go(c2-C*(1./scalingXY[0])/3.,d1+k+D*n) #let's write x
  75. up()
  76. right(90)
  77. shape('classic')
  78. stamp()
  79. write(str(scalingXY[2]), align='left', font=("Arial", 14, "normal"))
  80. pensize(1)
  81. else: go(c2,d1+k+D*n)
  82. #responsible for drawing a curves
  83. update()
  84. for m in curves:
  85. tracer(0)
  86. pencolor(m[0][0])
  87. pensize(m[0][1])
  88. up()
  89. go(c1+C*(m[1][0]-x1)/difx,d1+D*(m[1][1]-y1)/dify)
  90. down()
  91. for n in m[2:]: go(c1+C*(n[0]-x1)/difx,d1+D*(n[1]-y1)/dify)
  92. update()
  93. pencolor("black")
  94. pensize(1)
  95. up()
  96. #responsible for marking axis - old marking of graph v1.0
  97. '''
  98. go(c1,d1)
  99. down()
  100. k=C/difx*(-x1-difx/scalingXY[0]*improved_int(-x1*scalingXY[0]/difx))
  101. for n in [float(i)/scalingXY[0] for i in range(scalingXY[0])]:
  102. go(c1+k+C*n,d1)
  103. write(str(round(x1+k*difx/C+n*difx,rounding)))
  104. dot(5)
  105. go(c1+k+C*(n+.5/scalingXY[0]),d1)
  106. write(str(scalingXY[2]))
  107. pensize(1)
  108. up()
  109. go(c1,d1)
  110. down()
  111. k=D/dify*(-y1-dify/scalingXY[1]*improved_int(-y1*scalingXY[1]/dify))
  112. for n in [float(i)/scalingXY[1] for i in range(scalingXY[1])]:
  113. go(c1,d1+k+D*n)
  114. write(str(round(y1+k*dify/D+n*dify,rounding)))
  115. dot(5)
  116. go(c1,d1+k+D*(n+.5/scalingXY[1]))
  117. write(str(scalingXY[3]))'''
  118. if message: print time()-t
  119. hideturtle()
  120. update()
  121. exitonclick()
  122.  
  123. def lim(n):
  124. k=1
  125. for i in xrange(2,n+1): k*=i**(1./n)
  126. return k/(n**0.87)
  127. graph([[('purple',5)]+[lim(i) for i in range(1,4000)]])
  128. f=[1.*ord(n) for n in open('C:\Users\Vartotojas\Desktop\Drums\Mid Conga0001.wav').read()]
  129. 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