[python工具] 如何使用plotly制作散列图

注意

  • 最好使用python3, 我在mac上,默认2.7,安装起来相当费劲,使用python3 + pip3 + virtualenv 比较合适

安装

pip install plotly

测试

➜  plotly_test git:(master) ✗ pwd
/Users/muahao/work/vpc/muahao_tools/plotly_test
➜  plotly_test git:(master) ✗ cat first.py
from plotly.graph_objs import *
import plotly.offline
trace0 = Scatter(
    x=[1,2,3,4,],
    y=[10,15,13,17]
)
trace1 = Scatter(
    x= [1,2,3,4],
    y= [16,5,11,9]
)
plotly.offline.plot([trace0,trace1])

测试2:

➜  plotly_test git:(master) ✗ cat ping2_lentency200.log | head
1582613630 3.03 ms
1582613631 1.87 ms
1582613633 0.436 ms
1582613634 1.85 ms
1582613635 0.412 ms
1582613636 1.69 ms
1582613637 1.98 ms
1582613638 1.03 ms
1582613639 3.62 ms
1582613640 8.56 ms

import sys
import subprocess
from plotly.graph_objs import *
import plotly.offline

# FILE = "ping2_lentency100.log.2001"
# TITLE = "OpenVPN txqueuelen=100"


FILE = "ping2_lentency200.log"
TITLE = "OpenVPN txqueuelen=200"


def get_x_y_data():
    x_list = list()
    y_list = list()
    cmd = "cat {}".format(FILE)
    code, out = subprocess.getstatusoutput(cmd)
    for line in out.split('\n'):
        if not line:
            continue
        line_split = line.split(' ')
        if len(line_split) < 2:
            continue
        x = line.split(' ')[0]
        y = line.split(' ')[1]
        x_list.append(x)
        y_list.append(y)
    return x_list, y_list


if __name__ == "__main__":
    x_list, y_list = get_x_y_data()

    trace0 = Scatter(
        x=x_list,
        y=y_list,
        mode='markers'
    )
    data_comp = [trace0]
    # plotly.offline.plot([trace0])

    layout_comp = Layout(
        title=TITLE,
        hovermode='closest',
        xaxis=dict(
            title='timestamp',
            ticklen=5,
            zeroline=False,
            gridwidth=2,
        ),
        yaxis=dict(
            title='Latency (ms)',
            ticklen=5,
            gridwidth=2,
        ),
    )
    fig_comp = Figure(data=data_comp, layout=layout_comp)
    plotly.offline.plot(fig_comp, filename=TITLE)

posted @ 2020-02-25 15:49  苏小北1024  阅读(326)  评论(0编辑  收藏  举报