Matplotlib inconvenient "reset to original view" while previously zoomed

Viewed 749

Updated:

After having zoomed to a region of interest, I would like to add a scatter point without having a reset to the original view.

It occurs when I double click after having zoomed to rectangle.

Of course this is a simplification of a problem encountered as I wanted to add markers to a large image after having properly zoomed to a region of interest.

Any help welcomed

import matplotlib.pyplot as plt
fig, ax = plt.subplots(nrows=1, figsize=(4,4))
plt.plot([0, 1, 2, 3], [10, 20, 30, 40])
def onclick(event):
    if event.dblclick:
        plt.scatter(event.xdata, event.ydata, c='r')
fig.canvas.mpl_connect('button_press_event', onclick)
plt.get_current_fig_manager().toolbar.zoom()
plt.show()

Answer:

import numpy as np
import matplotlib.pyplot as plt
fig, ax = plt.subplots(nrows=1, figsize=(4,4))
ax.imshow([[1, 2], [5, 6]])
ax.autoscale(False)  # disable autoscaling for all future plotting functions.
def onclick(event):
    if event.dblclick:
        plt.scatter(event.xdata, event.ydata, c='r')
fig.canvas.mpl_connect('button_press_event', onclick)
plt.get_current_fig_manager().toolbar.zoom()
plt.show()
1 Answers

I believe the problem comes from the autoscale features that kicks in whenever you call plt.scatter(). The solution is simply to disable autoscale (but draw the initial plot beforehand):

import matplotlib.pyplot as plt

fig, ax = plt.subplots(nrows=1, figsize=(4, 4))
plt.plot([0, 1, 2, 3], [10, 20, 30, 40])
fig.canvas.draw()  # force draw so that the axes are autoscaled here
ax.autoscale(False)  # disable autoscaling for all future plotting functions.


def onclick(event):
    if event.inaxes and event.dblclick:
        plt.scatter(event.xdata, event.ydata, marker='o', s=10, c='r')


fig.canvas.mpl_connect('button_press_event', onclick)
plt.show()
Related