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

aka "Bayesian Methods for Hackers": An introduction to Bayesian methods + probabilistic programming with a computation/understanding-first, mathematics-second point of view. All in pure Python ;)
http://camdavidsonpilon.github.io/Probabilistic-Programming-and-Bayesian-Methods-for-Hackers/
MIT License
26.51k stars 7.84k forks source link

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

Open tehreemnaqvi opened 4 years ago

tehreemnaqvi commented 4 years ago

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)

hvgazula commented 4 years ago

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.

tehreemnaqvi commented 4 years ago

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

hvgazula commented 4 years ago

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)
tehreemnaqvi commented 4 years ago

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

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

Thank you. It's working now.

hvgazula commented 4 years ago

Or another way to do it is

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

Yeah, it's also working.

Thanks for your help.