Python Forum
Dynamically plotting graphs with matplotlib
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dynamically plotting graphs with matplotlib
#1
I'm trying to create an application that formats data from a data logger that I have coded using an Arduino. I have the code finished and the output data is, for example:

Quote:Time,Sw,28C5
53782,1,27.89
53792,1,26.34
53802,1,27.89
53812,1,27.89
53822,1,25.95
53832,0,27.89
53842,0,26.42
53852,0,24.49
53862,1,27.89
53872,1,27.89
53882,0,27.89
53892,1,30.07
53902,1,27.89
53912,1,24.67
53922,1,33.93
53932,1,31.45
53942,1,27.89
53952,0,24.68
53962,1,22.11
53972,1,21.79

I use pandas to put it into an excel sheet, but to plot the data as a graph is the part of which I am not sure of. As I want to use this application for any data logger I need to create (there will be more devices I'm making, which will all have different output data), I do not know the correct syntax to dynamically plot the data. As time will always be the X-axis, that part of the code is okay, but- for example- for another datalogger, I may have to plot more than 1 sensor (28C5, in this circumstance, is a DS18B20 temperature sensor). Could anyone help me with how to plot a graph for any number of columns of data? I have already used something like that in my code, but I'm not sure how to implement it into matplotlib. Here is my code (unfinished, just trying to make it functional for the moment):

import os
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox
import pandas as pd
import matplotlib.pyplot as plt


class DataFormatter():


    def __init__(self):


        read_path=os.listdir('E:/DataLogging/')

        self.root=tk.Tk()
        self.root.geometry('350x150')
        self.root.title('Data Formatter')
        self.root.iconbitmap('Y:/ENGINEERING/DATALOGGING/Files/datalog.ico')

        self.mainframe=tk.Frame(self.root,background='white')
        self.mainframe.pack(fill='both',expand=True)

        self.type_label=ttk.Label(self.mainframe,text='Select data type: ',background='white',pad=10)
        self.type_label.grid(row=0, column=0, padx=70)

        self.type_box=ttk.Combobox(self.mainframe, values=read_path)
        self.type_box.grid(row=1,column=0)

        format_button=ttk.Button(self.mainframe,text='Format',command=self.format_data)
        format_button.grid(row=2,column=2)

        self.graph_check=tk.Checkbutton(self.mainframe, text='Add a graph', background='white')
        self.graph_check.grid(row=2,column=0, pady=10)

        self.root.mainloop()

        return

    def format_data(self):


        data_type=self.type_box.get()

        read_path = f'E:/DataLogging/{data_type}'
        new_file = (f'Y:/ENGINEERING/DATALOGGING/{data_type}')

        if os.path.exists(new_file)==False:

            os.mkdir(new_file)

        else:

            os.chdir(new_file)

        write_text_path = f'{new_file}/Text'
        write_excel_path = f'{new_file}/Excel'
        file_list = os.listdir(f'{read_path}/')

        for file in file_list:

            write_path = f'{write_text_path}/Parsed_{file}'

            if os.path.exists(write_text_path):

                os.chdir(write_text_path)
                os.chdir(write_excel_path)

            else:

                os.mkdir(write_text_path)
                os.mkdir(write_excel_path)

            with open(f'{read_path}/{file}', 'r') as read_file, open(write_path, 'w') as write_file:

                read_file = read_file.readlines()

                for line in read_file:

                    line = line.strip()
                    if line.find("ERROR:"):
                        line.strip()

                    columns = line.split(',')

                    formatted_line = ''.join(f'{column:>15}' for column in columns)

                    write_file.write(f'{formatted_line}\n')

                write_file.close()

            with open(write_path, 'r') as data:

                txt_to_xl = file.replace('.txt', '.xlsx')
                excel_file = f'{new_file}/Excel/Parsed_{txt_to_xl}'
                df = pd.read_csv(data, delim_whitespace=True)
                df = df.set_index("Time")

                df.to_excel(excel_file)

                print(excel_file)
            XL_data = pd.read_excel(excel_file)
            plt.plot(XL_data.index,  #what do I put here for dynamic plotting?)
            plt.show()




        if write_text_path and write_excel_path:
          messagebox.showinfo('Datalog Formatter', 'Formatting successful!')

        else:
            messagebox.showerror('Datalog Formatter', 'Formatting failed! Check all used files and directories.')

if __name__ == '__main__':
    DataFormatter()
Reply


Messages In This Thread
Dynamically plotting graphs with matplotlib - by CAD79 - Apr-15-2024, 10:46 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to plot 2 graphs in one figure? man0s 1 1,429 Apr-25-2022, 09:18 AM
Last Post: Axel_Erfurt
  Plotting issue Matplotlib garam0 0 1,581 May-23-2020, 12:11 AM
Last Post: garam0
  plotting of graphs mudezda1 2 2,900 Feb-11-2019, 12:44 PM
Last Post: mudezda1
  Tuple Unpacking with graphs in matplotlib smw10c 2 6,089 Mar-23-2017, 05:13 PM
Last Post: smw10c

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020