performance.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import matplotlib
  2. matplotlib.use('Agg')
  3. import matplotlib.pyplot as plt
  4. import numpy as np
  5. import io
  6. from matplotlib.backends.backend_agg import FigureCanvasAgg
  7. from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg
  8. from matplotlib.figure import Figure
  9. import cProfile
  10. import sys
  11. def gen_data():
  12. N = 100000
  13. x = np.random.rand(N) * 10
  14. y = np.random.rand(N) * 10
  15. colors = np.random.rand(N)
  16. area = np.pi * (15 * np.random.rand(N))**2 # 0 to 15 point radiuses
  17. data = x, y, area, colors
  18. return data
  19. # @profile
  20. def large_plot(data):
  21. x, y, area, colors = data
  22. fig = Figure(figsize=(10, 10), dpi=80)
  23. axes = fig.add_axes([0.0, 0.0, 1.0, 1.0], alpha=1.0)
  24. axes.set_frame_on(False)
  25. axes.set_xticks([])
  26. axes.set_yticks([])
  27. # axes.set_xlim(0, 10)
  28. # axes.set_ylim(0, 10)
  29. axes.scatter(x, y, s=area, c=colors, alpha=0.5)
  30. axes.set_xlim(0, 10)
  31. axes.set_ylim(0, 10)
  32. canvas = FigureCanvasAgg(fig)
  33. canvas.draw()
  34. # canvas = FigureCanvasQTAgg(fig)
  35. # buf = canvas.tostring_rgb()
  36. buf = fig.canvas.tostring_rgb()
  37. ncols, nrows = fig.canvas.get_width_height()
  38. img = np.fromstring(buf, dtype=np.uint8).reshape(nrows, ncols, 3)
  39. return img
  40. def small_plot(data):
  41. x, y, area, colors = data
  42. fig = Figure(figsize=(3, 3), dpi=80)
  43. axes = fig.add_axes([0.0, 0.0, 1.0, 1.0], alpha=1.0)
  44. axes.set_frame_on(False)
  45. axes.set_xticks([])
  46. axes.set_yticks([])
  47. # axes.set_xlim(5, 6)
  48. # axes.set_ylim(5, 6)
  49. axes.scatter(x, y, s=area, c=colors, alpha=0.5)
  50. axes.set_xlim(4, 7)
  51. axes.set_ylim(4, 7)
  52. canvas = FigureCanvasAgg(fig)
  53. canvas.draw()
  54. # canvas = FigureCanvasQTAgg(fig)
  55. # buf = canvas.tostring_rgb()
  56. buf = fig.canvas.tostring_rgb()
  57. ncols, nrows = fig.canvas.get_width_height()
  58. img = np.fromstring(buf, dtype=np.uint8).reshape(nrows, ncols, 3)
  59. return img
  60. def doit():
  61. d = gen_data()
  62. img = large_plot(d)
  63. return img
  64. if __name__ == "__main__":
  65. d = gen_data()
  66. if sys.argv[1] == 'large':
  67. cProfile.runctx('large_plot(d)', None, locals())
  68. else:
  69. cProfile.runctx('small_plot(d)', None, locals())