【问题标题】:Add annotation in an interactive plot在交互式图中添加注释
【发布时间】:2016-09-26 14:12:32
【问题描述】:

我正在尝试在交互式绘图的中间添加注释 我想查看生成我的 test 列表的循环的 i 值 我所有的数据。对于每个 imshow 图,我想查看我的 i 值, 我添加了一个 ax.annotate 但它不起作用。

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
fig = plt.figure() # make figure
ax = fig.add_subplot(111)

test = []
mask2 = np.random.randint(255, size=(20, 20))

for i in range(1,5,3):
  kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(i,i))
  res = (cv2.morphologyEx(mask2.astype(uint8),cv2.MORPH_OPEN,kernel))
  #plt.imshow(res,cmap=plt.cm.gray,alpha=1);plt.show()
  test.append(res)


# make axesimage object
# the vmin and vmax here are very important to get the color map correct
im = ax.imshow(test[0], cmap=plt.get_cmap('hot'), vmin=0, vmax=255)
im2 = ax.annotate('This is awesome!', 
             xy=(76, -10.75),  
             xycoords='data',
             textcoords='offset points',
             arrowprops=dict(arrowstyle="->"))
plt.show()

# function to update figure
def updatefig(j):
    # set the data in the axesimage object
    im.set_array(test[j])
    # return the artists set
    return im,
# kick off the animation
ani = animation.FuncAnimation(fig, updatefig, frames=range(20), 
                              interval=50, blit=True)
plt.show()

【问题讨论】:

    标签: python numpy matplotlib annotations interactive


    【解决方案1】:

    我找到了出路。我在更新函数中添加了一个“set_text”,然后返回图片和文本:

      test = []
      test2 = []
      for i in range(3,27,3):
    kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(i,i))
    res = (cv2.morphologyEx(mask2,cv2.MORPH_OPEN,kernel))
    #plt.imshow(res,cmap=plt.cm.gray,alpha=1);plt.show()
    test.append(res)
    test2.append(i)
    
    
      fig = plt.figure() # make figure
      ax = fig.add_subplot(111)
    
      # make axesimage object
      # the vmin and vmax here are very important to get the color map correct
      im = ax.imshow(test[0], cmap=plt.get_cmap('hot'), vmin=0, vmax=255)
      time_template = 'Diffusion - Kernel size : %2.2d'    # prints running simulation time
      txt = ax.text(500, 80, '', fontsize=15,color='red')
      #plt.show()
    
      # function to update figure
      def updatefig(j):
      # set the data in the axesimage object
      im.set_array(test[j])
      txt.set_text(time_template%(float(np.asarray(test2[j]))))
      return im,txt
    
      ani = animation.FuncAnimation(fig, updatefig, frames=range(len(test)), 
                    interval=100, blit=False,repeat=True)
      plt.show()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-27
      • 2020-01-09
      相关资源
      最近更新 更多