0

I am trying to embed a matplotlib FuncAnimation live updating plot to a tkinter window but I am getting 2 plots (one inside the tkinter window and other as an extra matplotlib plot) as shown below.

enter image description here

But I just need one inside the tkinter window.

This is the part of the code which generates the GUI.

Main Class

from tkinter import Tk

import VisualizationPage1

class VisualizationGui(Tk):
    def __init__(self, *args, **kwargs):
        Tk.__init__(self, *args, **kwargs)

        ....other code

        frame = VisualizationPage1.VisualizationPage1(self.global_container, self)
        frame.grid(row=0, column=0, sticky=NSEW)
        frame.tkraise()
        frame.set_ui()

if __name__ == "__main__":
    app = VisualizationGui()
    app.mainloop()

VisualizationPage1 Class

class VisualizationPage1():
    def __init__(self, parent, controller):
        super().__init__(parent, controller)

        # Define self.body_sublabelframe2 and other frames

        plotting_function()

    def plotting_function(self):
        x_val = []
        y_val = []

        figure1 = plt.figure(figsize=(8, 8), linewidth=5, edgecolor="#04253a", dpi=35)
        ax1 = figure1.add_subplot(111)
        canvas_1 = FigureCanvasTkAgg(figure1, self.body_sublabelframe2)
        canvas_1.get_tk_widget().grid(row=0, column=0)

        ax1.legend(["Some Legend"])
        ax1.set_xlabel("X Label")
        ax1.set_ylabel("Y Label")
        ax1.set_title("Plot Title")

        def animate(i):
            x_val.append(len(x_val))
            y_val.append(2 ** len(y_val))

            ax1.cla()
            ax1.plot(x_val, y_val)

        ani1 = FuncAnimation(figure1, animate, interval=100)
        plt.show()

The data is just some dummy data which I need to replace with some live data which is why I need this live updating feature in matplotlib.

  • while your code dosent seem a [mre], I think removing `plt.show()` will solve the primary issue in your code. Note that the plot is shown by `canvas_1.get_tk_widget().grid(row=0, column=0)` in tkinter while `plt.show()` offers a way to show the plot without tkinter. – Thingamabobs Jul 24 '22 at 19:03
  • I tried removing the `plt.show()` line but then the graph will render but the lines for the plot wont. – Amazonian_panda Jul 24 '22 at 19:07

0 Answers0