# Untitled

By: a guest on May 6th, 2012  |  syntax: None  |  size: 2.92 KB  |  hits: 21  |  expires: Never
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
1. why is plotting with Matplotlib so slow?
2. from pylab import *
3. import time
4.
5. ion()
6. fig = figure()
13.
14. x = arange(0,2*pi,0.01)
15. y = sin(x)
16. line1, = ax1.plot(x, y, 'r-')
17. line2, = ax2.plot(x, y, 'g-')
18. line3, = ax3.plot(x, y, 'y-')
19. line4, = ax4.plot(x, y, 'm-')
20. line5, = ax5.plot(x, y, 'k-')
21. line6, = ax6.plot(x, y, 'p-')
22.
23. # turn off interactive plotting - speeds things up by 1 Frame / second
24. plt.ioff()
25.
26.
27. tstart = time.time()               # for profiling
28. for i in arange(1, 200):
29.     line1.set_ydata(sin(x+i/10.0))  # update the data
30.     line2.set_ydata(sin(2*x+i/10.0))
31.     line3.set_ydata(sin(3*x+i/10.0))
32.     line4.set_ydata(sin(4*x+i/10.0))
33.     line5.set_ydata(sin(5*x+i/10.0))
34.     line6.set_ydata(sin(6*x+i/10.0))
35.     draw()                         # redraw the canvas
36.
37. print 'FPS:' , 200/(time.time()-tstart)
38.
39. import matplotlib.pyplot as plt
40. import numpy as np
41. import time
42.
43. x = np.arange(0, 2*np.pi, 0.01)
44. y = np.sin(x)
45.
46. fig, axes = plt.subplots(nrows=6)
47. styles = ['r-', 'g-', 'y-', 'm-', 'k-', 'c-']
48. lines = [ax.plot(x, y, style)[0] for ax, style in zip(axes, styles)]
49.
50. fig.show()
51.
52. tstart = time.time()
53. for i in xrange(1, 20):
54.     for j, line in enumerate(lines, start=1):
55.         line.set_ydata(np.sin(j*x + i/10.0))
56.     fig.canvas.draw()
57.
58. print 'FPS:' , 20/(time.time()-tstart)
59.
60. import matplotlib.pyplot as plt
61. import numpy as np
62. import time
63.
64. x = np.arange(0, 2*np.pi, 0.1)
65. y = np.sin(x)
66.
67. fig, axes = plt.subplots(nrows=6)
68.
69. styles = ['r-', 'g-', 'y-', 'm-', 'k-', 'c-']
70. def plot(ax, style):
71.     return ax.plot(x, y, style, animated=True)[0]
72. lines = [plot(ax, style) for ax, style in zip(axes, styles)]
73.
74. # Let's capture the background of the figure
75. backgrounds = [fig.canvas.copy_from_bbox(ax.bbox) for ax in axes]
76.
77. fig.show()
78.
79. # We need to draw the canvas before we start animating...
80. fig.canvas.draw()
81.
82. tstart = time.time()
83. for i in xrange(1, 2000):
84.     items = enumerate(zip(lines, axes, backgrounds), start=1)
85.     for j, (line, ax, background) in items:
86.         fig.canvas.restore_region(background)
87.         line.set_ydata(np.sin(j*x + i/10.0))
88.         ax.draw_artist(line)
89.         fig.canvas.blit(ax.bbox)
90.
91. print 'FPS:' , 2000/(time.time()-tstart)
92.
93. import matplotlib.pyplot as plt
94. import matplotlib.animation as animation
95. import numpy as np
96.
97. x = np.arange(0, 2*np.pi, 0.1)
98. y = np.sin(x)
99.
100. fig, axes = plt.subplots(nrows=6)
101.
102. styles = ['r-', 'g-', 'y-', 'm-', 'k-', 'c-']
103. def plot(ax, style):
104.     return ax.plot(x, y, style, animated=True)[0]
105. lines = [plot(ax, style) for ax, style in zip(axes, styles)]
106.
107. def animate(i):
108.     for j, line in enumerate(lines, start=1):
109.         line.set_ydata(np.sin(j*x + i/10.0))
110.     return lines
111.
112. # We'd normally specify a reasonable "interval" here...
113. ani = animation.FuncAnimation(fig, animate, xrange(1, 200),
114.                               interval=0, blit=True)
115. plt.show()