直接上代码:在一个图中,画多个子图,每个子图为折线图;
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @wangyoujin
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdate
import pandas as pd
def testDrawUserNetworkBehaviour(userId,date,col_frame,vec_attribute):
#子图个数
cnames = [ 'red', 'blue','green','indigo', 'gold','pink','black','springgreen','skyblue', 'yellow',
'slateblue','orange', 'forestgreen','orangered',
'greenyellow', 'seashell', 'mediumspringgreen',
'gray', 'darkturquoise', 'goldenrod', 'darkgreen','lightskyblue', 'lightpink',
'mediumvioletred', 'hotpink', 'firebrick', 'indianred',
'mistyrose', 'darkolivegreen', 'olive', 'darkseagreen', 'tomato',
'lightcoral', 'navajowhite', 'lime', 'palegreen', 'burlywood', 'fuchsia',
'papayawhip', 'blanchedalmond', 'chartreuse', 'dimgray', 'peachpuff',
'aquamarine', 'white', 'lightsalmon', 'darkslategray', 'brown', 'ivory',
'dodgerblue', 'peru', 'lawngreen', 'chocolate', 'crimson', 'lightseagreen',
'cyan', 'mintcream', 'silver', 'antiquewhite', 'mediumorchid', 'skyblue',
'gray', 'darkturquoise', 'goldenrod', 'darkgreen', 'floralwhite', 'darkviolet',
'darkgray', 'moccasin', 'saddlebrown', 'darkslateblue', 'deeppink', 'limegreen',
'darkmagenta', 'palegoldenrod', 'plum', 'turquoise', 'lightgoldenrodyellow',
'darkgoldenrod', 'lavender', 'maroon', 'yellowgreen', 'sandybrown', 'thistle',
'violet', 'navy', 'magenta', 'tan', 'rosybrown', 'olivedrab', 'lightblue',
'ghostwhite', 'honeydew', 'cornflowerblue', 'linen', 'darkblue', 'powderblue',
'seagreen', 'darkkhaki', 'snow', 'sienna', 'mediumblue', 'royalblue', 'lightcyan',
'mediumpurple', 'midnightblue', 'cornsilk', 'paleturquoise', 'bisque', 'slategray',
'darkcyan', 'khaki', 'wheat', 'teal', 'darkorchid', 'deepskyblue', 'salmon', 'darkred',
'steelblue', 'palevioletred', 'lightslategray', 'aliceblue', 'lightgreen', 'orchid',
'gainsboro', 'mediumseagreen', 'lightgray', 'mediumturquoise', 'lemonchiffon', 'cadetblue',
'lightyellow', 'lavenderblush', 'coral', 'purple', 'aqua', 'whitesmoke', 'mediumslateblue', 'darkorange',
'mediumaquamarine', 'darksalmon', 'beige', 'blueviolet', 'azure', 'lightsteelblue', 'oldlace']
number_subplot = len(vec_attribute) + 1
print(number_subplot)
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
fig = plt.figure(figsize=(30, 110))
ax = fig.add_subplot(number_subplot, 1, 1)
ax.xaxis.set_major_formatter(mdate.DateFormatter('%Y%m%d')) # 设置时间标签显示格式
ax.xaxis.set_major_locator(mdate.DayLocator())
ax.set_title("U AVIOUR")
for i in range(len(col_frame)):
print(i, col_frame[i], cnames[i],vec_attribute[i])
ax.plot(date, col_frame[i], '.-', color=cnames[i], label=vec_attribute[i])
plt.xticks(rotation=45) # 旋转45度显示
ax.legend(loc='upper left')
for j in range(len(col_frame)):
print(j, col_frame[j])
ax = fig.add_subplot(number_subplot, 1, j+2)
ax.xaxis.set_major_formatter(mdate.DateFormatter('%Y%m%d')) # 设置时间标签显示格式
ax.xaxis.set_major_locator(mdate.DayLocator())
ax.set_title(vec_attribute[j])
ax.plot(date, col_frame[j], 'r.-')
plt.xticks(rotation=45) # 旋转45度显示
plt.savefig(userId+'52882.png')
#plt.show()
if __name__ == '__main__':
userId = "TONG"
col_frame = [
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 8, 5, 12, 24, 117, 1376, 9, 402, 416, 730, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 0, 14, 0, 5, 2, 2, 6, 5, 1, 5,
0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 1, 0, 0, 2, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 1132, 915, 1565, 1487, 1431, 1122, 1203, 1293, 25, 734, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 5, 0, 0, 22, 35,
12, 478, 1268, 599, 155, 0, 4, 0, 6, 0, 0, 18, 0, 0, 0, 0, 3, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 466, 197, 474, 28, 5, 96, 1645, 673, 249, 84, 0, 0, 0, 0, 0, 0, 0, 0, 0, 49, 1, 359, 0, 13, 158, 46, 53,
510, 591, 294, 0, 7, 2, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 8, 0, 0, 0, 0],
[5, 129, 90, 27, 153, 65, 39, 58, 44, 11, 0, 0, 0, 0, 0, 0, 2, 890, 42, 676, 159, 102, 46, 45, 12, 62, 0, 22,
17, 18, 13, 46, 9, 14, 39, 13, 48, 72, 0, 4, 1, 2, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 9, 2, 0,
3, 0, 0, 0, 0, 3, 13, 0, 12, 21, 34, 5, 0, 21, 37, 25, 12, 89, 9, 17, 0, 12, 29, 11, 11, 5, 7, 0, 18],
[0, 0, 0, 5, 27, 4, 3, 1, 3, 3, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 5, 0, 3, 0, 2, 0,
0, 0, 1, 0, 2, 2, 30, 51, 70, 72, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[5, 129, 90, 32, 180, 69, 42, 59, 47, 14, 0, 0, 0, 0, 0, 0, 2, 892, 42, 676, 159, 102, 46, 46, 12, 62, 0, 24,
17, 18, 13, 51, 9, 17, 39, 15, 48, 72, 0, 1611, 1118, 2055, 1542, 1583, 2645, 2928, 2440, 690, 1548, 0, 0, 0,
0, 0, 0, 0, 0, 0, 64, 15, 375, 0, 43, 195, 60, 537, 1783, 1194, 467, 0, 24, 23, 40, 5, 0, 39, 37, 26, 12, 89,
16, 17, 0, 15, 29, 11, 21, 5, 7, 0, 18],
[5, 129, 90, 32, 180, 69, 42, 59, 47, 14, 0, 0, 0, 0, 0, 0, 2, 892, 42, 676, 159, 102, 46, 46, 12, 62, 0, 24,
17, 18, 13, 51, 9, 17, 39, 15, 48, 72, 0, 1611, 1118, 2055, 1542, 1583, 2645, 2928, 2440, 690, 1548, 0, 0, 0,
0, 0, 0, 0, 0, 0, 64, 15, 375, 0, 43, 195, 60, 537, 1783, 1194, 467, 0, 24, 23, 40, 5, 0, 39, 37, 26, 12, 89,
16, 17, 0, 15, 29, 11, 21, 5, 7, 0, 18],
[3, 119, 55, 19, 103, 45, 21, 46, 37, 9, 0, 0, 0, 0, 0, 0, 2, 827, 33, 651, 131, 79, 31, 31, 7, 39, 0, 10, 13,
7, 6, 35, 5, 11, 23, 11, 31, 17, 0, 440, 209, 463, 48, 137, 1457, 1530, 799, 643, 807, 0, 0, 0, 0, 0, 0, 0, 0,
0, 52, 5, 369, 0, 13, 164, 45, 65, 468, 237, 167, 0, 16, 20, 24, 3, 0, 13, 14, 19, 7, 83, 8, 6, 0, 10, 14, 6,
7, 4, 4, 0, 15],
[5, 129, 89, 31, 178, 67, 41, 59, 47, 13, 0, 0, 0, 0, 0, 0, 2, 890, 41, 675, 158, 100, 44, 45, 11, 61, 0, 22,
17, 17, 12, 49, 9, 16, 39, 15, 47, 71, 0, 1584, 1088, 2028, 1521, 1564, 2624, 2898, 2430, 688, 1538, 0, 0, 0,
0, 0, 0, 0, 0, 0, 61, 11, 375, 0, 33, 190, 58, 521, 1717, 1178, 453, 0, 22, 23, 37, 4, 0, 35, 36, 26, 11, 88,
14, 15, 0, 14, 27, 10, 21, 5, 7, 0, 18]]
date = pd.date_range('2019-01-22', '2019-04-22')
vec_attribute = ['/account/cus', '/managx_queryappllist.do',
'/account/querymng.do', '/accomanualdeduction.do',
'/accoo', 'caps', '/accfolistapplyno.do',
'/accuditing.do', '/action.do']
testDrawUserNetworkBehaviour(userId,date,col_frame,vec_attribute)
python Matplot画图、多个子图
最新推荐文章于 2024-08-04 09:30:31 发布