高效的Matplotlib重绘

高效的Matplotlib重绘,第1张

高效的Matplotlib重绘

当然!你想要的是发短信。如果您不是在编写gui,则可以使用来简化其中的一些 *** 作

matplotlib.animation
,但是如果您希望事物是交互式的,则需要直接处理它。

在matplotlib而言,想要的组合

fig.canvas.copy_from_bbox
,然后交替拨打
fig.canvas.restore_region(background)
ax.draw_artist(what_you_want_to_draw)
并且
fig.canvas.blit

background = fig.canvas.copy_from_bbox(ax.bbox)for x, y in user_interactions:    fig.canvas.restore_region(background)    points.append([x, y])    scatter.set_offsets(points)    ax.draw_artist(scatter)    fig.canvas.blit(ax.bbox)

简单的划线示例:添加

就您而言,如果仅添加点,则实际上可以跳过保存和恢复背景。但是,如果走那条路线,由于抗锯齿点会相互重复重绘,因此您会对图进行一些微妙的更改。

无论如何,这是您想要的事物类型的最简单的示例。如上所述,这仅涉及添加点,并且跳过了保存和恢复背景的 *** 作:

import matplotlib.pyplot as pltimport numpy as npdef main():    fig, ax = plt.subplots()    ax.pcolormesh(np.random.random((100, 100)), cmap='gray')    ClickToDrawPoints(ax).show()class ClickToDrawPoints(object):    def __init__(self, ax):        self.ax = ax        self.fig = ax.figure        self.xy = []        self.points = ax.scatter([], [], s=200, color='red', picker=20)        self.fig.canvas.mpl_connect('button_press_event', self.on_click)    def on_click(self, event):        if event.inaxes is None: return        self.xy.append([event.xdata, event.ydata])        self.points.set_offsets(self.xy)        self.ax.draw_artist(self.points)        self.fig.canvas.blit(self.ax.bbox)    def show(self):        plt.show()main()

有时候简单太简单

但是,假设我们要右键单击删除一个点。

在这种情况下,我们需要能够还原背景而不重绘背景。

好的,一切都很好。我们将使用与答案顶部提到的伪代码段相似的内容。

但是,有一个警告:如果调整了图的大小,我们需要更新背景。同样,如果以交互方式缩放/平移轴,则需要更新背景。基本上,您需要在绘制绘图时随时更新背景。

很快您就需要变得相当复杂。


更复杂:添加/拖动/删除点

这是您最终放置到位的“脚手架”类型的一般示例。

由于绘图绘制了两次,因此效率有些低下。(例如,平移会很 )。可以解决这个问题,但是我将这些例子再留一遍。

这实现了添加点,拖动点和删除点。要在交互式缩放/平移后添加/拖动点,请再次单击工具栏上的缩放/平移工具以将其禁用。

这是一个相当复杂的示例,但希望它能使人们理解通常以交互方式绘制/拖动/编辑/删除matplotlib艺术家而无需重绘整个图的框架类型。

import numpy as npimport matplotlib.pyplot as pltclass DrawDragPoints(object):    """    Demonstrates a basic example of the "scaffolding" you need to efficiently    blit drawable/draggable/deleteable artists on top of a background.    """    def __init__(self):        self.fig, self.ax = self.setup_axes()        self.xy = []        self.tolerance = 10        self._num_clicks = 0        # The artist we'll be modifying...        self.points = self.ax.scatter([], [], s=200, color='red',     picker=self.tolerance, animated=True)        connect = self.fig.canvas.mpl_connect        connect('button_press_event', self.on_click)        self.draw_cid = connect('draw_event', self.grab_background)    def setup_axes(self):        """Setup the figure/axes and plot any background artists."""        fig, ax = plt.subplots()        # imshow would be _much_ faster in this case, but let's deliberately        # use something slow...        ax.pcolormesh(np.random.random((1000, 1000)), cmap='gray')        ax.set_title('Left click to add/drag a pointnRight-click to delete')        return fig, ax    def on_click(self, event):        """Decide whether to add, delete, or drag a point."""        # If we're using a tool on the toolbar, don't add/draw a point...        if self.fig.canvas.toolbar._active is not None: return        contains, info = self.points.contains(event)        if contains: i = info['ind'][0] if event.button == 1:     self.start_drag(i) elif event.button == 3:     self.delete_point(i)        else: self.add_point(event)    def update(self):        """Update the artist for any changes to self.xy."""        self.points.set_offsets(self.xy)        self.blit()    def add_point(self, event):        self.xy.append([event.xdata, event.ydata])        self.update()    def delete_point(self, i):        self.xy.pop(i)        self.update()    def start_drag(self, i):        """Bind mouse motion to updating a particular point."""        self.drag_i = i        connect = self.fig.canvas.mpl_connect        cid1 = connect('motion_notify_event', self.drag_update)        cid2 = connect('button_release_event', self.end_drag)        self.drag_cids = [cid1, cid2]    def drag_update(self, event):        """Update a point that's being moved interactively."""        self.xy[self.drag_i] = [event.xdata, event.ydata]        self.update()    def end_drag(self, event):        """End the binding of mouse motion to a particular point."""        for cid in self.drag_cids: self.fig.canvas.mpl_disconnect(cid)    def safe_draw(self):        """Temporarily disconnect the draw_event callback to avoid recursion"""        canvas = self.fig.canvas        canvas.mpl_disconnect(self.draw_cid)        canvas.draw()        self.draw_cid = canvas.mpl_connect('draw_event', self.grab_background)    def grab_background(self, event=None):        """        When the figure is resized, hide the points, draw everything,        and update the background.        """        self.points.set_visible(False)        self.safe_draw()        # With most backends (e.g. TkAgg), we could grab (and refresh, in        # self.blit) self.ax.bbox instead of self.fig.bbox, but Qt4Agg, and        # some others, requires us to update the _full_ canvas, instead.        self.background = self.fig.canvas.copy_from_bbox(self.fig.bbox)        self.points.set_visible(True)        self.blit()    def blit(self):        """        Efficiently update the figure, without needing to redraw the        "background" artists.        """        self.fig.canvas.restore_region(self.background)        self.ax.draw_artist(self.points)        self.fig.canvas.blit(self.fig.bbox)    def show(self):        plt.show()DrawDragPoints().show()


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5642983.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存