代码之家  ›  专栏  ›  技术社区  ›  Laxmikant

为多个属性绘制图形+matplotlib

  •  -1
  • Laxmikant  · 技术社区  · 7 年前

    我有以下数据:

    efficiency = [{'weights': '25', 'sets': '5', 'time': '8.48', 'machine': 'a'},
     {'weights': '100', 'sets': '25', 'time': '7.43', 'machine': 'a'},
     {'weights': '25', 'sets': '5', 'time': '8.70', 'machine': 'b'},
     {'weights': '100', 'sets': '25', 'time': '7.73', 'machine': 'b'},
     {'weights': '25', 'sets': '5', 'time': '7.14', 'machine': 'c'},
     {'weights': '100', 'sets': '25', 'time': '8.38', 'machine': 'c'},
     {'weights': '25', 'sets': '5', 'time': '6.11', 'machine': 'd'}...]
    

    它表示机器在给定重量和集合的情况下完成任务所需的时间(分钟)。

    我是新来的 matplotlib 所以不知道如何沿着x轴和y轴绘制图形,如图所示 Graph .

    图不必与此图相似。请给出任何代表机器所需时间的图表,

    基本上, 我想表示 time 需要一个 machine 对于给定的 weights sets . 我不知道如何表示特征(时间、机器、重量和集合)

    2 回复  |  直到 7 年前
        1
  •  1
  •   Shivid    7 年前

    鉴于数据是时变的,线图最适合它。

    老实说,如果我是您的话,我会对数据集做一些预处理,您需要做的事情非常简单;分别创建一个时间戳数组和每台机器的一些数组。不过,我不知道您在数据中保留的限制有多大。

    import matplotlib.pylab as plt
    import pandas as pd
    
    
    efficiency = [{'weights': '21', 'sets': '5', 'time': '1', 'machine': 'a'}, 
    {'weights': '93', 'sets': '25', 'time': '2', 'machine': 'a'},
    {'weights': '53', 'sets': '5', 'time': '1', 'machine': 'b'},
    {'weights': '61', 'sets': '25', 'time': '2', 'machine': 'b'},
    {'weights': '66', 'sets': '5', 'time': '1', 'machine': 'c'},
    {'weights': '90', 'sets': '25', 'time': '2', 'machine': 'c'},
    {'weights': '79', 'sets': '5', 'time': '1', 'machine': 'd'},
    {'weights': '100', 'sets': '25', 'time': '1', 'machine': 'd'}]
    
    
    
    class Efficiency:
        plt.rcParams['figure.figsize'] = (16,6)
        def __init__(self, data):
            self.data = data
            self.machines = []
            self.Timestamp = []
            for i in data:
                setattr(self, 'machine_'+i['machine'], dict(efficiency=[]))
                if not i['machine'] in self.machines: self.machines.append(i['machine'])
                if not i['time'] in self.Timestamp: self.Timestamp.append(i['time'])
    
        def readData(self):
            for i in self.data:
                getattr(self, 'machine_' + i['machine'])['efficiency'].append(float(i['weights']))
    
        def plotData(self):
            eff_dict = {}
            for i in self.machines:
                eff_dict['machine ' + i] = getattr(self, 'machine_' + i)['efficiency']
            self.df = pd.DataFrame(eff_dict, index= self.Timestamp)
    
            self.df.plot()
            plt.grid()
            plt.xticks(fontsize=12, rotation=90)
            plt.tick_params(axis='both', which='major', labelsize=18)
            plt.legend(fontsize=12)
            plt.tight_layout()
    
            plt.show()
            print(self.df)
    
    test = Efficiency(efficiency)
    test.readData()
    test.plotData()
    

    这是你的密码; enter image description here

    如果数据定义如下:

    import matplotlib.pylab as plt
    import pandas as pd
    import numpy as np
    import datetime
    
    efficiency2 = [{'weights': np.random.uniform(60,100, 10),'sets':np.random.randint(5,25, 10), 'time': np.arange(0,10,1), 'machine': 'a'},\
    {'weights': np.random.uniform(60,100, 10), 'sets': np.random.randint(5,25, 10), 'time': np.arange(0,10,1), 'machine': 'b'}]
    
    
    
    class Efficiency:
        plt.rcParams['figure.figsize'] = (16,6)
        def __init__(self, data):
            self.data = data
            self.machines = []
            self.Timestamp = []
            for i in data:
                setattr(self, 'machine_'+i['machine'], dict(weight=i['weights'], sets = i['sets']))
                if not i['machine'] in self.machines: self.machines.append(i['machine'])
            self.Timestamp = data[0]['time']
    
    
        def plotData(self):
            weight_dict = {}
            for i in self.machines:
                weight_dict['machine ' + i + ' weight'] = getattr(self, 'machine_' + i)['weight']
                weight_dict['machine ' + i + ' sets'] = getattr(self, 'machine_' + i)['sets']
            self.df = pd.DataFrame(weight_dict, index= self.Timestamp)
    
            self.df.plot()
            plt.grid()
            plt.xticks(fontsize=12, rotation=90)
            plt.tick_params(axis='both', which='major', labelsize=18)
            plt.legend(fontsize=20)
            plt.ylabel('(%)',fontsize=20)
            plt.xlabel('time',fontsize=20)
    
            plt.show()
            print(self.df)
    
    test = Efficiency(efficiency2)
    test.plotData()
    

    你会得到这个: enter image description here

    您可以有两个Y标签,需要 x_axis 格式和各种属性,这取决于您的喜好。

        2
  •  1
  •   ImportanceOfBeingErnest    7 年前

    数据看起来应该用条形图很好地表示,其中时间显示在y轴上。

    import pandas as pd
    import matplotlib.pyplot as plt
    
    df = pd.DataFrame(efficiency) # efficiency as defined in the question
    df["time"] = df["time"].astype(float)
    piv = df.pivot(columns="machine", index="weights", values="time")
    piv.plot(kind="bar")
    

    enter image description here

    或者由机器订购,

    piv = df.pivot(columns="weights", index="machine", values="time")
    piv.plot(kind="bar")
    

    enter image description here