cassiePython 2018-09-25
今天想直观的展示一下数据就用到了matplotlib模块,之前都是一张图只有一条曲线,现在想同一个图片上绘制多条曲线来对比,实现很简单,具体如下:
#!usr/bin/env python #encoding:utf-8 ''' __Author__:沂水寒城 功能:折线图、散点图测试 ''' import random import matplotlib import matplotlib.pyplot as plt def list2mat(data_list,w): ''' 切片、转置 ''' mat=[] res=[] for i in range(0,len(data_list)-w+1,w): mat.append(data_list[i:i+w]) for i in range(len(mat[0])): one_list=[] for j in range(len(mat)): one_list.append(mat[j][i]) res.append(one_list) return res def draw_pic_test(): ''' 作图 ''' data_list=[] for i in range(100): data_list.append(random.randint(2,150)) month_list=range(1,11,1) mat=list2mat(data_list,w=10) for one_list in mat: one_list=[int(one) for one in one_list] plt.plot(month_list,one_list,"x-",label="test_zhexian") plt.savefig('test_zhexian.png') plt.close() for one_list in mat: one_list=[int(one) for one in one_list] plt.scatter(month_list,one_list,marker='x',label='test_sandian',s=30) plt.savefig('test_sandian.png') plt.close() if __name__ == '__main__': draw_pic_test()
结果如下:
1.折线图
2.散点图
挺有意思的。