X Tutup
Skip to content

Can not maintain zoom level when left key is pressed #6888

@barpaum

Description

@barpaum
  • Matplotlib version: 2.0.0b1+117.gb2b37fb, Python version: 3.5.2 and Platform: Ubuntu 16.04 64bit
  • Matplotlib from source and Python is shipped with system
  • The following code is an example has the issue. It's from matplotlib example 'buttons.py', but modified with keyboard event supporting ( I only inserted lines from "istep=0" to "fig.canvas.mpl_connect" ). The problem is that after I zoomed in the figure with the zoom tool of matplotlib, the zooming level maintained when I pressed the next and previous button and right key, but lost when I pressed left key. I'm not sure whether this issue comes from my code or it is just a feature not supported by matplotlib.
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Button

freqs = np.arange(2, 20, 3)

fig, ax = plt.subplots()
plt.subplots_adjust(bottom=0.2)
t = np.arange(0.0, 1.0, 0.001)
s = np.sin(2*np.pi*freqs[0]*t)
l, = plt.plot(t, s, lw=2)


class Index(object):
    ind = 0

    def next(self, event):
        self.ind += 1
        i = self.ind % len(freqs)
        ydata = np.sin(2*np.pi*freqs[i]*t)
        l.set_ydata(ydata)
        plt.draw()

    def prev(self, event):
        self.ind -= 1
        i = self.ind % len(freqs)
        ydata = np.sin(2*np.pi*freqs[i]*t)
        l.set_ydata(ydata)
        plt.draw()

istep=0

def on_keyboard(event):
    global istep
    global l
    if event.key == 'right':
        istep+=1
    elif event.key == 'left':
        istep-=1
    ydata = np.sin(2*np.pi*freqs[istep%len(freqs)]*t)
    l.remove()
    l,=ax.plot(t,ydata)
    fig.canvas.draw()

fig.canvas.mpl_connect('key_press_event', on_keyboard)

callback = Index()
axprev = plt.axes([0.7, 0.05, 0.1, 0.075])
axnext = plt.axes([0.81, 0.05, 0.1, 0.075])
bnext = Button(axnext, 'Next')
bnext.on_clicked(callback.next)
bprev = Button(axprev, 'Previous')
bprev.on_clicked(callback.prev)

plt.show()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      X Tutup