简体   繁体   English

Python 分别显示子图

[英]Python show subplots separately

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 subplots (one for each worker), where each one has at x-axis the iteration step and at y-axis their corresponding value.所以,我想制作子图(每个工人一个),其中每个子图在 x 轴上都有迭代步骤,在 y 轴上有对应的值。 My current code outputs this ugly figure:我当前的代码输出这个丑陋的数字:

fig, ax = plt.subplots(len(steps), len(steps))

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])
    for k in steps:
        ax_sub.set_title(k[j][0])
plt.show()

在此处输入图像描述

I want to show all of the subplots for my n workers, so I tried to show 8 subplots per figure, by changing fig, ax = plt.subplots(len(steps), len(steps)) to fig, ax = plt.subplots(4, 4) , and now I get only the 8 first workers subplots, instead of all of them.我想显示我的n 个工人的所有子图,所以我尝试通过将fig, ax = plt.subplots(len(steps), len(steps))更改为fig, ax = plt.subplots(4, 4) ,现在我只得到了 8 个第一个工人子图,而不是全部。

在此处输入图像描述

Can anyone help me on this?谁可以帮我这个事? How to show for example 8 subplots per figure, but show all of the subplots, not only the first 8?例如,如何显示每个图 8 个子图,但显示所有子图,而不仅仅是前 8 个?

I tried to use the scarce data the OP provided (very few point, syntactically erroneous, not the real data structure they used in the code snippet) to elaborate a more complete answer (my previous one is still available in the edit history)我尝试使用 OP 提供的稀缺数据(很少点,语法错误,而不是他们在代码片段中使用的真实数据结构)来阐述更完整的答案(我之前的答案在编辑历史中仍然可用)

import matplotlib.pyplot as plt

# wrt the previous version I have reduced the number of row and cols
nr =1 ; nc = 3 ; nsubplots = nr*nc

# the data in the OP, edited to get a syntactically correct object.    
data = [
    [('worker-159', 1.1685709120273498), ('worker-156', 0.7916160785059027), ('worker-150', 1.1486401201147178), ('worker-153', 0.6132945731919339)], 
    [('worker-159', 1.1950497228704960), ('worker-156', 1.0330889397508607), ('worker-150', 1.1598074339882078), ('worker-153', 1.0162635831405047)], 
    [('worker-159', 1.2002260342341922), ('worker-156', 1.0442120194115220), ('worker-150', 1.1610147533213582), ('worker-153', 1.0155351093960254)], 
    [('worker-159', 1.2010865644481130), ('worker-156', 1.0452712882782897), ('worker-150', 1.1611455202975516), ('worker-153', 1.0102820381745612)], 
    [('worker-159', 1.2014539763295100), ('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)],] 

# Put the raw data in a more useful data structure, for me a dict
workers = {}
for step in data:
    for worker, value in step:
        workers.setdefault(worker, []).append(float(value))

# the loop is on an enumerated sequence of workers and values
for iw, (worker, values) in enumerate(workers.items()):

    if iw % nsubplots == 0: 
        # we are at the start OR we have completed a figure
        if iw > 0:
            # if iw>0 we have a completed figure to ship out          
            nfig = iw//nsubplots
            plt.tight_layout()
            fig.savefig('NewFigure%d.png'%nfig)
        # in any case we instantiate a new figure and
        # an iter object to keep track of the subplot position
        fig = plt.figure(figsize=(6,2))
        pos = iter((i, j) for i in range(nr) for j in range(nc))

    # for each worker we place an axes on the correct position
    # and do something with it (you know best what to do with it)
    ax = plt.subplot2grid((nr, nc), next(pos))
    ax.text(0.5, 0.5, worker, va="center", ha="center", transform=ax.transAxes)
    ax.plot(values)

# at the end of the for loop we have a pending figure to ship out
plt.tight_layout()
fig.savefig('NewFigure%d.png'%(nfig+1))

and here the two figures I have got:这是我得到的两个数字:

在此处输入图像描述

and

在此处输入图像描述

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

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