【问题标题】:How to plot data from a list?如何绘制列表中的数据?
【发布时间】:2019-01-22 06:13:27
【问题描述】:

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

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

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:
    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()
    

    结果:

    【讨论】:

    • 非常感谢您的回答。但是我需要在我代表源地址的每个图中都有子图,在我放置目的地的图例中。您精确地呈现数据图例的方式使图形的分析非常复杂。
    • @dina 查看编辑。然而,这并不是 SO 应该如何工作的。你至少应该发布你试图展示你的努力的东西..
    • 我将我的数据和结果放入一个新问题stackoverflow.com/questions/54298370/…
    • 如果您能帮助我,我将不胜感激。
    • @dina 您是否看到我在答案中添加了一些内容?这是你想要的吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-25
    • 1970-01-01
    • 2018-11-06
    • 1970-01-01
    • 1970-01-01
    • 2018-08-29
    相关资源
    最近更新 更多