😺

Tabular Data Plot

2023/09/07に公開
import pandas as pd
import matplotlib.pyplot as plt
from pathlib import Path

# import glob


class SingleLotDataModel:
    def __init__(self, csvPath):
        self.__loadData(csvPath=csvPath)
        self.timeColmun = "TimeFromStart"
        self.intervalName = ["INTERVAL"]
        self.__addTimeFromStart()
        print(self.dataFrame.columns)

    def __loadData(self, csvPath):
        try:
            self.dataFrame = pd.read_csv(
                filepath_or_buffer=csvPath, header=0, encoding="shift-jis"
            )
            print(self.dataFrame.head())
        except UnicodeDecodeError:
            print("encodingが正しくありません")
            raise
        except FileNotFoundError:
            print("Fileが見つかりません")
            raise
        except ValueError:
            print("Headerの指定が正しくありません")
            raise

    def __addTimeFromStart(self):
        self.dataFrame[self.timeColmun] = self.dataFrame[
            self.intervalName
        ].cumsum()
        self.time = self.dataFrame[self.timeColmun]
        # print(self.dataFrame.tail())

    # def showSelectedData(self, sensorNameList):
    #     time = self.dataFrame[self.timeColmun]
    #     selectedSensor = self.dataFrame[sensorNameList]
    #     figure = plt.figure()
    #     graph1 = figure.add_subplot(1, 1, 1)
    #     for colmun in selectedSensor.columns:
    #         graph1.plot(
    #             time.head(), self.dataFrame[colmun].head(), label=colmun
    #         )
    #     graph1.legend(title="Sensors")
    #     plt.show()
    #     plt.close()


class DataModel:
    def __init__(self, folderPath, fileType):
        self.loadFolder(folderPath=folderPath, fileType=fileType)

    def loadFolder(self, folderPath, fileType):
        filesPath = folderPath.glob(fileType)
        # print(list(filesName))
        self.lotDataList: SingleLotDataModel = []
        if fileType == "*.csv":
            for filePath in filesPath:
                self.lotDataList.append(SingleLotDataModel(csvPath=filePath))

    def showPlot(self, sensorNameList):
        figure = plt.figure()
        graphList = []
        minValue = float("inf")
        maxValue = -float("inf")

        for i, lotData in enumerate(self.lotDataList):
            time = lotData.time
            selectedSensor = lotData.dataFrame[sensorNameList]
            graphList.append(
                figure.add_subplot(len(self.lotDataList), 1, i + 1)
            )
            maxValue = (
                selectedSensor.max().max()
                if maxValue < selectedSensor.max().max()
                else maxValue
            )
            minValue = (
                selectedSensor.min().min()
                if minValue > selectedSensor.min().min()
                else minValue
            )
            for colmun in selectedSensor.columns:
                graphList[-1].plot(
                    time.head(), lotData.dataFrame[colmun].head(), label=colmun
                )
            graphList[-1].legend(
                title="Sensors", loc="upper left", bbox_to_anchor=(1, 1)
            )
            for graph in graphList:
                graph.set_ylim(
                    minValue * 1.1 if minValue < 0 else minValue * 0.9,
                    maxValue * 1.1 if maxValue > 0 else maxValue * 0.9,
                )
        figure.subplots_adjust(right=0.8)
        plt.show()
        plt.close()


if __name__ == "__main__":
    homePath = Path.home()
    folderPath = homePath / Path("Downloads/sampleCSV")
    fileType = "*.csv"
    sensorNameList = ["Sensor1", "Sensor2", "Sensor3", "Sensor4"]
    dataModel = DataModel(folderPath=folderPath, fileType=fileType)
    dataModel.showPlot(sensorNameList=sensorNameList)

Discussion