平时压力测试,生成一些数据后分析,直接看 log 不是很直观,前段时间看到公司同事分享了一个绘制图表python 模块 : plotly, 觉得很实用,利用周末时间熟悉下。
plotly
plotly 主页 : https://plot.ly/python/
安装
在 ubuntu 环境下,安装 plotly 很简单
python 版本2.7+
1
|
$ sudo pip install plotly |
绘图
在 plotly 网站注册后,可以直接将生成的图片保存到网站上,便于共享保存。
这里使用离线的接口,生成的 html 保存在本地文件
绘制直线图
先随便搞一组数据用来绘制图表
1
2
3
4
5
6
7
8
9
|
lcd@ubuntu:~ / $ cat gen_log.sh #!/bin/bash count = $ 1 while [ $count - gt 0 ] do sar - n DEV 1 1 | grep "Average:" | grep "eth0" | awk '{print $4,$5,$6}' count = $(($count - 1 )) done lcd@ubuntu:~ / $ sh gen_log.sh 1000 > log.txt |
通过上述脚本,获取每秒钟网卡的3个数据,记录文本,利用 ploty 按时间绘制成直线图,实现如下:
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
|
#!/usr/bin/env python import plotly.offline as pltoff import plotly.graph_objs as go def line_plots(name = "line_plots.html" ): dataset = { 'time' : [], 'rx' : [], 'tx' : [], 'util' : [] } with open ( "./log.txt" ) as f: i = 0 for line in f: items = line.split() dataset[ 'time' ].append(i) dataset[ 'rx' ].append(items[ 0 ]) dataset[ 'tx' ].append(items[ 1 ]) dataset[ 'util' ].append(items[ 2 ]) i + = 1 data_g = [] # 构建 time - rx 数据关系,折线图 tr_rx = go.Scatter( x = dataset[ 'time' ], y = dataset[ 'rx' ], name = 'rx' ) data_g.append(tr_rx) tr_tx = go.Scatter( x = dataset[ 'time' ], y = dataset[ 'tx' ], name = 'tx' ) data_g.append(tr_tx) tr_util = go.Scatter( x = dataset[ 'time' ], y = dataset[ 'util' ], name = 'util' ) data_g.append(tr_util) # 设置图表布局 layout = go.Layout(title = "Line plots" , xaxis = { 'title' : 'time' }, yaxis = { 'title' : 'value' }) fig = go.Figure(data = data_g, layout = layout) # 生成离线html pltoff.plot(fig, filename = name) if __name__ = = '__main__' : line_plots() |
生成图表如下所示 :
line_plot
柱形图
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
|
#!/usr/bin/env python import plotly.offline as pltoff import plotly.graph_objs as go def bar_charts(name = "bar_charts.html" ): dataset = { 'x' :[ 'man' , 'woman' ], 'y1' :[ 35 , 26 ], 'y2' :[ 33 , 30 ]} data_g = [] tr_y1 = go.Bar( x = dataset[ 'x' ], y = dataset[ 'y1' ], name = '2016' ) data_g.append(tr_y1) tr_y2 = go.Bar( x = dataset[ 'x' ], y = dataset[ 'y2' ], name = '2017' ) data_g.append(tr_y2) layout = go.Layout(title = "bar charts" , xaxis = { 'title' : 'x' }, yaxis = { 'title' : 'value' }) fig = go.Figure(data = data_g, layout = layout) pltoff.plot(fig, filename = name) if __name__ = = '__main__' : bar_charts() |
bar char
饼状图
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
#!/usr/bin/env python import plotly.offline as pltoff import plotly.graph_objs as go def pie_charts(name = 'pie_chart.html' ): dataset = { 'labels' :[ 'Windows' , 'Linux' , 'MacOS' ], 'values' :[ 280 , 10 , 30 ]} data_g = [] tr_p = go.Pie( labels = dataset[ 'labels' ], values = dataset[ 'values' ] ) data_g.append(tr_p) layout = go.Layout(title = "pie charts" ) fig = go.Figure(data = data_g, layout = layout) pltoff.plot(fig, filename = name) if __name__ = = '__main__' : pie_charts() |
原文链接:http://www.jianshu.com/p/c2997494085b