nicholas@886: import sys nicholas@886: import os nicholas@886: import csv nicholas@886: import matplotlib.pyplot as plt nicholas@886: import numpy as np nicholas@886: nicholas@886: rating_folder = 'ratings/' # folder with rating csv files nicholas@886: show_individual = 'frank' nicholas@886: nicholas@886: # get every csv file in folder nicholas@886: for file in os.listdir(rating_folder): # You have to put this in folder where rating csv files are. nicholas@886: if file.endswith(".csv"): nicholas@886: page_name = file[:-4] # file name (without extension) is page ID nicholas@886: nicholas@886: # get header nicholas@886: with open(rating_folder+file, 'r') as readfile: # read this csv file nicholas@886: filereader = csv.reader(readfile, delimiter=',') nicholas@886: headerrow = filereader.next() # use headerrow as X-axis nicholas@886: headerrow = headerrow[1:] nicholas@886: nicholas@886: # read ratings into matrix nicholas@886: ratings = np.loadtxt(open(rating_folder+file,"rb"), nicholas@886: delimiter=",", nicholas@886: skiprows=1, nicholas@886: usecols=range(1,len(headerrow)+1) nicholas@886: ) nicholas@886: nicholas@886: # draw boxplot nicholas@886: plt.boxplot(ratings) nicholas@886: nicholas@886: # add rating of individual(s) nicholas@886: with open(rating_folder+file, 'r') as readfile: # read this csv file nicholas@886: filereader = csv.reader(readfile, delimiter=',') nicholas@886: headerrow = filereader.next() # use headerrow as X-axis nicholas@886: headerrow = headerrow[1:] nicholas@886: markerlist = ["x", ".", "o", "*", "+", "v", ">", "<", "8", "s", "p"] nicholas@886: increment = 0 nicholas@886: linehandles = [] nicholas@886: legendnames = [] nicholas@886: for row in filereader: nicholas@886: subject_id = row[0][:-4] nicholas@886: if subject_id in show_individual: nicholas@886: plothandle, = plt.plot(range(1,len(row)), # x-values nicholas@886: row[1:], # y-values: csv values except subject name nicholas@886: color='k', nicholas@886: marker=markerlist[increment%len(markerlist)], nicholas@886: markersize=10, nicholas@886: linestyle='None', nicholas@886: label=subject_id nicholas@886: ) nicholas@886: increment += 1 # increase counter nicholas@886: linehandles.append(plothandle) nicholas@886: legendnames.append(subject_id) nicholas@886: plt.legend(linehandles, legendnames, nicholas@886: loc='upper right', nicholas@886: bbox_to_anchor=(1.1, 1), borderaxespad=0.) nicholas@886: nicholas@886: nicholas@886: plt.xlabel('Fragment') nicholas@886: plt.title('Box plot '+page_name) nicholas@886: plt.xlim(0, len(headerrow)+1) # only show relevant region, leave space left & right) nicholas@886: plt.xticks(range(1, len(headerrow)+1), headerrow) # show fragment names nicholas@886: nicholas@886: plt.ylabel('Rating') nicholas@886: plt.ylim(0,1) nicholas@886: nicholas@886: #plt.show() # show plot nicholas@886: #exit() nicholas@886: nicholas@886: plt.savefig(rating_folder+page_name+"-box.png") nicholas@886: plt.close()