目标
如图所示,在.py中,c是一个二维数组,其中一维做横坐标,另一维做纵坐标,写到.m中。画图。
本来想用正则在原文件中替换的,太麻烦,于是按照格式写了一个新文件。
import os
import glob
import cPickle as pickle
file_w = open('WinRecall.m','w')
print >> file_w, 'figure(1);'
print >> file_w, 'hold on;'
color = ['k','b','g','r','c','m','y',':k',':b',':g',':r',':c',':m',
':y','--k','--b','--g','--r','--c','--m']
all_files = glob.glob('*.pkl') #只列出当前文件夹下.pkl文件
i = 0
for files in all_files:
f = file(files,'rb')
c = pickle.load(f)
recall = c['rec']
recall = ' '.join(str(i) for i in recall) #array转化成string
precision = c['prec']
precision = ' '.join(str(i) for i in precision)
objects_name = files.split('_')[0]
print >> file_w, objects_name + ' = ' + '[' + recall + '];'
print >> file_w, 'Prec = ' + '[' + precision + '];'
print >> file_w, 'plot(Prec, '+ objects_name + ", '" + color[i] + "', " + "'linewidth', 2);"
i = i+1
print >> file_w, "legend('aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus', 'car', 'cat', 'chair', 'cow', 'diningtable', 'dog', 'horse', 'motorbike', 'person', 'pottedplant', 'sheep', 'sofa', 'train', 'tvmonitor');;"
print >> file_w, 'hold off;'
print >> file_w, "xlabel('Precision');"
print >> file_w, "ylabel('Recall');"
print >> file_w, "grid on;"
print >> file_w, "title('PrecisionRecall.m')"
file_w.close()
//画图看一下pottedplant的数据
import matplotlib.pyplot as plt
f = file('pottedplant_pr.pkl','rb')
c = pickle.load(f)
recall = c['rec']
precision = c['prec']
print recall
print precision
plt.plot(precision, recall)
plt.savefig("pottedplat.jpg")