基于列绘图

时间:2017-05-16 16:41:10

标签: python pandas matplotlib

我有一个这个数据集(panda df),很多,但这是结构。

 Replication                   State Machine  Average Time  Std. Percent
          1                    IDLE    bend        1.0863         68.42
          1  State Bend real failur    bend       19.7653          8.24
          1                    BUSY    bend        0.3144         19.88
          1   State Bend Die change    bend       25.0000          3.47
          2   State Bend Die change    bend       25.0000          3.47
          2  State Bend real failur    bend       20.4153          6.88
          2                    BUSY    bend        0.3058         19.93
          2                    IDLE    bend        1.0742         69.73
          3                    IDLE    bend        1.0803         69.25
          3   State Bend Die change    bend       25.0000          3.47
          3  State Bend real failur    bend       19.4699          7.23
          3                    BUSY    bend        0.3117         20.04

我想做的是制作4个直方图(每个状态一个)。我想在x轴上复制,并且'std。的值。 y轴上的“百分比”或“平均时间”。任何人都可以对如何绘制这些内容有所了解吗?

这是我到目前为止所做的代码,我没有做任何循环,因为我想让它与一个工作,然后我可以循环所有后记

import pandas as pd
import matplotlib.pyplot as plt

data = pd.read_csv('frequencies50cleaned.csv', sep=',')
data.columns= ['Replication', 'State', 'Machine', 'Average Time','Std. Percent']


bend = data[data['Machine'] == 'bend']
punch = data[data['Machine'] == 'punch']
form = data[data['Machine'] == 'form']
shear = data[data['Machine'] == 'shear']

bend = bend.sort(columns='Replication')
x = bend[bend['State'] == 'IDLE']['Replication']
y = bend[bend['State'] == 'IDLE']['Std. Percent']

punch = punch.sort(columns='Replication')
z = punch[punch['State'] == 'IDLE']['Replication']
p = punch[punch['State'] == 'IDLE']['Std. Percent']

bend.hist(column=['Replication'])
#plt.clf()
#plt.plot(x,y)
#plt.plot(z,p)
#plt.show()

1 个答案:

答案 0 :(得分:0)

所以我得到了代码。我会在这里发布,希望其他人可以使用它: - )

将pandas导入为pd     将matplotlib.pyplot导入为plt     导入numpy为np

# Prefix for the scenario
scen_name = "scenario1"

# Importing the Data
data = pd.read_csv(scen_name+'_frequencies50limcleaned.csv', sep=',')
data.columns= ['Replication', 'State', 'Machine', 'Average Time','Std. Percent']

# Conforming the data
data.State = data.State.replace('State Bend Blocked', 'Blocked')
data.State = data.State.replace('State Punch Blocked', 'Blocked')
data.State = data.State.replace('State Shear Blocked', 'Blocked')
data.State = data.State.replace('State Form Blocked', 'Blocked')

data.State = data.State.replace('State Bend real failur', 'Real Failure')
data.State = data.State.replace('State Punch real failur', 'Real Failure')
data.State = data.State.replace('State Shear real failur', 'Real Failure')
data.State = data.State.replace('State Form real failur', 'Real Failure')

data.State = data.State.replace('State Bend Die change', 'Die Change')
data.State = data.State.replace('State Punch Die change', 'Die Change')
data.State = data.State.replace('State Shear Die change', 'Die Change')
data.State = data.State.replace('State Form Die change', 'Die Change')

# Splitting data on machine
bend = data[data['Machine'] == 'bend']
punch = data[data['Machine'] == 'punch']
form = data[data['Machine'] == 'form']
shear = data[data['Machine'] == 'shear']

report_vals = ['Average Time', 'Std. Percent']
states = ['IDLE', 'BUSY', 'Blocked']
post_fixes = ['avg_time', 'percent']
titles = ['Average time in state: ','Frequencies of in percent: ']

i = 0
for rep in report_vals:
    for state in states:
        bend = bend.sort_values('Replication')
        x = bend[bend['State'] == state]['Replication']
        punch = punch.sort_values('Replication')
        y = punch[punch['State'] == state]['Replication']
        form = form.sort_values('Replication')
        z = form[form['State'] == state]['Replication']
        shear = shear.sort_values('Replication')
        w = shear[shear['State'] == state]['Replication']

        b = bend[bend['State'] == state][rep]
        p = punch[punch['State'] == state][rep]
        f = form[form['State'] == state][rep]
        s = shear[shear['State'] == state][rep]

        fig, ax = plt.subplots()

        rects1 = plt.plot(x,b,'b',
                         label='Bend')

        rects2 = plt.plot(y,p,'g',
                         label='Punch')

        rects3 = plt.plot(z,f,'r',
                         label='Form')

        rects4 = plt.plot(w,s,'y',
                         label='Shear')

        plt.xlabel('Replication')
        plt.ylabel(rep)
        plt.title(titles[i]+state)
        plt.legend()

        plt.tight_layout()
        plt.savefig('/home/misterwhite/Dropbox/Aarhus Universitet/8. Semester/Modellering, simulation og analyse/Arena Project/Report/images/'+scen_name+'_'+state+'_'+post_fixes[i]+'.png')
    i +=1