2

I have a figure with different plots on several axes. Some of those axes do not play well with some of the navigation toolbar actions. In particular, the shortcuts to go back to the home view and the ones to go to the previous and next views.

Is there a way to disable those shortcuts only for those axes? For example, in one of the two in the figure from the example below.

import matplotlib.pyplot as plt

# Example data for two plots
x1 = [1, 2, 3, 4]
y1 = [10, 20, 25, 30]
x2 = [2, 3, 4, 5]
y2 = [5, 15, 20, 25]

# Create figure and axes objects
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 5))

# Plot data on the first axis
ax1.plot(x1, y1)
ax1.set_title("First Plot")

# Plot data on the second axis
ax2.plot(x2, y2)
ax2.set_title("Second Plot")

# Show plot
plt.show()

Edit 1:

The following method will successfully disable the pan and zoom tools from the GUI toolbox in the target axis.

ax2.set_navigate(False)

However, the home, forward, and back buttons remain active. Is there a trick to disable also those buttons in the target axis?

Stefano
  • 359
  • 1
  • 5
  • 16
  • What do the home, forward, and back buttons do when you can't pan and zoom? – aaron Feb 08 '23 at 16:19
  • @aaron - They behave as usual and take other plots (like the one in the axis `ax1` in the example) to their home, forward, and back states. The problem is that in my actual code (not the example one), changes in plots in axis `ax2` are triggered from user interactions with plots in `ax1` (using the function `mpl_connect` to detect mouse clicks, etc) and there is when the home, forward, and back buttons do not play nice. – Stefano Feb 08 '23 at 21:18
  • Can you edit the question to show that so that an answer can be verified? – aaron Feb 09 '23 at 00:44
  • Do you want to disable the home, forward and back buttons *without* disabling the pan and zoom buttons? – aaron Feb 09 '23 at 06:52
  • @Aaron - I want to disable all buttons for `ax2` only. – Stefano Feb 16 '23 at 15:47

3 Answers3

1

It worked for me when I did as below:

import matplotlib
matplotlib.rcParams['toolbar'] = 'None'
plt = matplotlib.pyplot
# Example data for two plots
x1 = [1, 2, 3, 4]
y1 = [10, 20, 25, 30]
x2 = [2, 3, 4, 5]
y2 = [5, 15, 20, 25]
# Create figure and axes objects
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 5))
# Plot data on the first axis
ax1.plot(x1, y1)
ax1.set_title("First Plot")
# Plot data on the second axis
ax2.plot(x2, y2)
ax2.set_title("Second Plot")
# Show plot
plt.show()

Source.

  • Thank you. This is close but it has a global effect in the figure instead of affecting only `ax2`. – Stefano Feb 16 '23 at 15:39
1

As I am not sure to fully understand which button you want to enable and disable, I provide here a code to customize your navigation toolbar individually across the different axes. It is using some matplotlib backend tools and you can change the line inside the class definition so it fits with what you want:

from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar

# Create a custom toolbar that only includes the pan and zoom tools
class CustomToolbar(NavigationToolbar):
    toolitems = [t for t in NavigationToolbar.toolitems if
                 t[0] in ('Pan', 'Zoom')]

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 5))

ax1.plot(x1, y1)
ax1.set_title("First Plot")

ax2.plot(x2, y2)
ax2.set_title("Second Plot")

# Set the custom toolbar for the second axis
ax2_navigation_toolbar = CustomToolbar(ax2.figure.canvas, ax2)
ax2.navigation_toolbar = ax2_navigation_toolbar

# Show plot
plt.show()

Hope this helps.

Barbara Gendron
  • 385
  • 1
  • 2
  • 16
  • Thank you. I am not familiar with the QT, so I will look further into the functions you used. However, when I run the code you gave, I get an error in the following line `ax2_navigation_toolbar = CustomToolbar(ax2.figure.canvas, ax2)` that says `TypeError: arguments did not match any overloaded call: QToolBar(str, parent: QWidget = None): argument 1 has unexpected type 'AxesSubplot' QToolBar(parent: QWidget = None): argument 1 has unexpected type 'AxesSubplot'`. – Stefano Feb 16 '23 at 15:36
0

You can try to use ax2.get_xaxis().set_visible(False)

angwrk
  • 394
  • 1
  • 8
  • Thanks, but this will hide both the tick marks and labels of the x-axis. However, it will not affect the navigation tool bar. – Stefano Feb 16 '23 at 15:33