1

When using matplotlib normally the navigation toolbar has 8 options including an "Edit axis, curves and image parameters" options highlighted here in blue.

import numpy as np
import matplotlib.pyplot as plt
   
y = [i**2 for i in range(101)]
plt.plot(y)
plt.show()

But when plotted in tkinter GUI, there are only 7 options - the edit option is the one needed and is missing. Also, the subplot options dialogue looks completely different.

from tkinter import *
import tkinter.ttk as ttk
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
fig = plt.Figure(figsize = (5, 5),dpi = 100)
y = [i**2 for i in range(101)]
plot1 = fig.add_subplot(111)
plot1.plot(y)

canvas = FigureCanvasTkAgg(fig,master = root)
canvas.draw()
canvas.get_tk_widget().pack(fill=BOTH, expand=True)

toolbar = NavigationToolbar2Tk(canvas,root)
toolbar.update()
canvas.get_tk_widget().pack(fill=BOTH, expand=True)

root.mainloop()

From this post here it seems like this is something to do with default options being overwritten by the tkinter library, specifically the NavigationToolbar2Tk class. Can someone help with adding the edit option back into the tkinter toolbar?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
camel_case
  • 13
  • 3

1 Answers1

0

Matplotlib can use different backends to display plot window.

Normally it uses PyQt but you can run it with Tk

import numpy as np
import matplotlib.pyplot as plt

plt.switch_backend('tkagg')

y = [i**2 for i in range(101)]
plt.plot(y)
plt.show()

PyQt uses NavigationToolbar2QT which adds button to run function edit_properties
but Tk uses NavigationToolbar2Tk which doesn't have this function.

They also use different code to create window with subplot options.

The same problem is when you create Tkinter program.It has to use NavigationToolbar2Tk which doesn't have edit_properties.

You will have to create own NavgationToolbar and add own function to edit properites.

import tkinter as tk  # PEP8: `import *` is not preferred
import tkinter.ttk as ttk
import tkinter.messagebox as msg
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
import matplotlib.pyplot as plt

class MyToolbar(NavigationToolbar2Tk):
    
    def __init__(self, canvas, parent):
        
        self.toolitems = (
            ('Home', 'Lorem ipsum dolor sit amet', 'home', 'home'),
            ('Back', 'consectetuer adipiscing elit', 'back', 'back'),
            ('Forward', 'sed diam nonummy nibh euismod', 'forward', 'forward'),
            (None, None, None, None),
            ('Pan', 'tincidunt ut laoreet', 'move', 'pan'),
            ('Zoom', 'dolore magna aliquam', 'zoom_to_rect', 'zoom'),

            # new button in toolbar
            ("Customize", "Edit axis, curve and image parameters", "subplots", "edit_parameters"),                    

            (None, None, None, None),
            ('Subplots', 'putamus parum claram', 'subplots', 'configure_subplots'),
            ('Save', 'sollemnes in futurum', 'filesave', 'save_figure'),
            
        )
        super().__init__(canvas, parent)

    def edit_parameters(self):
        print("You have to create edit_parameters()")
        msg.showwarning("Warning", "You have to create edit_parameters()")
        
# --- main ---

y = [i**2 for i in range(101)]

root = tk.Tk()

fig = plt.Figure(figsize=(5, 5), dpi=100)
plot1 = fig.add_subplot(111)
plot1.plot(y)

canvas = FigureCanvasTkAgg(fig, master=root)
canvas.get_tk_widget().pack(fill='both', expand=True)
#canvas.draw()

#toolbar = NavigationToolbar2Tk(canvas, root)
toolbar = MyToolbar(canvas, root)  # <-- uses own Toolbar
#toolbar.update()

root.mainloop()
furas
  • 134,197
  • 12
  • 106
  • 148
  • Thanks furas for taking the time to reply. It is a shame that such a useful option is not already included. It is beyond my current ability to recreate the "figure options" popup so I will leave the chart static. – camel_case Aug 09 '21 at 22:30