Python-based data charts and python data charts
In normal stress tests, some data is generated and analyzed. It is not intuitive to directly view the log. Some time ago, I saw my colleagues share a python module for drawing charts: plotly, which is very practical, familiarize yourself with the weekend time.
Plotly
Plotly home: https://plot.ly/python/
Install
Installing plotly in ubuntu is simple.
Python version 2.7 +
$ sudo pip install plotly
Plotting
After registering a plotly website, you can directly Save the generated images to the website for shared storage.
The offline interface is used to save the generated html to a local file.
Draw a line chart
Create a set of data to draw charts.
lcd@ubuntu:~/$ cat gen_log.sh #!/bin/bashcount=$1while [ $count -gt 0 ]do sar -n DEV 1 1 | grep "Average:" | grep "eth0" | awk '{print $4,$5,$6}' count=$(($count-1))donelcd@ubuntu:~/$ sh gen_log.sh 1000 > log.txt
Use the above script to obtain the three data records of the network card per second, record the text, and use the ploty to draw a line chart by time. The implementation is as follows:
#! /Usr/bin/env pythonimport plotly. offline as pltoffimport plotly. graph_objs as godef 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 ['til']. append (items [2]) I + = 1 data_g = [] # Build the time-rx Data Relationship, line chart 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) # Set the chart layout = go. layout (title = "Line plots", xaxis = {'title': 'time'}, yaxis = {'title': 'value'}) fig = go. figure (data = data_g, layout = layout) # generate offline html pltoff. plot (fig, filename = name) if _ name __= = '_ main _': line_plots ()
The generated chart is as follows:
Line_plot
Column chart
#!/usr/bin/env pythonimport plotly.offline as pltoffimport plotly.graph_objs as godef 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
Pie Chart
#!/usr/bin/env pythonimport plotly.offline as pltoffimport plotly.graph_objs as godef 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()