首页 > 解决方案 > 如何绘制列表中的数据?

问题描述

我正在过滤数据列表。我需要绘制每个源节点的结果。这是我的数据模式:

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

在 plt.xlabel 我想放置时间戳。在 plt.ylabel 我想放柜台。在图例中,我想根据目的地地址放置标记。每一行都是我的数据集非常重要。我正在使用这个功能:

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])

标签: pythonmatplotlib

解决方案


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()

结果:

结果

list_info(如果您已经按时间戳排序,则无需排序。)

编辑:

# 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()

结果:

结果


推荐阅读