We have next source code:
%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 = ['Engineering', 'Computer Science', 'Psychology', 'Biology', 'Physical Sciences', 'Math and Statistics']
fig = plt.figure(figsize=(18, 3))
for sp in range(0,6):
ax = fig.add_subplot(1,6,sp+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)
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()
Let's modify it that we get finally required plot.
stem_cats = ['Engineering', 'Computer Science', 'Psychology', 'Biology',
'Physical Sciences', 'Math and Statistics']
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']
# Concatenate lists
all_cats = [stem_cats, lib_arts_cats, other_cats]
# Define max rows from list of lists
def len_list(x):
if isinstance (x, list):
yield len(x)
for y in x:
yield from len_list(y)
# Set dimensions for plot using by axis
ax_row = max(len_list(all_cats))
ax_col = len(all_cats)
# Define subpot
fig, ax = plt.subplots(ax_row,ax_col, figsize=(12, 24))
# Ierate column
for i, row in enumerate (all_cats):
# Iterate row
for j, column in enumerate(row):
# Define max for plot other styles for bottonest in columns
bottonest = len(row) - 1
ax[j,i].plot(women_degrees['Year'], women_degrees[all_cats[i][j]],
c=cb_dark_blue, label='Women', linewidth=3)
ax[j,i].plot(women_degrees['Year'], 100-women_degrees[all_cats[i][j]],
c=cb_orange, label='Men', linewidth=3)
# Make invisible spines
ax[j,i].spines["right"].set_visible(False)
ax[j,i].spines["left"].set_visible(False)
ax[j,i].spines["top"].set_visible(False)
ax[j,i].spines["bottom"].set_visible(False)
ax[j,i].set_xlim(1968, 2011)
ax[j,i].set_ylim(0,100)
ax[j,i].set_title(all_cats[i][j])
ax[j,i].tick_params(bottom=False, top=False, left=False, right=False,
labelbottom=False)
# Make Invisible x labels
ax[j,i].set_xlabel(xlabel = '0', visible = False)
ax[j,i].set_yticks([0,100])
# Make vertical line by y = 50
ax[j,i].axhline(50, c=(171/255, 171/255, 171/255), alpha=0.3)
# Make different plot style for bottonest elements of columns
if j == bottonest :
# Label for men
ax[j,i].text(2006, 89, 'Men')
# Label for women
ax[j,i].text(2004, 25, 'Women')
# Where place label
ax[j,i].tick_params(bottom=True, top=False, left=False,
right=False, labelbottom='on')
ax[j,i].set_xlabel(xlabel = '', visible = True)
# Tight suboplots
plt.tight_layout(pad = 0, h_pad = 3, w_pad = 2)
# Save final image
plt.savefig('gender_degrees.png')
We modify source code and unbind it from fixed predefined values of rows and columns and can print list of lists with variable length.
Created on Feb 10, 2021
@author: Vadim Maklakov, used some ideas from public Internet resources.
© 3-clause BSD License
Software environment:
Debian 10.7
Python 3.8.7
required preinstalled next python modules:
pandas
matplotlib