Probabilistic-Programming-and-Bayesian-Methods-for-Hackers icon indicating copy to clipboard operation
Probabilistic-Programming-and-Bayesian-Methods-for-Hackers copied to clipboard

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

Open tehreemnaqvi opened this issue 4 years ago • 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)

tehreemnaqvi avatar Jun 22 '20 11:06 tehreemnaqvi

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.

hvgazula avatar Jun 22 '20 11:06 hvgazula

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

tehreemnaqvi avatar Jun 22 '20 12:06 tehreemnaqvi

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)

hvgazula avatar Jun 22 '20 12:06 hvgazula

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

tehreemnaqvi avatar Jun 22 '20 12:06 tehreemnaqvi

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

hvgazula avatar Jun 22 '20 12:06 hvgazula

Thank you. It's working now.

tehreemnaqvi avatar Jun 22 '20 12:06 tehreemnaqvi

Or another way to do it is

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

hvgazula avatar Jun 22 '20 12:06 hvgazula

Yeah, it's also working.

Thanks for your help.

tehreemnaqvi avatar Jun 22 '20 12:06 tehreemnaqvi