python使用matplotlib模块绘制多条折线图、散点图
今天想直观的展示一下数据就用到了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.散点图
挺有意思的。
相关推荐
jiahaohappy 2020-05-11
maybeyoucan 2019-11-04
CloudXli 2019-10-29
Cyuhong 2019-03-19
chaigang 2020-09-10
chaigang 2020-06-05
sxyhetao 2020-05-01
georgeandgeorge 2020-03-28
zhangxiaojiakele 2020-02-24
大史哥哥 2020-02-23
Norsaa 2019-12-29
星辰大海的路上 2019-06-18
znancy 2019-04-02
diyanpython 2019-03-19
牧灵泉的崖巢 2019-05-09
sesiria 2019-04-11
飞天喜欢你 2017-06-30
石山下 2016-03-21
paopao00 2019-06-27