CamDavidsonPilon/Probabilistic-Programming-and-Bayesian-Methods-for-Hackers

ValueError: num must be 1 <= num <= 20, not 21

tehreemnaqvi opened this issue · 8 comments

Hi,
What's wrong with this code?
I have an error like this??


for i, image in enumerate(images):
plt.subplot(4,5, i + 1)
plt.imshow(image)

You can only have 20 subplots (4 times 5) indexed from 1 through 20. By saying i+1, you are asking for subplot 21 which cannot be done.

you mean I have to change like that?
for i, image in enumerate(images):
plt.subplot(4,5, i)
plt.imshow(image)

Assuming you only have 20 images, the following should work. However, if you have more than 20 images you may want to adjust the shape of your grid.

for i, image in enumerate(images, 1):
plt.subplot(4, 5, i)
plt.imshow(image)

I tried this one but again got this error:
ValueError: num must be 1 <= num <= 20, not 21

for i, image in enumerate(images, 1):
    try:
        plt.subplot(4, 5, i)
        plt.imshow(image)
    except ValueError:
        break

Thank you. It's working now.

Or another way to do it is

for i, image in enumerate(images[:20], 1):
    plt.subplot(4, 5, i)
    plt.imshow(image)

Yeah, it's also working.

Thanks for your help.