Reputation: 976
In my code, at each iteration step I get a list of tuples that contain a worker_id and a value, like this:
[('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. 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. 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.
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?
Upvotes: 0
Views: 624
Reputation: 25093
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)
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
Upvotes: 2