# Code Optimization: Guided Project: Visualizing The Gender Gap In College Degrees

My Code:

``````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']

alist = [stem_cats, lib_arts_cats, other_cats]
for cats in alist:
fig = plt.figure(figsize=(16, 20))
count = 0
for sp in range(count, 18, 3):
cat_index = int((sp - count)/3)
if len(cats) - 1 < cat_index:
plt.show()
count += 1
continue
else:
ax.plot(women_degrees['Year'], women_degrees[cats[cat_index]], c=cb_dark_blue, label='Women', linewidth=3)
ax.plot(women_degrees['Year'], 100-women_degrees[cats[cat_index]], c=cb_orange, label='Men', linewidth=3)
for key, spine in ax.spines.items():
spine.set_visible(False)
ax.set_xlim(1968, 2011)
ax.set_ylim(0, 100)
ax.set_title(cats[cat_index])
ax.tick_params(bottom='off', top='off', left='off', right='off')

if cat_index == 0:
ax.text(2003, 85, 'Women')
ax.text(2005, 10, 'Men')
elif cat_index == 5:
ax.text(2005, 87, 'Men')
ax.text(2003, 7, 'Women')

``````

What I expected to happen:
So I was trying to plot the entire graphs in one go. I have been able to plot all in a column.

2 Likes

For me I looped range(6), then added the plots in different columns, my figure has `1` row, and `6` columns

``````cb_dark_blue = (0/255,107/255,164/255)
cb_orange = (255/255, 128/255, 14/255)
stem_cats = ['Engineering', 'Computer Science', 'Psychology', 'Biology', 'Physical Sciences', 'Math and Statistics']

fig = plt.figure(figsize=(18, 3))

for sp in range(0,6):
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)
ax.spines["right"].set_visible(False)
ax.spines["left"].set_visible(False)
ax.spines["top"].set_visible(False)
ax.spines["bottom"].set_visible(False)
ax.set_xlim(1968, 2011)
ax.set_ylim(0,100)
ax.set_title(stem_cats[sp])
ax.tick_params(bottom="off", top="off", left="off", right="off")

if sp == 0:
ax.text(2005, 87, 'Men')
ax.text(2002, 8, 'Women')
elif sp == 5:
ax.text(2005, 62, 'Men')
ax.text(2001, 35, 'Women')
plt.show()
``````

Thank you.

But I want `stems_cats, lib_arts_cats, and other_cats` plots with this code at one go.

This is only for `stem_cats`. I want to optimize the code. This is why I have included `alist`

You could use `plt.subplots(nrows, ncols, index)`.

With this everything should be plotted as desired.

I have subplots but there are all in one column.

I want the code to switch columns immediately it hits `continue`

`1` row `3` columns?

@monorienaghogho

Try initializing `count = 0` outside the for loop .

It gets reset on every run, so this can be the issue.

Five or 6 rows depending on the cats and three columns. Each column representing one cat: stem, other, liberal.

This totally changes the equation. The graph plots like this. For stem the subplot numbers are: 1, 4, 7, 10 etc if you have 3 columns.

``````    for sp in range(count, 18, 3):
cat_index = int((sp - count)/3)
if len(cats) - 1 < cat_index:
plt.show()
count += 1
continue``````

In that case you’ll have to loop every column at a time, as illustrated in the instruction.

Okay then. I may have to stick to the instructions for now.

Try moving `count = 0` before the first for loop and changing the if clause condition to `==`.

I think this should move the graphs properly and enable count to reach 0, 1, 2.

Will try this. Thanks!

1 Like

@vorunplz solved the problem with this code:

``````import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

cb_dark_blue = (0/255,107/255,164/255)
cb_orange = (255/255, 128/255, 14/255)

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']

cats = [stem_cats,lib_arts_cats,other_cats]

fig = plt.figure(figsize=(15, 20))

def diagramVertic(fig,rows,cols,plotsCol,x,women,deg):
len_deg = len(deg)
plot_position = np.arange(start=plotsCol,stop=((rows*cols)-(3-plotsCol))+1,step=cols)
for sp in range(0,len_deg):
ax_obj.plot(women[x], women[deg[sp]], c=cb_dark_blue, label='Women', linewidth=3)
ax_obj.plot(women[x], 100-women[deg[sp]], c=cb_orange, label='Men', linewidth=3)
ax_obj.spines["right"].set_visible(False)
ax_obj.spines["left"].set_visible(False)
ax_obj.spines["top"].set_visible(False)
ax_obj.spines["bottom"].set_visible(False)
ax_obj.set_xlim(1968, 2011)
ax_obj.set_ylim(0,100)
ax_obj.set_title(deg[sp])
ax_obj.tick_params(bottom="off", top="off", left="off", right="off")

if sp == 0:
ax_obj.text(2005, 87, 'Men')
ax_obj.text(2002, 8, 'Women')
elif sp == len_deg-1:
ax_obj.text(2005, 62, 'Men')
ax_obj.text(2001, 35, 'Women')

for i in range (0,3):
diagramVertic(fig,6,3,i+1,'Year',women_degrees,cats[i])

plt.show()``````
3 Likes

You can also try these two nested loops. The text is missing, I’m still working on that.

``````fig = plt.figure(figsize=(18,20))
for i in range(3):
for j in range(len(cats[i])): #ragged column
cat = cats[i][j]