Functionize the plotting?

How would I go about creating a plot function for Guided Project: Visualizing The Gender Gap In College Degrees? I know how I would do this in R but am having difficulty applying the same logic in python. My approach : Create a function that takes the list of columns for a category, generate the plot, label annotations based on the min and max of each year for the subset.

Here is my attempt that doesn’t work :slight_smile:

%matplotlib inline
import pandas as pd
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']


 for sp in range(0,6):
		ax = fig.add_subplot(1,6,sp+1)
		group.append('Year')
		subset = women_degrees[group]
		ax.plot(subset['Year'], subset[group[sp]], c=cb_dark_blue, label='Women', linewidth=3)
		ax.plot(subset['Year'], 100-subset[group[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(group[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()

Then:
create_plot(stem_cats)

How can I get the plots to show up and automate this further?

Hi @yee.laurene,

You can do it like this:

%matplotlib inline
import pandas as pd
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']
def create_plots(lst):
    fig = plt.figure(figsize=(18, 3))

    for sp in range(0,len(lst)):
        ax = fig.add_subplot(1,len(lst),sp+1)
        ax.plot(women_degrees['Year'], women_degrees[lst[sp]], c=cb_dark_blue, label='Women', linewidth=3)
        ax.plot(women_degrees['Year'], 100-women_degrees[lst[sp]], 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(lst[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 == len(lst) - 1:
            ax.text(2005, 62, 'Men')
            ax.text(2001, 35, 'Women')
    plt.show()

create_plots(stem_cats)

create_plots(lib_arts_cats)

create_plots(other_cats)

Hope this helps :slightly_smiling_face:

Best,
Sahil

1 Like