Как построить данные из списка? - PullRequest
0 голосов
/ 20 января 2019

Я фильтрую список данных.Мне нужно отобразить мои результаты для исходного узла.Это образец моих данных:

 ('timestamp', 'node_source', 'node_destination', 'node_source_counter_acces_to_specific_function')

В plt.xlabel я хочу поставить метку времени.В плт.йлабел хочу поставить счетчик.В легенде я хочу поставить маркер на основе адреса назначения.Каждая строка моего набора данных очень важна.Я использую эту функцию:

import matplotlib.pyplot as plt

list_info= [('1547977394', '02141592cc0000000100000000000000', '02141592cc0000000500000000000000', '1'),
('1547977395', '02141592cc0000000100000000000000', '02141592cc0000000300000000000000', '2'), 
('1547977396', '02141592cc0000000100000000000000', '02141592cc0000000500000000000000', '3'), 
('1547977397', '02141592cc0000000100000000000000', '02141592cc0000000700000000000000', '4'), 
('1547977398', '02141592cc0000000100000000000000', '02141592cc0000000300000000000000', '5'), 
('1547977399', '02141592cc0000000100000000000000', '02141592cc0000000500000000000000', '6'), 
('1547977400', '02141592cc0000000500000000000000', '02141592cc0000000500000000000000', '7'),
('1547977401', '02141592cc0000000500000000000000', '02141592cc0000000500000000000000', '8'),
('1547977402', '02141592cc0000000200000000000000', '02141592cc0000000500000000000000', '9'),
('1547977403', '02141592cc0000000100000000000000', '02141592cc0000000500000000000000', '10'),
('1547977404', '02141592cc0000000200000000000000', '02141592cc0000000300000000000000', '11'),
('1547977405', '02141592cc0000000400000000000000', '02141592cc0000000500000000000000', '12'),
('1547977406', '02141592cc0000000300000000000000', '02141592cc0000000500000000000000', '13'),
('1547977407', '02141592cc0000000400000000000000', '02141592cc0000000500000000000000', '14'),
('1547977408', '02141592cc0000000400000000000000', '02141592cc0000000500000000000000', '15'),
('1547977409', '02141592cc0000000300000000000000', '02141592cc0000000500000000000000', '16'),
('1547977410', '02141592cc0000000400000000000000', '02141592cc0000000300000000000000', '18'),
('1547977411', '02141592cc0000000200000000000000', '02141592cc0000000500000000000000', '19')]

Например, ожидаемые данные, которые должны быть нанесены на график и визуализированы до node_source:02141592cc0000000100000000000000:

('02141592cc0000000100000000000000',timestam':[1547977395,1547977396,1547977397,1547977398,1547977399,1547977403],'Counter':[2,3,4,5,6,10],'dest':[02141592cc0000000300000000000000,02141592cc0000000500000000000000,02141592cc0000000700000000000000,02141592cc0000000300000000000000,02141592cc0000000500000000000000,02141592cc0000000500000000000000])

1 Ответ

0 голосов
/ 20 января 2019
plots = dict()

# create dictionary like this: plots[source]={'timestam': list of integer timestamps, 'counter': list of integer counter, 'dest': list of destinations}
for i, el in enumerate(list_info):
    if el[1] in plots:
        plots[el[1]]['timestam'].append(int(el[0]))
        plots[el[1]]['counter'].append(int(el[3]))
        plots[el[1]]['dest'].append(el[2])
    else:
        plots[el[1]]={'timestam': [int(el[0])], 'counter': [int(el[3])], 'dest': [el[2]]}

# iterate over elements in 'plots', sort by timestamp, plot
for i in plots:
    y = [x for _,x in sorted(zip(plots[i]['timestam'],plots[i]['counter']))]
    labels = [x for _,x in sorted(zip(plots[i]['timestam'],plots[i]['dest']))]
    x = sorted(plots[i]['timestam'])
    plt.plot(x, y, label=', '.join(labels), marker='o') # label=... creates the legend entry
plt.xlabel('timestamp')
plt.ylabel('counter')
plt.title('some data')
plt.legend() # add legend
plt.show()

Результат:

result

(Сортировка не требуется, если ваш list_info уже отсортирован по меткам времени.)

Edit:

# iterate over elements in 'plots', sort by timestamp, plot
f, ax = plt.subplots(len(plots), 1, sharex=True)
for i, item in enumerate(plots):
    y = [x for _,x in sorted(zip(plots[item]['timestam'],plots[item]['counter']))]
    labels = [x for _,x in sorted(zip(plots[item]['timestam'],plots[item]['dest']))]
    x = sorted(plots[item]['timestam'])
    ax[i].plot(x, y, c='k') # label=... creates the legend entry
    for j, _ in enumerate(x):
        ax[i].scatter(x[j], y[j], label=labels[j])
    ax[i].legend() # add legend
    ax[i].set_ylabel('counter')
plt.xlabel('timestamp')
plt.show()

Результат:

result

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...