简体   繁体   English

Python 元组列表如何 plot 具有相同键的所有元组

[英]Python list of tuples how to plot all the tuples with the same key

In my code, at each iteration step I get a list of tuples that contain a worker_id and a value, like this:在我的代码中,在每个迭代步骤中,我都会得到一个包含 worker_id 和值的元组列表,如下所示:

[('worker-159', 1.1685709120273498), ('worker-156', 0.7916160785059027), ('worker-150', 1.1486401201147178), ('worker-153', 0.6132945731919339)]
[('worker-159', 1.195049722870496), ('worker-156', 1.0330889397508607), ('worker-150', 1.1598074339882078), ('worker-153', 1.0162635831405047)]
[('worker-159', 1.2002260342341922), ('worker-156', 1.044212019411522), ('worker-150', 1.1610147533213582), ('worker-153', 1.0155351093960254)]
[('worker-159', 1.201086564448113), ('worker-156', 1.0452712882782897), ('worker-150', 1.1611455202975516), ('worker-153', 1.0102820381745612)]
[('worker-159', 1.20145397632951), ('worker-156', 1.0455816259596025), ('worker-150', 1.1611884914303927), ('worker-153', 1.0068296997277124)
[('worker-159', 1.2024538250404766), ('worker-156', 1.0461755869603413), ('worker-150', 1.1612801087850406), ('worker-153', 0.9958443656576963)]

I want to plot how each value changes in each step for every worker.我想 plot 每个工人的每个步骤中的每个值如何变化。 So, I want to make 4 plots (one for each worker), where each one has at x-axis the iteration step and at y-axis their corresponding value.所以,我想制作 4 个图(每个工人一个),其中每个图在 x 轴上都有迭代步骤,在 y 轴上有对应的值。

Does anyone have a clue how to do this?有谁知道如何做到这一点? I tried to use the method zip, but it only plots the different tuples within the list.我尝试使用 zip 方法,但它只绘制列表中的不同元组。 ie: IE:

        x, y= zip(*res)
        plt.plot(x, y)
        plt.show()

Gives me one plot for every step with all the worker_ids in x-axis and all their values in y-axis.每一步都给我一个 plot,所有 worker_id 在 x 轴上,所有值在 y 轴上。

I want one plot for every worker_id, the iteration step in the x-axis and his value in y-axis.我想要一个 plot 用于每个worker_id,x轴上的迭代步骤和y轴上的值。

I guess it's a matter of how to group the first tuples and then the 2nd tuples etc, based on their key (which is worker_id).我想这是如何根据它们的键(即worker_id)对第一个元组和第二个元组等进行分组的问题。 But i don't know how to do that.但我不知道该怎么做。

You can try this way:你可以这样试试:

steps = [[('worker-159', 1.1685709120273498), ('worker-156', 0.7916160785059027), ('worker-150', 1.1486401201147178), ('worker-153', 0.6132945731919339)],
[('worker-159', 1.195049722870496), ('worker-156', 1.0330889397508607), ('worker-150', 1.1598074339882078), ('worker-153', 1.0162635831405047)],
[('worker-159', 1.2002260342341922), ('worker-156', 1.044212019411522), ('worker-150', 1.1610147533213582), ('worker-153', 1.0155351093960254)],
[('worker-159', 1.201086564448113), ('worker-156', 1.0452712882782897), ('worker-150', 1.1611455202975516), ('worker-153', 1.0102820381745612)],
[('worker-159', 1.20145397632951), ('worker-156', 1.0455816259596025), ('worker-150', 1.1611884914303927), ('worker-153', 1.0068296997277124)],
[('worker-159', 1.2024538250404766), ('worker-156', 1.0461755869603413), ('worker-150', 1.1612801087850406), ('worker-153', 0.9958443656576963)]]

import matplotlib.pyplot as plt
fig, ax = plt.subplots(2,2)
ax = ax.ravel() #all subaxes in single numpy array, to easily iterate over
ls = len(steps)
for j, ax_sub in enumerate(ax):
    ax_sub.plot(range(ls), [k[j][1] for k in steps])

plt.show()

This is just one way.这只是一种方式。 You can also plot them separately in their own figures.你也可以把它们分别放在自己的图中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM