It is possible to use your own axes class. In your case you can inherit from matplotlib.axes.Axes
and change the drag_pan
method to always act as though the 'x' key is being pressed. However the zooming doesn't seem to be defined in that class. The following will only allow x axis panning:
import matplotlib
import matplotlib.pyplot as plt
class My_Axes(matplotlib.axes.Axes):
name = "My_Axes"
def drag_pan(self, button, key, x, y):
matplotlib.axes.Axes.drag_pan(self, button, 'x', x, y)
matplotlib.projections.register_projection(My_Axes)
figure = plt.figure()
ax = figure.add_subplot(111, projection="My_Axes")
ax.plot([0, 1, 2], [0, 1, 0])
plt.show()
For the zooming, you may have to look at the toolbar control itself. The NavigationToolbar2 class has the drag_zoom
method which seems to be what's relevant here, but tracking down how that works is quickly complicated by the fact that the different backends all have their own versions (e.g. NavigationToolbar2TkAgg
edit
You can monkeypatch the desired behaviour in:
import types
def press_zoom(self, event):
event.key='x'
matplotlib.backends.backend_tkagg.NavigationToolbar2TkAgg.press_zoom(self,event)
figure.canvas.toolbar.press_zoom=types.MethodType(press_zoom, figure.canvas.toolbar)
You could do it properly and make a subclass of the toolbar, but you have to then create instances of Figure, FigureCanvas and your NavigationToolbar and put them in a Tk app or something. I don't think there's a really straightforward way to just use your own toolbar with the simple plotting interface.