0

I've integrated matplotlib in tkinter and I want to keep the pan button on by default and then hide the pan button

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),
    ('Pan', 'Pan axes with left mouse, zoom with right', 'move', 'pan'),
    # ('Subplots', 'Configure subplots', 'subplots', 'configure_subplots'),
    (None, None, None, None),
    ('Save', 'Save the figure', 'filesave', 'save_figure'),
  )

enter image description here

How do I achieve this?

1 Answer 1

1

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()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.