Guest User

Untitled

a guest
Feb 22nd, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2.  
  3. from timeit import default_timer as timer
  4. import matplotlib.pyplot as plt
  5. import Gnuplot, Gnuplot.funcutils
  6. import numpy as np
  7. import sys
  8. import os
  9.  
  10. def mPlotAndSave(x, y):
  11. plt.scatter(x, y)
  12. plt.savefig('mtmp.png')
  13. plt.clf()
  14.  
  15. def gPlotAndSave(data, g):
  16. g("set output 'gtmp.png'")
  17. g.plot(data)
  18. g("clear")
  19.  
  20. def cleanup():
  21. try:
  22. os.remove('gtmp.png')
  23. except OSError:
  24. pass
  25. try:
  26. os.remove('mtmp.png')
  27. except OSError:
  28. pass
  29.  
  30. begin = 2
  31. end = 500000
  32. step = 10000
  33. numberOfPoints = range(begin, end, step)
  34. n = len(numberOfPoints)
  35. gnuplotTime = []
  36. matplotlibTime = []
  37. progressBarWidth = 30
  38.  
  39. # Init Gnuplot
  40. g = Gnuplot.Gnuplot()
  41. g("set terminal png size 640,480")
  42.  
  43. # Init matplotlib to avoid a peak in the beginning
  44. plt.clf()
  45.  
  46. for idx, val in enumerate(numberOfPoints):
  47. # Print a nice progress bar (crucial)
  48. sys.stdout.write('r')
  49. progress = (idx+1)*progressBarWidth/n
  50. bar = "▕" + "▇"*progress + "▁"*(progressBarWidth-progress) + "▏" + str(idx) + "/" + str(n-1)
  51. sys.stdout.write(bar)
  52. sys.stdout.flush()
  53.  
  54. # Generate random data
  55. x = np.random.randint(sys.maxint, size=val)
  56. y = np.random.randint(sys.maxint, size=val)
  57. gdata = zip(x,y)
  58.  
  59. # Generate string call to a matplotlib plot and save, call it and save execution time
  60. start = timer()
  61. mPlotAndSave(x, y)
  62. end = timer()
  63. matplotlibTime.append(end - start)
  64.  
  65. # Generate string call to a gnuplot plot and save, call it and save execution time
  66. start = timer()
  67. gPlotAndSave(gdata, g)
  68. end = timer()
  69. gnuplotTime.append(end - start)
  70.  
  71. # Clean up the files
  72. cleanup()
  73.  
  74. del g
  75. sys.stdout.write('n')
  76. plt.plot(numberOfPoints, gnuplotTime, label="gnuplot")
  77. plt.plot(numberOfPoints, matplotlibTime, label="matplotlib")
  78. plt.legend(loc='upper right')
  79. plt.xlabel('Number of points in the scatter graph')
  80. plt.ylabel('Execution time (s)')
  81. plt.savefig('execution.png')
  82. plt.show()
  83.  
  84. 500.000 points scatterplot
  85. gnuplot: 5.171 s
  86. matplotlib: 230.693 s
Add Comment
Please, Sign In to add comment