Solution: Visualizing Gender gap, 6 by 3 plot grid

I would like to share with you the solution to Visualize Gender gap:

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

women_degrees = pd.read_csv('percent-bachelors-degrees-women-usa.csv')
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']

other_cats = ['Health Professions', 'Public Administration', 'Education', 'Agriculture','Business', 'Architecture']

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 = fig.add_subplot(rows,cols,plot_position[sp])
        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()

2 Likes

Nice. I will try this approach.

1 Like