Guided Project: Visualizing The Gender Gap In College Degrees

I’m working on this guided project and don’t understand the following:

for sp in range(0,18,3):
cat_index = int(sp/3)
ax = fig.add_subplot(6,3,sp+1)

My understanding is that in the range() function the “0” is where the loop will start, the 18 is where it will end, and the 3 is the intervals in which the loop with iterate over. I also don’t understand the purpose of the cat_index and third line of code. In subsequent loops for the other two degree categories, I’m unsure of why the code has sp-1 and sp-2 respectively. Thanks!

4 Likes

Hey there Robert! Welcome to the community!

Do remember to mark a solution as “Solved” if it answers your question (this helps other students find the post!)

This is correct. Let’s look at the code you’re trying to wrap your head around:

for sp in range(0,18,3):
    cat_index = int(sp/3)
    ax = fig.add_subplot(6,3,sp+1)

Let’s break this down line by line:

First line

for sp in range(0,18,3):

range(0,18,3) can be thought of as a list containing the following: [0, 3, 6, 9, 12, 15]

You’re essentially iterating over these 6 values in the list. In the first iteration, sp is 0, in the second it’s 3, etc.

We’ll touch on why this matters down below.

Second Line

cat_index = int(sp/3)

So above, we saw how sp takes the values 0, 3, 6, 9, etc. cat_index, after the division by 3, therefore works out to the values 0, 1, 2, 3, etc.

If you look below in the solutions, this cat_index value is used to index the list of major categories, like so:
women_degrees[stem_cats[cat_index]]

Where women_degrees was the csv file you read in initially, and stem_cats is the following list of STEM majors.

stem_cats

stem_cats = ['Psychology', 'Biology', 'Math and Statistics', 'Physical Sciences', 'Computer Science', 'Engineering']

stem_cats[cat_index] therefore indexes the first element in the list when sp = 0, it indexes the second element when sp = 3 (because sp is divided by 3 to calculate cat_index), and so on!

Third Line

ax = fig.add_subplot(6,3,sp+1)

Here, you’re adding a sub-plot to the figure. Again, keep in mind the values that sp takes on during each iteration (0, 3, 6, etc).

During the first iteration, when sp = 0, the code works out to: ax = fig.add_subplot(6,3,1)

In fig.add_subplot(), the first parameter is the number of rows of the figure, the second parameter is the number of columns, and the 3rd parameter (the one that changes depending on sp in our case) is the position of the sub-plot in the figure. In this case, for sp = 0, its position will be “1”. When sp is 3, its position will be “4”, which would place the 2nd subplot just below the first one.

In an earlier mission, it’s explained to you how the sub-plot positions are mapped out on a figure.

In a 3x3 figure containing 9 sub-plots, the sub-plots would be positioned like so:
1, 2, 3
4, 5, 6
7, 8, 9

Notice that sub-plot 4 is right below sub-plot 1, and sub-plot 7 is right below sub-plot 4. This is how the difference of 3 in the sp variable helps you position graphs belonging to the same category of major in the same column in the figure. In the case of the code you linked, we’re plotting all the graphs for the STEM majors in the very first column.

In the solutions, the sp variable is altered like that to plot graphs in different positions.

Again, refer to the above 3x3 figure example. If you didn’t want to plot your sub-plots in positions 1, 4, and 7, and you instead wanted positions 2, 5, and 8, you’d have to use a different strategy to refer to them. You could once again generate a range of sp values with intervals of 3, and then instead of adding 1, you could subtract 1, or subtract 2, to refer to the position you want.

My solution to this project involved a slightly different approach, where I didn’t generate a range of sp values in intervals of 3, but the concept is the same.

6 Likes

Wow! Super helpful. I clearly didn’t understand that the value sp takes in in ax=fig.add_subplot(6,3,sp+1) dictates the position of individual graphs, otherwise I might have figured it out. Your solution is a little more intuitive to me but I suppose the provided solution allows for the same approach universally for all three of the lists.

Thanks for your help!

No problem! Remember to mark a post “Solved” if it helped settle your problem!

Hey BBP85!

Noticed you have a solution to this project, was just wondering if there were multiple solution files to each of the projects just to compare? I find myself taking different routes than most of my guided projects and would love to see others as well.

Hi @patrickalfante! As far as I’m aware, there are only 1 set of official DQ solutions to each guided project. But there’s certainly plenty of student solutions out there on GitHub!

What I’ve done in the past is google search the name of the guided project - that usually returns a handful of other students’ solutions

hello!
I’m stuck with this project - I just can’t figure out how to create a matrix for the plots. In the mission it’s said I need 3 for-loops, so I tried to write a for loop for each percentage group but it still doesn’t work. I don’t want to look up the solution yet (or should I), as I want to have a learning effect. Can you just give me some extra tips / more detailed description on how to create the matrix?

thx!

Hi @apk0108!

The essence of what’s happening is this - each of the 3 for loops populates the a single column of the 6x3 grid layout.

You might structure your first for loop like this:

for sp in range(0,6):
    ax = fig.add_subplot(6,3,(sp)*3+1)
    ax.plot(women_degrees['Year'], women_degrees[stem_cats[sp]], c=cb_dark_blue, label='Women', linewidth=3)
    ax.plot(women_degrees['Year'], 100-women_degrees[stem_cats[sp]], c=cb_orange, label='Men', linewidth=3)

Your second one like this:

for sp in range(0,5):
    ax = fig.add_subplot(6,3,sp*3+2)
    ax.plot(women_degrees['Year'], women_degrees[lib_arts_cats[sp]], c=cb_dark_blue, label='Women', linewidth=3)
    ax.plot(women_degrees['Year'], 100-women_degrees[lib_arts_cats[sp]], c=cb_orange, label='Men', linewidth=3)

And your third like this:

for sp in range(0,6):
    ax = fig.add_subplot(6,3,sp*3+3)
    ax.plot(women_degrees['Year'], women_degrees[other_cats[sp]], c=cb_dark_blue, label='Women', linewidth=3)
    ax.plot(women_degrees['Year'], 100-women_degrees[other_cats[sp]], c=cb_orange, label='Men', linewidth=3)

We see that in each of the for loops, the following line is written with different variations, so specify a different position in the grid!

ax = fig.add_subplot(6,3,(sp)*3+1)

The above line of code populates the first column, since the 3rd argument (represented by sp*3+1), takes on the values of 1, 4, 7, etc.

As mentioned in an earlier post of mine, the sub-plot positions in this grid have the following layout:
1, 2, 3
4, 5, 6
7, 8, 9

To populate the 2nd column, I’d want the 3rd argument to take on the values 2, 5, 8, 11, etc, and for the 3rd column, it would have to be 3, 6, 9, 12, etc.

So this is the first main difference in the 3 different for loops.

The other difference is simply the fact that they reference a different list of majors! In the first for loop we index the stem category of majors (which remember was written in a list!) when we write women_degrees[stem_cats[sp]], in the second for loop, we index the liberal arts category of majors when we write women_degrees[lib_arts_cats[sp]], etc.

2 Likes

Hey BlueBerry!!
With utmost difficulty I understood the logic behind this code… Thanx a ton for ur detailed explanation - it helped a lot.

But!! How do we come up with the logic part?? Be it any of the below methods, how do we come up with such logic at first place?? I mean, how do we start thinking in that direction to begin with?? Coz, no matter how I hard I tried I couldn’t come up with the logic to hack around this problem… :

Logic 1

for sp in range(0,18,3):
    cat_index = int(sp/3)
    ax = fig.add_subplot(6,3,sp+1) 
for sp in range(1,16,3):
    cat_index = int((sp-1)/3)
    ax = fig.add_subplot(6,3,sp+1)
for sp in range(2,20,3):
    cat_index = int((sp-2)/3)
    ax = fig.add_subplot(6,3,sp+1)

Logic 2

for sp in range(0,6):
    ax = fig.add_subplot(6,3,(sp)*3+1)
for sp in range(0,5):
    ax = fig.add_subplot(6,3,sp*3+2)
for sp in range(0,6):
    ax = fig.add_subplot(6,3,sp*3+3)

Any help/suggestions/guidance wrt to how to be able to start thinking for logic in such situations!!

@blueberrypudding85 Cud u pls respond to my above :point_up: post!!

Hi @Datom! Sorry for the late response!

While unspecific, that’s a very worthwhile question to ask! I’m far from a master at this myself, so I can only chime in with what I feel has helped me. This is going to be a generic answer, but seriously - practice!! Your brain will take a while until algorithms start taking root inside that will later form the basis for a “pythonic” frame of mind when it comes to solving these kinds of problems! It also helps to force yourself out of your comfort zone and tackle problems that really force you to think and apply your knowledge in ways you might not have previously.

One thing that’s helpful for everyone is to look at the solutions other people come up with, and draw lessons from those! That’s where I find sites like codewars.com very handy! Another thing you could do is participate in communities (like ours!) and actively involve yourself in discussions like these :slight_smile:

3 Likes

Hi!
Was just working on this project project, and of course got caught on this step.
Is there something I missed earlier on in the course? They didn’t really suggest using loops in the lessons up to this. After going back and looking at solutions for questions I already answered, I saw they used them, but didn’t explain them in the solutions or lessons.
As such this feels a little out of scope for me, LOL.

Thanks!

Thanks for this blueberrypudding, yours was also the approach i was taking, and i also didn’t understand the provided solution (the interval steps of 3 in the range, is new to me).
Small detail, but I do think your solution messes up the labels though, looks like ‘men’/‘women’ is sometimes switched?