1

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()

4
  • Please post the code for the whole class. It is unclear what the values of all the member variables are. Commented Aug 24, 2022 at 13:12
  • 1
    don't use .set_data(x,y) because it replaces previous values in plot. You should use plot(x,y) every data. OR maybe even plot(x1, y1, x2, y2, ...) But both versions will plot all on one grid. If you need separated plots then you will need also subplots() Commented Aug 24, 2022 at 13:42
  • 1
    BTW: when you click Cancel in dialog askopenfilename then files will be None and you should check this before you add to filelist Commented Aug 24, 2022 at 13:45
  • How can i post my all code? When I try to do this, it says I have too many characters Commented Aug 24, 2022 at 14:38

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks! I had a little error in thinking!
if it is small problem then you can write in comment. But rather you should create new question on new page - you will have more space for code and description. And code will be more readable. And new people may see it so more people may help you.
Do you know a smart solution, how i can fix this? : BTW: when you click Cancel in dialog askopenfilename then files will be None and you should check this before you add to filelist @furas
Can you help me with my other problem please? stackoverflow.com/questions/73486577/… @furas

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.