Range() method step param is 3, confused why -- Guided Project: Visualizing The Gender Gap In College Degrees

# Group the degrees into STEM, liberal arts, and other:

stem_cats = ['Psychology', 'Biology', 'Math and Statistics', 'Physical Sciences', 'Computer Science', 'Engineering']
lib_arts_cats = ['Foreign Languages', 'English', 'Communications and Journalism', 'Art and Performance', 'Social Sciences and History']
other_cats = ['Health Professions', 'Public Administration', 'Education', 'Agriculture','Business', 'Architecture']

#Set figure width 18 inches and height 3.
fig = plt.figure(figsize=(16, 20))

## Generate first column of line charts. STEM degrees.
#range(start, stop[, step])
for sp in range(0,18,3):
    cat_index = int(sp/3)

Could it please be explained why the step is 3 here?
for sp in range(0,18,3):

I see that there are 3 lists for the different categories, each with 6 elements and that
the step is a difference between each number in the result, with the default of 1 if not specified, but do not understand why the step should be set to three.

hi @jamesberentsen

I had attached a link to the detailed explanation for this in your own post here

(Improving plot aesthetics - Displaying Multiple Plots per Figure)

Are you more confused after reading through that post :thinking: Let us know will try to improve it and make it better.

attaching direct link here - Completely Lost on Matplotlib

1 Like

@jamesberentsen Hey,

I can’t say exactly, can you provide link to your mission?

Based on this, I would say that it’s just for learning purposes.

See, you are getting cat_index by dividing sp,

sp would be 0,3,6,9,12,15
cat_index would be 0,1,2,3,4,5

It could be for just a demonstration of a range function.

1 Like

Hi Jenil,

Sure here is link:
https://app.dataquest.io/m/149/guided-project%3A-visualizing-the-gender-gap-in-college-degrees/2/comparing-across-all-degrees

Screenshot from solution code


Here is the solution code:
https://github.com/dataquestio/solutions/blob/master/Mission149Solutions.ipynb

Task:

Generate a 6 row by 3 column grid of subplots.
In the first column:
Generate line charts for both male and female percentages for every degree in stem_cats.
Add text annotations for Women and Men in the topmost and bottommost plots.
In the second column:
Generate line charts for both male and female percentages for every degree in lib_arts_cats.
Add text annotations for Women and Men for only the topmost plot (since the lines overlap at the end in the bottommost plot).
In the third column:
Generate line charts for both male and female percentages for every degree in other_cats.
Add text annotations for Women and Men in the topmost and bottommost plots.

@jamesberentsen

Okay, so as per the solution book you can see that all stem_cats plots are in one column. Likewise all lib_arts_cats in the second column and all other_cats in the third column.

To achieve this we need to add subplot at a specific position that’s why we are using 3 in range.

First for loop generates,

ax = fig.add_subplot(6,3,1)
ax = fig.add_subplot(6,3,4)
ax = fig.add_subplot(6,3,7)
ax = fig.add_subplot(6,3,10)
ax = fig.add_subplot(6,3,13)
ax = fig.add_subplot(6,3,16)

which will give us first column plots for all stem cats.

I hope it clarifies your question!!

Hi Rucha,

Thanks for link, while it cleared many things, I am still a little confused.

cat_index will give you major element present at that index

As I understand it (6,3,1) (6,3,4). the 1 and 4 would be ‘Biology’ and ‘Computer Science’ appearing in row1 & 2 of column one since that is their index number in list and that is the output , but where do the other elements come from, like the 0 index element for Psychology… 1st index fig.add_subplot(6,3,sp+1) it is set to 1, but explanation was like this:

So the SP + 1 would yield this result:

SP = 0 >> SP + 1 = 0 + 1 = 1
SP = 3 >> SP + 1 = 3 + 1 = 4
.......
SP = 15 >> SP + 1 = 15 + 1 = 16

which would give us a correct subplot number to plot the Majors.

so it seems like it is skipping pysch?

@jamesberentsen

See, for indexing, we are using cat_index variable.

which is obtained by dividing sp.
cat_index = int(sp/3)

and subplot like this,
add_subplot(6,3,sp+1)

So for loop will execute like this,

sp = 0, cat_index = 0  1 pass
subplot(6,3,1)

sp = 3, cat_index = 1   2 pass
subplot(6,3,4)

sp = 6, cat_index = 2   3 pass
subplot(6,3,7)

sp = 9, cat_index = 3   4 pass
subplot(6,3,10)

sp = 12, cat_index = 4   5 pass
subplot(6,3,13)

sp = 15, cat_index = 5   6 pass
subplot(6,3,16)

sp+1 is for subplot.
and for index sp/3

Hi Jenil,

In the stem list…If 0,1,2,3 -pysch, bio, mat n stat, phyical sci … are stem then it seems like their index number is being skipped over since step is 3 it goes 0+1,1+3,

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

so it seems like it skipping some of the stem elements.

Sorry
i can’t quite get it still.

@jamesberentsen

It’s for subplot…

Now for indexing,

we are using cat_index variable which we are obtaining by sp/3

so

sp 0,3,6,9,12,15
cat_index 0,1,2,3,4,5

so we are accessing the element of stem_cats with cat_index variable.

stem_cats[cat_index]

1 Like

[/quote]

sp 0,3,6,9,12,15
cat_index 0,1,2,3,4,5

so we are accessing the element of stem_cats with cat_index variable

Wow, thanks . I got it now…I can see how this works now.
Thanks for your kind explanation.

ax.plot(women_degrees['Year'], women_degrees[stem_cats[cat_index]], c=cb_dark_blue, label='Women', linewidth=3)

1 Like