Gallery - nschloe/dufte GitHub Wiki

Bachelor's degrees by gender

See https://matplotlib.org/stable/gallery/showcase/bachelors_degrees_by_gender.html.

import csv

import dufte
import matplotlib.pyplot as plt
import numpy as np

# read the data
filename = "percent_bachelors_degrees_women_usa.csv"
with open(filename) as f:
    reader = csv.reader(f)
    labels = next(reader)
labels = labels[1:]
#
data = np.array(np.genfromtxt(filename, delimiter=","))[1:]
year = data[:, 0]
data = data[:, 1:].T

# sort by last value
idx = np.argsort(-data[:, -1])
data = data[idx]
labels = [labels[i] for i in idx]

# create the plot
plt.style.use(dufte.style)

fig, ax = plt.subplots(1, 1, figsize=(12, 14))

for label, values in zip(labels, data):
    ax.plot(year, values, label=label)

plt.title(
    "Percentage of Bachelor's degrees conferred to women in "
    "the U.S.A. by major (1970-2011)"
)
plt.ylim(0, 100)

ax.yaxis.set_major_formatter("{x:.0f}%")
dufte.legend()

plt.savefig("out.png", bbox_inches="tight")