<div dir="ltr"><div>You might want to consider not having the limits change at *every* mouse motion over the axes. You can add a function state variable and maybe have it refresh every other motion or so.<br><br>```<br>    def _on_move(self, event):<br>
        zoom1 = 100<br>
        if event.inaxes:<br>
<br>
            ax = event.inaxes  # the axes instance<br>
            if 'AX1' in ax.get_label():<br></div>                _on_move.counter += 1<br><div>                if _on_move.counter % 2:<br></div><div>                    return<br></div><div>
                # Mouse is in subplot 1.<br>
                xinf2 = <a href="http://np.int" rel="noreferrer" target="_blank">np.int</a>(event.xdata - zoom1)<br>
                xsup2 = <a href="http://np.int" rel="noreferrer" target="_blank">np.int</a>(event.xdata + zoom1)<br>
                yinf2 = <a href="http://np.int" rel="noreferrer" target="_blank">np.int</a>(event.ydata - zoom1)<br>
                ysup2 = <a href="http://np.int" rel="noreferrer" target="_blank">np.int</a>(event.ydata + zoom1)<br>
                ax2data = self.data[yinf2:ysup2, xinf2:xsup2]<br>
                self.plt2.set_data(ax2data)<br>
                self.ax2.figure.canvas.draw()<br></div><div>    _on_move.counter = 0<br></div><div>```<br></div><div>Another approach could be done where you capture the timestamp of when this function was last used for updating the limits, and only do a new limit update if a certain amount of time passed, such as half a second or so.<br><br></div><div>Cheers!<br></div><div>Ben Root<br><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Feb 19, 2018 at 9:37 AM, Éric Depagne <span dir="ltr"><<a href="mailto:eric@depagne.org" target="_blank">eric@depagne.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Le vendredi 16 février 2018, 21:41:07 SAST <a href="mailto:vincent.adrien@gmail.com">vincent.adrien@gmail.com</a> a écrit :<br>
Hi Adrien,<br>
<br>
Thanks for the answer, I'll have a look and will try to get things working a<br>
bit more efficiently.<br>
<br>
Cheers,<br>
Éric.<br>
<div class="HOEnZb"><div class="h5">> Hi Éric,<br>
><br>
> I am not very used to play with interactive events, but I guess some<br>
> blitting could help you with performance:<br>
> -<br>
> <a href="https://stackoverflow.com/questions/29277080/efficient-matplotlib-redrawing" rel="noreferrer" target="_blank">https://stackoverflow.com/<wbr>questions/29277080/efficient-<wbr>matplotlib-redrawing</a><br>
> -<br>
> <a href="https://matplotlib.org/users/event_handling.html#draggable-rectangle-exercis" rel="noreferrer" target="_blank">https://matplotlib.org/users/<wbr>event_handling.html#draggable-<wbr>rectangle-exercis</a><br>
> e (see the extra credit example)<br>
><br>
> See the attached script that is inspired from your code and seems to<br>
> less stress my CPU.<br>
><br>
> Hopefully this helps.<br>
><br>
> Best,<br>
> Adrien<br>
><br>
> On 02/16/2018 01:36 AM, Éric Depagne wrote:<br>
> > Hi all,<br>
> ><br>
> > I have a gridspec plot and one of the subplot is a zoom on the part over<br>
> > which the mouse hovers on another subplot.<br>
> ><br>
> > It works, but I think the way I implemented it is not very efficient,<br>
> > since each time I use it, I see one CPU go to 100%.<br>
> > Here is the code that does implement the zoom. Any idea on how to make it<br>
> > (more) efficient or changes that I should implement welcome.<br>
> ><br>
> > Thanks.<br>
> ><br>
> > def plot(self):<br>
> >          gs = gridspec.GridSpec(6, 2)<br>
> >          ax1 = plt.subplot(gs[1:, 0])<br>
> >          plt1 = ax1.imshow(self.data, vmin=self.dataminzs,<br>
> >          vmax=self.datamaxzs)<br>
> >          ax1.set_label('AX1')<br>
> >          self.ax2 = plt.subplot(gs[0:3, 1])<br>
> >          zoomeddata = self.data[<a href="http://np.int" rel="noreferrer" target="_blank">np.int</a>(self.data.<wbr>shape[0]/<br>
> ><br>
> > 2)-50:<a href="http://np.int" rel="noreferrer" target="_blank">np.int</a>(self.data.shape[<wbr>0]/2)+50, <a href="http://np.int" rel="noreferrer" target="_blank">np.int</a>(self.data.shape[1]/<br>
> > 2)-50:<a href="http://np.int" rel="noreferrer" target="_blank">np.int</a>(self.data.shape[<wbr>1]/2)+50]<br>
> ><br>
> >          self.plt2 = self.ax2.imshow(zoomeddata, vmin=self.dataminzs,<br>
> ><br>
> > vmax=self.datamaxzs)<br>
> ><br>
> >          ax1.figure.canvas.mpl_connect(<wbr>'motion_notify_event',<br>
> >          self._on_move)<br>
> ><br>
> >      def _on_move(self, event):<br>
> >          zoom1 = 100<br>
> ><br>
> >          if event.inaxes:<br>
> >              ax = event.inaxes  # the axes instance<br>
> ><br>
> >              if 'AX1' in ax.get_label():<br>
> >                  # Mouse is in subplot 1.<br>
> >                  xinf2 = <a href="http://np.int" rel="noreferrer" target="_blank">np.int</a>(event.xdata - zoom1)<br>
> >                  xsup2 = <a href="http://np.int" rel="noreferrer" target="_blank">np.int</a>(event.xdata + zoom1)<br>
> >                  yinf2 = <a href="http://np.int" rel="noreferrer" target="_blank">np.int</a>(event.ydata - zoom1)<br>
> >                  ysup2 = <a href="http://np.int" rel="noreferrer" target="_blank">np.int</a>(event.ydata + zoom1)<br>
> >                  ax2data = self.data[yinf2:ysup2, xinf2:xsup2]<br>
> >                  self.plt2.set_data(ax2data)<br>
> >                  self.ax2.figure.canvas.draw()<br>
<br>
<br>
</div></div><div class="HOEnZb"><div class="h5">--<br>
Un clavier azerty en vaut deux<br>
------------------------------<wbr>----------------------------<br>
Éric Depagne<br>
<br>
<br>
______________________________<wbr>_________________<br>
Matplotlib-users mailing list<br>
<a href="mailto:Matplotlib-users@python.org">Matplotlib-users@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/matplotlib-users" rel="noreferrer" target="_blank">https://mail.python.org/<wbr>mailman/listinfo/matplotlib-<wbr>users</a><br>
</div></div></blockquote></div><br></div>