Is Chapter three of Mission 146 Solutions Correct?

Hi I hope someone can help.

In chapter three of Mission 146 I was able to complete the Histogram task but did so in a very ‘clunky’ way. I looked at the solution guide to find a better way but it seems that either I or the solution guide is doing something incorrect.

The below is from the solution guide
cols = [“Sample_size”, “Median”, “Employed”, “Full_time”, “ShareWomen”, “Unemployment_rate”, “Men”, “Women”]

fig = plt.figure(figsize=(5,12))
for r in range(1,5):
ax = fig.add_subplot(4,1,r)
ax = recent_grads[cols[r]].plot(kind=‘hist’, rot=40)

fig = plt.figure(figsize=(5,12))
for r in range(4,8):
ax = fig.add_subplot(4,1,r-3)
ax = recent_grads[cols[r]].plot(kind=‘hist’, rot=40)

I duplicated this because it was so much cleaner than mine but wanted to make it a little better and add titles etc. But when I did this it appeared that the first code was displaying graphs [“Median”, “Employed”, “Full_time”, “ShareWomen”,] and the second was displaying [“ShareWomen”, “Unemployment_rate”, “Men”, "Women]. Basically it looks like the ‘SharedWomen’ graph is being displayed twice and column ‘Sample_size’ is not being displayed. If I attempt to set the range(1,5) to range(0,5) I get an error. If I add a filler into the first position of the cols list such as ‘xxxxx’ and rerun the code I appear to get the correct graphs.

col = [‘xxxxx’,‘Sample_size’,‘Median’,‘Employed’,‘Full_time’,‘ShareWomen’,‘Unemployment_rate’,‘Men’,‘Women’]

fig = plt.figure(figsize =(5,12))

for r in range(1,5):
ax = fig.add_subplot(4,1,r)
ax = recent_grads[col[r]].plot(kind=‘hist’,rot=30).set_title(col[r])

My questions are:

A - Am I correct that the solution guide is missing one graph and displaying the other twice? Or am I going crazy.

B - Why won’t the range() accept ‘0’ as a starting point? Basically how do I reference the first item in the cols list without using the filler workaround that I have in there?

1 Like

Hi Lawrence, welcome to the community!

I think you’re right that there is an error here in the solution guide. We’re using r for a couple different things. We can’t use range(0,5) because the fig.add_subplot(4, 1, r) can’t accept 0 as a position in the subplot grid. I think this is the reason why the range started at 1. The problem though is that means we won’t graph the histogram for Sample_size because that’s col[0].

I think the thing to do here is to change the first set of histograms to this:

col = ["Sample_size", "Median", "Employed", "Full_time", "ShareWomen", "Unemployment_rate", "Men", "Women"]

fig = plt.figure(figsize=(5,12))
for r in range(0,4):         #allows us to index the list starting at 0
    ax = fig.add_subplot(4,1,r+1)      #r+1 makes sure we start at plot position 1
    ax = recent_grads[col[r]].plot(kind='hist', rot=40).set_title(col[r])

I’ll tag @Sahil or @Bruno to see if this can be listed as a bug. Thanks for bringing it up.

2 Likes

Hi @Learner1,

You are correct! (Not crazy), I will get it logged. :slightly_smiling_face:

Best,
Sahil

Hello, how do we determine the upper limit for the range?

Hi vroomvroom,

I am new so anyone feel free to correct me but in this instance the Upper limit for range is the second argument. I think of it like this.

range(first argument, second argument, third (Optional) argument)

where the:
first argument is the Lower Limit
Second argument is the Upper Limit
Third argument is the STEP (which in this instance is the default value of 1 and therefor is not defined)

Does this help at all?

Kind regards

Hi Learner1, thanks for replying. I was trying to find how we could know the value for the upper limit of the range. For anyone interested, it is here: