今天想直观的展示一下数据就用到了matplotlib模块,之前都是一张图只有一条曲线,现在想同一个图片上绘制多条曲线来对比,实现很简单,具体如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
#!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.散点图
挺有意思的。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/Together_CZ/article/details/78947495