Search code examples
python-2.7matplotlibwxpythonzoomingmagnification

Figure Size & Position after Matplotlib Zoom


I have a matplotlib image plot within a wxPython panel that I zoom in on using the native matplotlib toolbar zoom.

Having zoomed in I wish to know the size of the resulting image so that I can calculate the magnification.

Moreover, I wish to know the position/dimensions of my zoomed in image in relation to the original image so that I can re-plot it again at a later time.

I don't know how to approach this. I have looked over documentation for canvas and figure but haven't found anything which would help me pin point the data I require. Thanks for any help.


Solution

  • You may want to read the following from the matplotlib doc:

    However, especially the transformations tutorial may take a while to wrap your head around. The transformation system is very efficient and complete, but it may take you a while to figure out what especially it is you do need.

    However in your case maybe the following code snippet could be sufficient:

    from matplotlib import pyplot as plt
    import numpy
    
    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.plot(numpy.random.rand(10))
    
    def ondraw(event):
        # print 'ondraw', event
        # these ax.limits can be stored and reused as-is for set_xlim/set_ylim later
        print ax.get_xlim(), ax.get_ylim()
    
    cid = fig.canvas.mpl_connect('draw_event', ondraw)
    
    plt.show()
    

    In the draw event you can get your axes limits, calculate a scaling and whatnot and can use it as-is later on to set the ax to the desired zoom level.