2016-04-20 198 views
0

我有一個文件夾中有多個CSV文件。每個文件都有3列('real','user','sys'),每列在行中有10個時間值(float)。我的目標是從文件夾中讀取這些csv文件並製作一個box-whisker圖比較每個csv文件的值。我寫了下面的python代碼,它給了我多個(等於文件數)單獨的圖。通過從循環中刪除plt.show(),只顯示最後的圖形。從python中的多個CSV文件夾中繪製Box-Plot圖

我希望將這些圖形合併到一個圖形中,並將每個文件名稱作爲定義哪個box-whisker用於哪個文件的標籤。請幫助。

import csv 
import numpy as np 
import pandas 
import matplotlib.pyplot as plt 
import glob 

files = glob.glob ('/Users/Desktop/sample/*.csv') 
print files 

for file in files: 
    df = pandas.read_csv(file, sep=',') 

    LABELS = ["real", "user", "sys"] 

    plt.title('Time Taken by Classifier') 
    plt.xlabel('Time_Types') 
    plt.ylabel('Time_Value in (sec)') 


    df.boxplot() 

    plt.show() 

回答

0

你可能想沿着這些線路試一下:

import pandas as pd 
data = pd.DataFrame() 
for file in files: 

    df = pandas.read_csv(file, sep=',') 
    df['file'] = file 
    data = pd.concat([data, df]) 


LABELS = ["real", "user", "sys"] 

plt.title('Time Taken by Classifier') 
plt.xlabel('Time_Types') 
plt.ylabel('Time_Value in (sec)') 


data.boxplot(by='file') 

plt.show() 

見你如何使用關鍵字參數傳遞給修改佈局pd.DataFrame.boxplot() docsunderlying matplotlib implementation

+0

這是否回答你的問題? – Stefan

相關問題