Python Forum
Dynamically plotting graphs with matplotlib
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dynamically plotting graphs with matplotlib
#11
The example code below does not use pandas, it uses the data as .csv (which is text with commas, same as data in your first post)
It will plot all files in the data directory of the structure (tree) shown below:

Output:
PlotData ├── data │   └── csv │   └── testdata.csv ├── src │   └── PlotData.py └──venv
# I use the following path tree for code as presented:
# modify as you wish.Do
# PlotData
#   ├── data
#   │   └── csv
#   │       └── testdata.csv
#   ├── src
#   │   └── PlotData.py
#   └──venv
#
# code named PlotData.py
import os
from matplotlib import pyplot as plt
from pathlib import Path
import csv


class DataFormatter():
    def __init__(self):
        os.chdir(os.path.abspath(os.path.dirname(__file__)))
        datapath = Path('../data')
        self.csvpath = datapath / 'csv'

    def plotdata(self, filename):
        # time row[0] is left axis
        # 
        x0 = []
        y0 = []
        x1 = [] 
        y1 = [] 
        
        with filename.open() as fp: 
            creader = csv.reader(fp)
            # plot for row[1] == 1
            for n, row in enumerate(creader):
                if n == 0:
                    header = row
                else:
                    if row[1] == '1':
                        x1.append(row[0]) 
                        y1.append(float(row[2]))
                    else:
                        x0.append(row[0])
                        y0.append(float(row[2]))

        self.plotxy(x0,y0,header)
        self.plotxy(x1,y1,header)

    def plotxy(self, x, y, header):
        plt.plot(x, y, color = 'g')
        plt.xlabel(header[0])
        plt.ylabel(header[2])
        plt.show() 
#--------------- End of class DataFormatter ---------------


def App():
    df = DataFormatter()
    filelist = [filename for filename in df.csvpath.iterdir() if filename.is_file() \
        and filename.suffix == '.csv']
    for file in filelist:
        df.plotdata(file)


if __name__ == '__main__':
    App()
Plot when 2nd element of data roe = 0
   

Plot when 2nd element of data row = 1
   
Reply
#12
It is possible to make a new dataframe that consists of selected columns of another dataframe. If the signal names follow some pattern this can be done using "like".
import pandas as pd
import random


data = {
    "time": range(1000, 1010),
    "sw1": random.choices([0, 1], k=10),
    "sw2": random.choices([0, 1], k=10),
    "tmp1": [random.random() * 5 + 10 for _ in range(10)],
    "sw3": random.choices([0, 1], k=10),
    "tmp2": [random.random() * 5 + 11 for _ in range(10)],
    "tmp3": [random.random() * 5 + 12 for _ in range(10)],
}
df = pd.DataFrame(data)

switches = df.filter(regex=r"(time)|sw.$")
temperatures = df.filter(regex=r"(time)|tmp.$")
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to plot 2 graphs in one figure? man0s 1 1,382 Apr-25-2022, 09:18 AM
Last Post: Axel_Erfurt
  Plotting issue Matplotlib garam0 0 1,550 May-23-2020, 12:11 AM
Last Post: garam0
  plotting of graphs mudezda1 2 2,848 Feb-11-2019, 12:44 PM
Last Post: mudezda1
  Tuple Unpacking with graphs in matplotlib smw10c 2 6,021 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