Python: Creating Multiple Plots In One Figure With For Loop
I have tried to create a 2 row, 3 column grid of plots (each having multiple data plotted on it) using matplotlib. However, no matter what I try, the final saved figure is just on
Solution 1:
here is the way you can use a loop to generate your subplots
and you can hide the axes which you do not need:
import pylab as plt
import numpy as np
fig ,axs=plt.subplots(2,3, sharex='col', sharey='row')
axs[-1,-1].axis('off')
namelist=['a','b','c','d','e']
ax=axs.ravel()
for i,someargs inenumerate(namelist):
x1,y1,x2,y2 = somefunction(someargs)
ax[i].plot(x1,y1)
ax[i].plot(x2,y2)
Solution 2:
After spending some some time closely looking at what I'm running, I've found that the problem might lie somewhere with the function I am using to generate data and how it interacts with the loop. Indeed, using basic test data causes no problem.
That function does a lot of stuff (and requires parallelism), so it's difficult to tell exactly what it's doing.
I'm not really sure what went wrong, but I fixed my problem by storing the data first, and then accessing/plotting it in the manner I posted or similarly to the other answer.
Post a Comment for "Python: Creating Multiple Plots In One Figure With For Loop"