0

I'm practicing some tkinter and matplotlib and I came across with something that I'd like to learn how to do. The idea is simple: for each plot, a new page (a new frame).

It's a simulation of a true scenario that I'm currently dealing with. In this example you should enter the number of plots that are supposed to happen then you click on 'File' > 'Open plot' and then the plotting begins. But the thing is... I know how to embed one graph into one frame, but I'd like to learn how to embed multiple graphs in different frames, so later on I can create a 'next' button which goes to the next frame that contains the next plot.

The following code is not too developed and probably contains mistakes, but I hope it informs the idea:

from tkinter import *
import matplotlib
matplotlib.use('TkAgg')
from matplotlib import pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg

x = [1,2,3]
y = [1,2,3]

numberofplots = None


numberofplots = eval(input('Number of plots: '))

class app():
    global numberofplots
    def __init__(self):
        root = Tk()
        root.geometry('640x400')
        menu_bar = Menu(root)
        root.configure(menu = menu_bar)
        menu_file = Menu(menu_bar)
        menu_bar.add_cascade(label='File',menu = menu_file)
        menu_file.add_command(label='Open plots',command = openPlots)
        root.mainloop()

def openPlots():
    global numberofplots
    fig = plt.figure()
    for i in range(numberofplots):
        plt.plot(x,y)

        # numberofplots = number of frames to be created

        frame = Frame(root)
        frame.pack()
        canvas = FigureCanvasTkAgg(fig, frame)
        canvas.show()
        canvas.get_tk_widget().pack(fill='both', expand=True)
        toolbar = NavigationToolbar2TkAgg(canvas, frame)
        toolbar.update()
        canvas._tkcanvas.pack(fill='both', expand=True)

run = app()
Victor José
  • 367
  • 1
  • 4
  • 12
  • What exactly is your question? You say `I'd like to learn how to embed multiple graphs in different frames` but that's pretty much what the code you posted is doing (apart from some small errors). I think the problem is you don't realize that there are multiple plots because the window resizes to fit only the first one, but the second one is actually beneath it and you will see it when you stretch the window vertically. – fhdrsdg Jun 04 '15 at 08:43
  • Thanks, I realized that you are right. My question in that case would be: how do I make the frame show me only the exact plot it was created for? Like... if I say nomberofplots = 3, there are supposed to be 3 frames, one for graph number 1, one for graph number 2 and one for graph number 3. How do I make each frame to pop up with its respective plot? So when I full screen the window I see only one graph at the time. – Victor José Jun 04 '15 at 10:52
  • 1
    I'd advise you to take a look at [this answer](http://stackoverflow.com/a/7557028/3714930). It's a nice example of how to ue multiple frames and switch between them. – fhdrsdg Jun 04 '15 at 12:05

0 Answers0