|
1 | 1 | """
|
2 |
| -============ |
3 |
| -Figure title |
4 |
| -============ |
| 2 | +============================================= |
| 3 | +Figure labels: suptitle, supxlabel, supylabel |
| 4 | +============================================= |
5 | 5 |
|
6 |
| -Each subplot can have its own title (`.Axes.set_title`). Additionally, |
7 |
| -`.Figure.suptitle` adds a centered title at the top of the figure. |
| 6 | +Often it is desirable to have a title for a figure, while each subplot gets |
| 7 | +its own title using `.FigureBase.suptitle`. |
| 8 | +
|
| 9 | +We can also add figure-level supxlabel and supylabel |
8 | 10 | """
|
| 11 | +from matplotlib.cbook import get_sample_data |
9 | 12 | import matplotlib.pyplot as plt
|
| 13 | + |
10 | 14 | import numpy as np
|
11 | 15 |
|
12 | 16 |
|
|
24 | 28 |
|
25 | 29 | fig.suptitle('Different types of oscillations', fontsize=16)
|
26 | 30 |
|
| 31 | +############################################################################## |
| 32 | +# Note it is also sometimes useful to have a global x- or y-label, which |
| 33 | +# can be achieved with the `.FigureBase.supxlabel` and `.FigureBase.supylabel` |
| 34 | +# methods. |
| 35 | + |
| 36 | +fig, axs = plt.subplots(3, 5, figsize=(8, 5), constrained_layout=True, |
| 37 | + sharey=True, sharex=True) |
| 38 | + |
| 39 | +fname = get_sample_data('percent_bachelors_degrees_women_usa.csv', |
| 40 | + asfileobj=False) |
| 41 | +gender_degree_data = np.genfromtxt(fname, delimiter=',', names=True) |
| 42 | + |
| 43 | +majors = ['Health Professions', 'Public Administration', 'Education', |
| 44 | + 'Psychology', 'Foreign Languages', 'English', |
| 45 | + 'Art and Performance', 'Biology', |
| 46 | + 'Agriculture', 'Business', |
| 47 | + 'Math and Statistics', 'Architecture', 'Physical Sciences', |
| 48 | + 'Computer Science', 'Engineering'] |
| 49 | + |
| 50 | +for nn, ax in enumerate(axs.flat): |
| 51 | + ax.set_xlim(1969.5, 2011.1) |
| 52 | + column = majors[nn] |
| 53 | + column_rec_name = column.replace('\n', '_').replace(' ', '_') |
| 54 | + |
| 55 | + line, = ax.plot('Year', column_rec_name, data=gender_degree_data, |
| 56 | + lw=2.5) |
| 57 | + ax.set_title(column, fontsize='small', loc='left') |
| 58 | + ax.set_ylim([0, 100]) |
| 59 | + ax.grid() |
| 60 | +fig.supxlabel('Year') |
| 61 | +fig.supylabel('Percent Degrees Awarded To Women') |
| 62 | + |
27 | 63 | plt.show()
|
0 commit comments