Matplotlib:更新图形后交互式缩放工具中的错误

时间:2019-07-04 12:51:26

标签: python matplotlib

我再次陷入与matplotlib进行交互式绘图的困境。

其他所有内容都像一个超级按钮(在图形中移动和单击对象),但是如果我缩放显示的图形并将其更新,则缩放矩形将保留在新图形中。可能我不得不以某种方式重置缩放设置,但是我无法从其他StackOverflow问题中找到正确的缩放方法(清除数字显然不够)。

我建立了一个玩具示例来说明问题。将四个点附加到四个图像上,并将它们绘制到该图上。通过将光标插入所选点的顶部进行交互模式,它将在图像框中显示相关图像。单击一个点后,程序将等待2秒钟,并通过将所有样本旋转15度来更新视图。

缩放当前视图,然后对其进行更新时,会发生此问题。缩放到矩形将自动开始,在图中的任何位置单击一次后,矩形将不做任何事情。如下图所示。我只想在更新图形后使用普通光标。

enter image description here

这是玩具示例的代码:

import matplotlib.pyplot as plt
import matplotlib as mpl
from matplotlib.offsetbox import OffsetImage, AnnotationBbox
import numpy as np
import copy

def initialize_figure(fignum):
    plt.figure(fignum)
    plt.clf()

def draw_interactive_figures(new_samples, images):
    global new_samples_tmp, images_tmp, offset_image_tmp, image_box_tmp, fig_tmp, x_tmp, y_tmp
    initialize_figure(1)
    plt.ion()
    fig_tmp = plt.gcf()
    images_tmp = copy.deepcopy(images)
    offset_image_tmp = OffsetImage(images_tmp[0,:,:,:])
    image_box_tmp = (40., 40.)
    x_tmp = new_samples[:,0]
    y_tmp = new_samples[:,1]
    new_samples_tmp = copy.deepcopy(new_samples)
    update_plot()

    fig_tmp.canvas.mpl_connect('motion_notify_event', hover)
    fig_tmp.canvas.mpl_connect('button_press_event', click)
    plt.show()
    fig_tmp.canvas.start_event_loop()
    plt.ioff()

def update_plot():
    global points_tmp, annotationbox_tmp
    ax = plt.gca()
    points_tmp = plt.scatter(*new_samples_tmp.T, s=14, c='b', edgecolor='k')
    annotationbox_tmp = AnnotationBbox(offset_image_tmp, (0,0), xybox=image_box_tmp, xycoords='data', boxcoords='offset points',  pad=0.3,  arrowprops=dict(arrowstyle='->'))
    ax.add_artist(annotationbox_tmp)
    annotationbox_tmp.set_visible(False)

def hover(event):
    if points_tmp.contains(event)[0]:
        inds = points_tmp.contains(event)[1]['ind']
        ind = inds[0]
        w,h = fig_tmp.get_size_inches()*fig_tmp.dpi
        ws = (event.x > w/2.)*-1 + (event.x <= w/2.) 
        hs = (event.y > h/2.)*-1 + (event.y <= h/2.)
        annotationbox_tmp.xybox = (image_box_tmp[0]*ws, image_box_tmp[1]*hs)
        annotationbox_tmp.set_visible(True)
        annotationbox_tmp.xy =(x_tmp[ind], y_tmp[ind])
        offset_image_tmp.set_data(images_tmp[ind,:,:])
    else:
        annotationbox_tmp.set_visible(False)
    fig_tmp.canvas.draw_idle()

def click(event):
    if points_tmp.contains(event)[0]:
        inds = points_tmp.contains(event)[1]['ind']
        ind = inds[0]
        initialize_figure(1)
        update_plot()
        plt.scatter(x_tmp[ind], y_tmp[ind], s=20, marker='*', c='y')
        plt.pause(2)
        fig_tmp.canvas.stop_event_loop()
    fig_tmp.canvas.draw_idle()

def main():
    fig, ax = plt.subplots(1, figsize=(7, 7))

    points = np.array([[1,1],[1,-1],[-1,1],[-1,-1]])
    zero_layer = np.zeros([28,28])
    one_layer = np.ones([28,28])*255
    images = np.array([np.array([zero_layer, zero_layer, one_layer]).astype(np.uint8),np.array([zero_layer, one_layer, zero_layer]).astype(np.uint8),np.array([one_layer, zero_layer, zero_layer]).astype(np.uint8),np.array([one_layer, zero_layer, one_layer]).astype(np.uint8)])
    images = np.transpose(images, (0,3,2,1))
    theta = 0
    delta = 15 * (np.pi/180)
    rotation_matrix = np.array([[np.cos(theta),-np.sin(theta)],[np.sin(theta),np.cos(theta)]])
    while True:
        rotated_points = np.matmul(points, rotation_matrix)
        draw_interactive_figures(rotated_points, images)
        theta += delta
        rotation_matrix = np.array([[np.cos(theta),-np.sin(theta)],[np.sin(theta),np.cos(theta)]])

if __name__== "__main__":
    main()

谢谢!

1 个答案:

答案 0 :(得分:0)

我在这里为您提供一个起点。以下是一个脚本,可创建绘图并允许您通过单击轴来添加新点。对于每个点,可以将鼠标悬停并显示相应的图像。

import matplotlib.pyplot as plt
from matplotlib.offsetbox import OffsetImage, AnnotationBbox
import numpy as np


class MyInteractivePlotter():
    def __init__(self):
        self.fig, self.ax = plt.subplots()
        self.ax.set(xlim=(0,1), ylim=(0,1))

        self.points = np.array([[0.5,0.5]]) # will become N x 2 array
        self.images = [np.random.rand(10,10)]

        self.scatter = self.ax.scatter(*self.points.T)
        self.im = OffsetImage(self.images[0], zoom=5)

        self.ab = AnnotationBbox(self.im, (0,0), xybox=(50., 50.), xycoords='data',
                                 boxcoords="offset points",  pad=0.3,  
                                 arrowprops=dict(arrowstyle="->"))
        # add it to the axes and make it invisible
        self.ax.add_artist(self.ab)
        self.ab.set_visible(False)

        self.cid = self.fig.canvas.mpl_connect("button_press_event", self.onclick)
        self.hid = self.fig.canvas.mpl_connect("motion_notify_event", self.onhover)

    def add_point(self):
        # Update points (here, we just add a new random point)
        self.points = np.concatenate((self.points, np.random.rand(1,2)), axis=0)
        # For each points there is an image. (Here, we just add a random one)
        self.images.append(np.random.rand(10,10))
        # Update the scatter plot to show the new point
        self.scatter.set_offsets(self.points)


    def onclick(self, event):
        self.add_point()
        self.fig.canvas.draw_idle()

    def onhover(self, event):
        # if the mouse is over the scatter points
        if self.scatter.contains(event)[0]:
            # find out the index within the array from the event
            ind, = self.scatter.contains(event)[1]["ind"]           
            # make annotation box visible
            self.ab.set_visible(True)
            # place it at the position of the hovered scatter point
            self.ab.xy = self.points[ind,:]
            # set the image corresponding to that point
            self.im.set_data(self.images[ind])
        else:
            #if the mouse is not over a scatter point
            self.ab.set_visible(False)
        self.fig.canvas.draw_idle()


m = MyInteractivePlotter()
plt.show()

我建议您接受此操作并将功能添加到其中。一旦发现问题,就可以使用它进行澄清。