如何使用字典制作散点图?

时间:2017-10-25 14:57:55

标签: python dictionary matplotlib plot

我将以下键和值列表作为列表:

comp = {
        0: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], 
        1: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], 
        2: [0.2073837448663338, 0.19919737000568305, 0.24386659105843467, 0.25659375810265855, 0.0, 0.2703400161511446, 0.0, 0.0], 
        3: [0.2752555116304319, 0.19919737000568305, 0.21704752129294347, 0.25659375810265855, 0.0, 0.2703400161511446, 0.0, 0.0], 
        4: [0.2752555116304319, 0.19919737000568305, 0.21782751590851177, 0.25659375810265855, 0.0, 0.2703400161511446, 0.0, 0.0], 
        5: [0.2752555116304319, 0.19919737000568305, 0.21782751590851177, 0.25659375810265855, 0.0, 0.2703400161511446, 0.0, 0.0], 
        6: [0.2752555116304319, 0.19919737000568305, 0.21782751590851177, 0.25659375810265855, 0.0, 0.2691379068024452, 0.0, 0.0], 
        7: [0.2752555116304319, 0.19919737000568305, 0.21782751590851177, 0.25659375810265855, 0.0, 0.2691379068024452, 0.0, 0.0]
       }

每个列表中分别有8个值(例如,每个节点/人1个)。密钥可以称为“时间戳”。并且从时间戳0到7记录8个节点/人的值。

我想要实现散点图,其中x-axis是时间戳,y-axis是值,plot上的点应该是与其对应的节点/人x和y。

该图应在每个时间戳上形成一个8点(节点)的。我有以下代码可以部分工作,但我认为它需要每个列表中所有8个值的平均值,并将这些点绘制为时间戳中的一个:

import pylab
import matplotlib.pyplot as plt

for key in comp:
    #print(key)
    for idx, item in enumerate(comp[key]):

        x = idx
        y = item
        if idx == 0:
            pylab.scatter(x, y, label=key)
        else:
            pylab.scatter(x, y)

pylab.legend()
pylab.show()

不确定如何创建我想要的群集。任何帮助表示赞赏。

(使用Ubuntu 14.04 32位VM和Python 2.7)

3 个答案:

答案 0 :(得分:2)

我认为你有点过于复杂了。如果您遍历并获取字典的键,则可以通过comp[key_name]获取值。然后可以将其传递给plt.scatter()。您必须使用[key] * 8重复密钥8次,以便传递整个值列表以进行分散:

import matplotlib.pyplot as plt

comp = {
        0: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
        1: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
        2: [0.2073837448663338, 0.19919737000568305, 0.24386659105843467, 0.25659375810265855, 0.0, 0.2703400161511446, 0.0, 0.0],
        3: [0.2752555116304319, 0.19919737000568305, 0.21704752129294347, 0.25659375810265855, 0.0, 0.2703400161511446, 0.0, 0.0],
        4: [0.2752555116304319, 0.19919737000568305, 0.21782751590851177, 0.25659375810265855, 0.0, 0.2703400161511446, 0.0, 0.0],
        5: [0.2752555116304319, 0.19919737000568305, 0.21782751590851177, 0.25659375810265855, 0.0, 0.2703400161511446, 0.0, 0.0],
        6: [0.2752555116304319, 0.19919737000568305, 0.21782751590851177, 0.25659375810265855, 0.0, 0.2691379068024452, 0.0, 0.0],
        7: [0.2752555116304319, 0.19919737000568305, 0.21782751590851177, 0.25659375810265855, 0.0, 0.2691379068024452, 0.0, 0.0]
       }

for key in comp:

    plt.scatter([key]*8, comp[key], label=key)

plt.legend()
plt.show()

更新:要获得您想要的颜色,您可以执行以下操作,这是@lkriener给出的答案的修改版本

array = np.zeros((8,8))
for key in comp:
    array[:,key] = comp[key]

x = range(8)
for i in range (8):
    plt.scatter(x, array[i,:], label=i)

plt.legend()
plt.show()

这给出了数字:

enter image description here

您可以通过调用plt.legend()某些参数来移动图例。最重要的是locbbox_to_anchor,其中的文档可以找到here

答案 1 :(得分:1)

要绘制相同颜色的相同节点的值,您可以执行以下操作:

import numpy as np
import matplotlib.pyplot as plt

comp = {
        '0': [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
        '1': [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
        '2': [0.2073837448663338, 0.19919737000568305, .24386659105843467,0.25659375810265855, 0.0, 0.2703400161511446, 0.0, 0.0],
        '3': [0.2752555116304319, 0.19919737000568305, 0.21704752129294347, 0.25659375810265855, 0.0, 0.2703400161511446, 0.0, 0.0],
        '4': [0.2752555116304319, 0.19919737000568305, 0.21782751590851177, 0.25659375810265855, 0.0, 0.2703400161511446, 0.0, 0.0],
        '5': [0.2752555116304319, 0.19919737000568305, 0.21782751590851177, 0.25659375810265855, 0.0, 0.2703400161511446, 0.0, 0.0],
        '6': [0.2752555116304319, 0.19919737000568305, 0.21782751590851177, 0.25659375810265855, 0.0, 0.2691379068024452, 0.0, 0.0],
        '7': [0.2752555116304319, 0.19919737000568305, 0.21782751590851177, 0.25659375810265855, 0.0, 0.2691379068024452, 0.0, 0.0],
        }
array = np.zeros([8,8])
for i, key in enumerate(comp.keys()):
    for j in range(8):
        array[j, i] = comp[key][j]

plt.xlim((-1,8))
plt.ylim((-0.05,0.3))
plt.xlabel('timestamps')
plt.ylabel('values of nodes')
for i in range(8):
    plt.plot(range(8), array[i], ls='--', marker='o', label='node {}'.format(i))
plt.legend(loc='upper_left')
plt.savefig('temp.png')
plt.show()

这会给你以下图片: enter image description here

答案 2 :(得分:1)

如果你能够使用其他一些模块,那么这里有一个小小的选择。标准散点图很有用,但是您的数据具有大量重叠点,这些重叠点在最终图形中不可见。为此,seaborn' swarmplot可能会有用。

为了让生活更轻松,我使用pandas将数据重新整形为数据框,然后直接调用sramplot

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

comp = {
        '0': [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
        '1': [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
        '2': [0.2073837448663338, 0.19919737000568305, 0.24386659105843467, 0.25659375810265855, 0.0, 0.2703400161511446, 0.0, 0.0],
        '3': [0.2752555116304319, 0.19919737000568305, 0.21704752129294347, 0.25659375810265855, 0.0, 0.2703400161511446, 0.0, 0.0],
        '4': [0.2752555116304319, 0.19919737000568305, 0.21782751590851177, 0.25659375810265855, 0.0, 0.2703400161511446, 0.0, 0.0],
        '5': [0.2752555116304319, 0.19919737000568305, 0.21782751590851177, 0.25659375810265855, 0.0, 0.2703400161511446, 0.0, 0.0],
        '6': [0.2752555116304319, 0.19919737000568305, 0.21782751590851177, 0.25659375810265855, 0.0, 0.2691379068024452, 0.0, 0.0],
        '7': [0.2752555116304319, 0.19919737000568305, 0.21782751590851177, 0.25659375810265855, 0.0, 0.2691379068024452, 0.0, 0.0],
        }

df = pd.DataFrame.from_dict(comp, orient='index')
df.index.rename('Observation', inplace=True)

stacked = df.stack().reset_index()
stacked.rename(columns={'level_1': 'Person', 0: 'Value'}, inplace=True)

sns.swarmplot(data=stacked, x='Observation', y='Value', hue='Person')
plt.show()

这给出了以下图:

enter image description here