Home > Software engineering >  Keep pan on by default in matplotlib?
Keep pan on by default in matplotlib?

Time:08-29

I've integrated matplotlib in tkinter and I want to keep the pan button on by default and then enter image description here

How do I achieve this?

CodePudding user response:

You can try this simple code.
First remove unwanted buttons, in our case "Pan" and then enter "Pan" mode.

I used this page as a source for my answer (how to remove button).

This works for TK based GUI (your case):

import matplotlib.pyplot as plt
import numpy as np
from matplotlib import backend_bases

backend_bases.NavigationToolbar2.toolitems = (
    ('Home', 'Reset original view', 'home', 'home'),
    ('Back', 'Back to  previous view', 'back', 'back'),
    ('Forward', 'Forward to next view', 'forward', 'forward'),
    (None, None, None, None),
    ('Save', 'Save the figure', 'filesave', 'save_figure'),
)

# Data for plotting
t = np.arange(0.0, 2.0, 0.01)
s = 1   np.sin(2 * np.pi * t)

fig, ax = plt.subplots()

ax.plot(t, s)
ax.set(xlabel='time (s)', ylabel='voltage (mV)',
       title='Example plot')
ax.grid()

# Enter "pan" mode
fig.canvas.manager.toolbar.pan()

plt.show()

This works for QT based GUI:

import matplotlib.pyplot as plt
import numpy as np

# Data for plotting
t = np.arange(0.0, 2.0, 0.01)
s = 1   np.sin(2 * np.pi * t)

fig, ax = plt.subplots()

# Remove unwanted buttons
toolbar = plt.get_current_fig_manager().toolbar
unwanted_buttons = ['Pan']
for x in toolbar.actions():
    if x.text() in unwanted_buttons:
        toolbar.removeAction(x)

ax.plot(t, s)
ax.set(xlabel='time (s)', ylabel='voltage (mV)',
       title='Example plot')
ax.grid()

# Enter "pan" mode
fig.canvas.manager.toolbar.pan()

plt.show()
  • Related