jmbejara / comp-econ-sp19

Main Course Repository for Computational Methods in Economics (Econ 21410, Spring 2019)
48 stars 26 forks source link

Hw 1 Q5 plotting several images using for loop #6

Closed cangubur closed 5 years ago

cangubur commented 5 years ago

I am trying to plot the requested images using a for loop and I can print everything except the last one. My code is:

values = [500,300,100,50,10] for i in values: plt.imshow(compress(face,i), cmap=plt.cm.gray) plt.figure(i) plt.plot()

why can't I print the last image?

cangubur commented 5 years ago

If anyone has the same issue this works:

values = [500,300,100,50,10] for v in values: plt.imshow(compress(face,v), cmap=plt.cm.gray) plt.figure(v)

jmbejara commented 5 years ago

Great post. In your first example, maybe rearranging like this would work:

values = [500,300,100,50,10]
for i in values:
    plt.figure(i)
    plt.imshow(compress(face,i), cmap=plt.cm.gray)

plt.figure() creates a new figure. Here's a generic example:

import numpy as np
from matplotlib import pyplot as plt

for n in range(5):
    x = np.linspace(0, (n+1) * np.pi,100)
    y = np.sin(x)
    plt.figure()
    plt.title(str(n+1))
    plt.plot(x,y)

returns:

image