2

Я пытаюсь нарисовать график в plotly из CSV файла.

файл содержит строки типа:

frame.number,frame.time_relative,frame.interface_name
"1","0.000000000","enp0s31f6"
"2","0.286968124","enp0s31f6"
"3","0.347273802","enp0s31f6"
"4","0.376051415","enp0s31f6"
"5","1.895804954","enp0s31f6"
"6","2.039418070","enp0s31f6"
"7","2.061084575","enp0s31f6"
"8","3.026748561","enp0s31f6"
"9","3.247024689","enp0s31f6"
"10","3.896585252","eth0"
"11","4.001361629","enp0s31f6"
"12","4.216054934","eth0"
"13","4.269130040","enp0s31f6"
"14","4.269426650","eth0"
"15","4.463316773","enp0s31f6"

Я сделал всё вот так:

import pandas as pd
import numpy as np
import scipy as sp
from datetime import datetime
import plotly.offline
import plotly.figure_factory as FF
import plotly.plotly as py
import plotly.graph_objs as go


df = pd.read_csv('test.csv')
df.head()

trace1 = go.Scatter(
                    x=df['frame.number'], y=df['frame.time_relative'],
                    mode='markers+text', name='enpos31f6',
)
trace2 = go.Scatter(x=df['frame.number'], y=df['frame.time_relative'], 
                    mode='markers+text', name='wlans',
)

layout = go.Layout(title='Simple Plot from csv data',
                   plot_bgcolor='rgb(230, 230,230)')

fig = go.Figure(data=[trace1, trace2], layout=layout)
plotly.offline.plot(fig)

На выходе получил: вывод

Как дифференцировать маркеры, чтобы всё что принадлежит enp0s31f6 было синими маркерами, а eth0 оранжевыми?. Сейчас они друг на друге.

2 ответа 2

4

Решение с использованием plot.ly:

import plotly
import plotly.plotly as py
import plotly.graph_objs as go


plotly.tools.set_credentials_file(username='DemoAccount', api_key='lr1c37zw81')

enpo = df.loc[df['frame.interface_name'].isin(['enp0s31f6'])]
eth0 = df.loc[df['frame.interface_name'].isin(['eth0'])]

trace1 = go.Scatter(
                    x=enpo['frame.number'], y=enpo['frame.time_relative'],
                    mode='markers+text', name='enpos31f6',
                    marker = dict(
                    size = 20,
                    color = 'blue',
                    line = dict(
                        width = 2,
                        color = 'rgb(0, 0, 0)'
                    )
    )
)
trace2 = go.Scatter(
                    x=eth0['frame.number'], y=eth0['frame.time_relative'],
                    mode='markers+text', name='eth0',
                    marker = dict(
                    size = 20,
                    color = 'orange',
                    line = dict(
                        width = 2,
                        color = 'rgb(0, 0, 0)'
                    )
    )
)

data = [trace1, trace2]

layout = dict(title = 'My Scatter',
              yaxis = dict(zeroline = False),
              xaxis = dict(zeroline = False)
             )
fig = dict(data=data, layout=layout)
py.iplot(fig, filename='styled-scatter')

введите сюда описание изображения

3

Пример решения без использования plotly - рисуем обычными средствами Pandas:

df.loc[df['frame.interface_name'].isin(['enp0s31f6']), 'color'] = 'b'
df.loc[df['frame.interface_name'].isin(['eth0']), 'color'] = 'orange'

график:

df.plot.scatter(x='frame.number', y='frame.time_relative', c=df['color'])

введите сюда описание изображения

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.