python - 如何绘制列表中的数据?

标签 python matplotlib

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

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

最佳答案

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 已按时间戳排序,则无需排序。)

编辑:

# 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

关于python - 如何绘制列表中的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54276754/

相关文章:

python - X 的浮点精度

Python(使用opencv保存图像的简单应用程序)

python - 导入错误 : No module named six [Windows]

python - 使用 matplotlib 在图像数据之上对线网格进行像素精确定位

Python Pylab 子图错误?

python - %matplotlib 行魔术导致 Python 脚本中的 SyntaxError

python - 获取主板上内存插槽总数的方法

python - 在 Numpy 数组中查找行最大化某些函数

python - 执行其他 celery 任务不起作用的 celery 周期性任务

python - 如何将 seaborn boxplot 须基于百分位数?