Search code examples
pythonmatplotlibcursor

Is there a way to choose a default mouse "mode" in matplotlib.pyplot.show() to be the zoom pointer?


I have a program that is used to show a bunch of different plots, and I often have to zoom into an area. I want to know if there is a way to get the default cursor to be zoom, as if I had already clicked on the magnifying glass. I want to avoid having to click on the "magnifying glass" every time my plot is shown... I have not tried any solutions as I could not find any method in the matplotlib.pyplot documentation that is remotely close to what I want to do.

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.plot([1,2,3])
plt.show()

enter image description here


Solution

  • You can call

    fig.canvas.toolbar.zoom()
    

    before showing the plot to activate the zoom mode straight away.

    Complete example:

    import matplotlib.pyplot as plt
    
    fig,ax = plt.subplots()
    
    # some code...
    
    fig.canvas.toolbar.zoom()
    plt.show()