.py里的结果写到.m中

目标


如图所示,在.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")






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值