pythonmatplotlibzoomingginput

can Python Matplotlib "ginput" be independent from "zoom to rectangle"?


I am using ginput for a graphic selection of a few points along a time signal. Sometimes, when the signal is too dense it might be useful to zoom over an area before making the selection of points. My problem is that it seems that the zoom to rectangle option seems to be accounted for in ginput.

For instance, with this sample code:

from __future__ import print_function
from pylab import arange, plot, sin, ginput, show
import numpy as np

t = np.linspace(0,25,500)
plot(t, sin(t))
x = ginput(3)
print("clicked",x)
show()

If I zoom over a portion of the signal, the clicks made for the zoom area selection are accounted for in ginput... Is there a way to avoid this and make the zoom area selection independent from ginput ?


Solution

  • As I did not get any answer and I could not find much on the topic, here is how I solved this problem: I added a pre-procedure allowing the user to zoom in over a chosen area. When the area is suitable for point picking, the user simply has to press "Enter" and go on with ginput.

    from __future__ import print_function
    from pylab import arange, plot, sin, ginput, show
    import matplotlib as plt  
    from pylab import *
    import numpy as np
    
    def closest_point(vec,val):
        ind = np.where(vec==min(vec,key=lambda x:abs(x-val)))
        return ind[0][0]
    
    t = np.linspace(0,250,5000)
    y = sin(t)
    
    fig = plt.figure()
    plt.suptitle('chosing points over a possibly dense signal...',fontsize=14)
    zoom_ok = False
    while not zoom_ok:
        plt.plot(t,y)
        plt.title('1 - click on the two corners of the area to enlarge',fontsize=12)
        zoom = ginput(2,timeout=-1)
        xzoom = np.zeros([0])
        for i in range(2):
            xzoom = np.concatenate((xzoom,[int(closest_point(t,zoom[i][0]))]))  
        temps_zoom = t[xzoom[0]:xzoom[1]]
        dep_zoom = y[xzoom[0]:xzoom[1]]
        plt.clf()
        plt.plot(temps_zoom,dep_zoom,'b')
        plt.title('2 - if zoom ok -> press Enter, otherwise -> mouse click',fontsize=12)
        zoom_ok = plt.waitforbuttonpress()
    plt.title('3 - you may now select the points ',fontsize=16)
    x = ginput(2,timeout=-1)
    plt.show()