Home > database >  How to plot data from multiple text files in a single graph in Tkinter
How to plot data from multiple text files in a single graph in Tkinter

Time:08-25

I have finished the code so far that I have created a list that stores all textdatein and then iterates over them. However, it currently overwrites all previous text files and only plots the last one. What is missing in my code that all previous text files are saved and plotted and the last one too?

    def openFile(self):
        filelist = []
        for i in range(2):
            files = filedialog.askopenfilename()
            filelist.append(files)

        for fname in filelist:
            with open(fname) as f:
                lines = f.readlines()
                x = [float(line.split(",")[0]) for line in lines]
                y = [float(line.split(",")[1]) for line in lines]
                self.plot4.set_data(x,y)
                self.ax4.set_xlim(min(x),max(x))
                self.ax4.set_ylim(min(y),max(y))
                self.canvas.draw_idle()
                self.canvas.is_saving()

CodePudding user response:

Problem are NOT files.

You replace values on graph using .set_data(x,y)

You should use plot(x, y) to add new plot/line to graph.

Problem can make also .set_xlim() .set_y_lim() because it will set for last x, y. And this may need to put data from all files in lists all_x all_y and at the end use .set_xlim(all_x) .set_ylim(all_y)

Because I can't test it so I don't know if it will need plot(..., ax=self.ax4) or self.ax4.plot(...)


It can be something like this:

def openFile(self):
        filelist = []
        
        for i in range(2):
            files = filedialog.askopenfilename()
            if files:
                filelist.append(files)

        # --- before loop ---
        
        all_x = []
        all_y = []
        
        # --- loop ---
        
        for fname in filelist:
            with open(fname) as f:
                lines = f.readlines()
                x = [float(line.split(",")[0]) for line in lines]
                y = [float(line.split(",")[1]) for line in lines]
                
                self.ax4.plot(x, y)
                
                all_x  = x
                all_y  = y
                
        # --- after loop ---
        
        self.ax4.set_xlim(min(all_x), max(all_x))
        self.ax4.set_ylim(min(all_y), max(all_y))
        
        self.canvas.draw_idle()
        self.canvas.is_saving()

EDIT:

Minimal working code

import tkinter
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
import numpy as np

root = tkinter.Tk()

fig = Figure(figsize=(5, 4), dpi=100)
canvas = FigureCanvasTkAgg(fig, master=root)
canvas.get_tk_widget().pack()

ax = fig.add_subplot(111)

# --- before loop ---

all_x = []
all_y = []

# --- loop ---

for a in range(1, 10):
    
    t = np.arange(0, 3, .01)
    x = list(t)
    y = list(a * np.sin(np.pi * t))
    
    ax.plot(x, y)

    all_x  = x
    all_y  = y

# --- after loop ---

ax.set_xlim(min(all_x)-0.1, max(all_x) 0.1)   # margin 0.1  
ax.set_ylim(min(all_y)-0.5, max(all_y) 0.5)   # margin 0.5

canvas.draw()

# ---

tkinter.mainloop()

Result:

enter image description here

  • Related