Python备忘录

提取某个路径下的所有文件夹的名字

import os
path='./somepath'
l = os.listdir(path)
print l

提取某个路径下所有的文件

from os import walk
f = []
for (dirpath, dirnames, filenames) in walk(mypath):
    f.extend(filenames)
    break

逐行读取文件

 with open(filename) as fp:            
            for line in fp:

复制文件

from shutil import copyfile
copyfile(src, dst)

给字典排序

sorted_cat = sorted(catNinstance.items(), key=operator.itemgetter(1),reverse=True)

matplot cmap

import matplotlib.pyplot as plt

figsim, ax = plt.subplots(figsize=(15,15))
# vmin vmax用于固定颜色范围,确保在不同的figure中,同一个值对应的颜色相同
cax = ax.matshow(sub_prob_matrix, interpolation='nearest',cmap=plt.cm.Blues_r,vmin=0, vmax=1)
ax.grid(True)
#plt.title('Title')
plt.xticks(range(n_w), labels, rotation=90,size=20);
plt.yticks(range(n_w), labels,size =20);
figsim.colorbar(cax, ticks=[0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0])
plt.show()
figsim.savefig('name.pdf')
figsim.savefig('name.png')

颜色表选项:
https://matplotlib.org/examples/color/colormaps_reference.html

posted @ 2017-12-22 00:00  zhsuiy  阅读(284)  评论(0编辑  收藏  举报