Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.89 KB | None | 0 0
  1. import matplotlib.pyplot as plt
  2. from matplotlib.offsetbox import OffsetImage, AnnotationBbox
  3. import numpy as np; np.random.seed(42)
  4.  
  5. # Generate data x, y for scatter and an array of images.
  6. x = np.arange(20)
  7. y = np.random.rand(len(x))
  8. arr = np.empty((len(x),10,10))
  9. for i in range(len(x)):
  10. f = np.random.rand(5,5)
  11. arr[i, 0:5,0:5] = f
  12. arr[i, 5:,0:5] =np.flipud(f)
  13. arr[i, 5:,5:] =np.fliplr(np.flipud(f))
  14. arr[i, 0:5:,5:] = np.fliplr(f)
  15.  
  16. # create figure and plot scatter
  17. fig = plt.figure()
  18. ax = fig.add_subplot(111)
  19. line, = ax.plot(x,y, ls="", marker="o")
  20.  
  21. # create the annotations box
  22. im = OffsetImage(arr[0,:,:], zoom=5)
  23. xybox=(50., 50.)
  24. ab = AnnotationBbox(im, (0,0), xybox=xybox, xycoords='data',
  25. boxcoords="offset points", pad=0.3, arrowprops=dict(arrowstyle="->"))
  26. # add it to the axes and make it invisible
  27. ax.add_artist(ab)
  28. ab.set_visible(False)
  29.  
  30. def hover(event):
  31. # if the mouse is over the scatter points
  32. if line.contains(event)[0]:
  33. # find out the index within the array from the event
  34. ind, = line.contains(event)[1]["ind"]
  35. # get the figure size
  36. w,h = fig.get_size_inches()*fig.dpi
  37. ws = (event.x > w/2.)*-1 + (event.x <= w/2.)
  38. hs = (event.y > h/2.)*-1 + (event.y <= h/2.)
  39. # if event occurs in the top or right quadrant of the figure,
  40. # change the annotation box position relative to mouse.
  41. ab.xybox = (xybox[0]*ws, xybox[1]*hs)
  42. # make annotation box visible
  43. ab.set_visible(True)
  44. # place it at the position of the hovered scatter point
  45. ab.xy =(x[ind], y[ind])
  46. # set the image corresponding to that point
  47. im.set_data(arr[ind,:,:])
  48. else:
  49. #if the mouse is not over a scatter point
  50. ab.set_visible(False)
  51. fig.canvas.draw_idle()
  52.  
  53. # add callback for mouse moves
  54. fig.canvas.mpl_connect('motion_notify_event', hover)
  55. plt.show()
  56.  
  57. import matplotlib.pyplot as plt
  58. from matplotlib.offsetbox import OffsetImage, AnnotationBbox
  59. import numpy as np; np.random.seed(42)
  60. import os
  61.  
  62. os.chdir('Path/to/your/images')
  63.  
  64. # Generate data x, y for scatter and an array of images.
  65. x = np.arange(3)
  66. y = np.random.rand(len(x))
  67. jpg_name_np = np.array(['904646.jpg', '903825.jpg', '905722.jpg']).astype('<U12') # names of your images files
  68.  
  69. cmap = plt.cm.RdYlGn
  70.  
  71. # create figure and plot scatter
  72. fig = plt.figure()
  73. ax = fig.add_subplot(111)
  74. #line, = ax.plot(x,y, ls="", marker="o")
  75. line = plt.scatter(x,y,c=heat, s=10, cmap=cmap)
  76. image_path = np.asarray(jpg_name_np)
  77.  
  78. # create the annotations box
  79. image = plt.imread(image_path[0])
  80. im = OffsetImage(image, zoom=0.1)
  81. xybox=(50., 50.)
  82. ab = AnnotationBbox(im, (0,0), xybox=xybox, xycoords='data',
  83. boxcoords="offset points", pad=0.3, arrowprops=dict(arrowstyle="->"))
  84. # add it to the axes and make it invisible
  85. ax.add_artist(ab)
  86. ab.set_visible(False)
  87.  
  88. def hover(event):
  89. # if the mouse is over the scatter points
  90. if line.contains(event)[0]:
  91. # find out the index within the array from the event
  92. ind, = line.contains(event)[1]["ind"]
  93. # get the figure size
  94. w,h = fig.get_size_inches()*fig.dpi
  95. ws = (event.x > w/2.)*-1 + (event.x <= w/2.)
  96. hs = (event.y > h/2.)*-1 + (event.y <= h/2.)
  97. # if event occurs in the top or right quadrant of the figure,
  98. # change the annotation box position relative to mouse.
  99. ab.xybox = (xybox[0]*ws, xybox[1]*hs)
  100. # make annotation box visible
  101. ab.set_visible(True)
  102. # place it at the position of the hovered scatter point
  103. ab.xy =(x[ind], y[ind])
  104. # set the image corresponding to that point
  105. im.set_data(plt.imread(image_path[ind]))
  106. else:
  107. #if the mouse is not over a scatter point
  108. ab.set_visible(False)
  109. fig.canvas.draw_idle()
  110.  
  111. # add callback for mouse moves
  112. fig.canvas.mpl_connect('motion_notify_event', hover)
  113.  
  114. fig = plt.gcf()
  115. fig.set_size_inches(10.5, 9.5)
  116.  
  117. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement