The data read in from the file
eg7-marriage-ages.txt, giving the median age at first marriage in the United States for 13 decades since 1890 are plotted by the program below. Grid lines are turned on for both axes with
ax.grid() and custom markers are used for the data points themselves.
import numpy as np import matplotlib.pyplot as plt year, age_m, age_f = np.loadtxt('eg7-marriage-ages.txt', unpack=True,skiprows=3) fig = plt.figure() ax = fig.add_subplot(111) # Plot ages with male or female symbols as markers ax.plot(year, age_m, marker='$\u2642$', markersize=14, c='blue', lw=2, mfc='blue', mec='blue') ax.plot(year, age_f, marker='$\u2640$', markersize=14, c='magenta', lw=2, mfc='magenta', mec='magenta') ax.grid() ax.set_xlabel('Year') ax.set_ylabel('Age') ax.set_title('Median age at first marriage in the US, 1890 - 2010') plt.show()