Python 单独显示子图
Python show subplots separately
在我的代码中,在每个迭代步骤中,我都会得到一个包含 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)]
我想为每个工人绘制每个值在每个步骤中的变化情况。所以,我想制作子图(每个工人一个),其中每个子图在 x 轴上都有迭代步骤,在 y 轴上有相应的值。我当前的代码输出这个丑陋的数字:
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()
我想显示我的 n 个工人的所有子图,所以我尝试通过将 fig, ax = plt.subplots(len(steps), len(steps))
更改为 [=14= 来显示每个图 8 个子图],现在我只得到了前 8 个工人的子图,而不是所有的子图。
谁能帮我解决这个问题?例如,如何显示每个图 8 个子图,但显示所有子图,而不仅仅是前 8 个?
我尝试使用 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))
这是我得到的两个数字:
和
在我的代码中,在每个迭代步骤中,我都会得到一个包含 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)]
我想为每个工人绘制每个值在每个步骤中的变化情况。所以,我想制作子图(每个工人一个),其中每个子图在 x 轴上都有迭代步骤,在 y 轴上有相应的值。我当前的代码输出这个丑陋的数字:
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()
我想显示我的 n 个工人的所有子图,所以我尝试通过将 fig, ax = plt.subplots(len(steps), len(steps))
更改为 [=14= 来显示每个图 8 个子图],现在我只得到了前 8 个工人的子图,而不是所有的子图。
谁能帮我解决这个问题?例如,如何显示每个图 8 个子图,但显示所有子图,而不仅仅是前 8 个?
我尝试使用 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))
这是我得到的两个数字:
和