|
36 | 36 | # define the figure size and grid layout properties |
37 | 37 | figsize = (10, 8) |
38 | 38 | cols = 3 |
39 | | -gs = gridspec.GridSpec(len(cases) // cols + 1, cols) |
40 | | -gs.update(hspace=0.4) |
| 39 | +rows = len(cases) // cols + 1 |
41 | 40 | # define the data for cartesian plots |
42 | 41 | delta = 0.11 |
43 | 42 | x = np.linspace(0, 10 - 2 * delta, 200) + delta |
44 | 43 | y = np.sin(x) + 1.0 + delta |
45 | 44 |
|
| 45 | + |
| 46 | +def trim_axs(axs, N): |
| 47 | + """little helper to massage the axs list to have correct length...""" |
| 48 | + axs = axs.flat |
| 49 | + for ax in axs[N:]: |
| 50 | + ax.remove() |
| 51 | + return axs[:N] |
| 52 | + |
46 | 53 | ############################################################################### |
47 | 54 | # Plot each markevery case for linear x and y scales |
48 | 55 |
|
49 | | -fig1 = plt.figure(num=1, figsize=figsize) |
50 | | -ax = [] |
51 | | -for i, case in enumerate(cases): |
52 | | - row = (i // cols) |
53 | | - col = i % cols |
54 | | - ax.append(fig1.add_subplot(gs[row, col])) |
55 | | - ax[-1].set_title('markevery=%s' % str(case)) |
56 | | - ax[-1].plot(x, y, 'o', ls='-', ms=4, markevery=case) |
| 56 | +fig1, axs = plt.subplots(rows, cols, figsize=figsize, constrained_layout=True) |
| 57 | +axs = trim_axs(axs, len(cases)) |
| 58 | +for ax, case in zip(axs, cases): |
| 59 | + ax.set_title('markevery=%s' % str(case)) |
| 60 | + ax.plot(x, y, 'o', ls='-', ms=4, markevery=case) |
57 | 61 |
|
58 | 62 | ############################################################################### |
59 | 63 | # Plot each markevery case for log x and y scales |
60 | 64 |
|
61 | | -fig2 = plt.figure(num=2, figsize=figsize) |
62 | | -axlog = [] |
63 | | -for i, case in enumerate(cases): |
64 | | - row = (i // cols) |
65 | | - col = i % cols |
66 | | - axlog.append(fig2.add_subplot(gs[row, col])) |
67 | | - axlog[-1].set_title('markevery=%s' % str(case)) |
68 | | - axlog[-1].set_xscale('log') |
69 | | - axlog[-1].set_yscale('log') |
70 | | - axlog[-1].plot(x, y, 'o', ls='-', ms=4, markevery=case) |
71 | | -fig2.tight_layout() |
| 65 | +fig2, axs = plt.subplots(rows, cols, figsize=figsize, constrained_layout=True) |
| 66 | +axs = trim_axs(axs, len(cases)) |
| 67 | +for ax, case in zip(axs, cases): |
| 68 | + ax.set_title('markevery=%s' % str(case)) |
| 69 | + ax.set_xscale('log') |
| 70 | + ax.set_yscale('log') |
| 71 | + ax.plot(x, y, 'o', ls='-', ms=4, markevery=case) |
72 | 72 |
|
73 | 73 | ############################################################################### |
74 | 74 | # Plot each markevery case for linear x and y scales but zoomed in |
75 | 75 | # note the behaviour when zoomed in. When a start marker offset is specified |
76 | 76 | # it is always interpreted with respect to the first data point which might be |
77 | 77 | # different to the first visible data point. |
78 | 78 |
|
79 | | -fig3 = plt.figure(num=3, figsize=figsize) |
80 | | -axzoom = [] |
81 | | -for i, case in enumerate(cases): |
82 | | - row = (i // cols) |
83 | | - col = i % cols |
84 | | - axzoom.append(fig3.add_subplot(gs[row, col])) |
85 | | - axzoom[-1].set_title('markevery=%s' % str(case)) |
86 | | - axzoom[-1].plot(x, y, 'o', ls='-', ms=4, markevery=case) |
87 | | - axzoom[-1].set_xlim((6, 6.7)) |
88 | | - axzoom[-1].set_ylim((1.1, 1.7)) |
89 | | -fig3.tight_layout() |
| 79 | +fig3, axs = plt.subplots(rows, cols, figsize=figsize, constrained_layout=True) |
| 80 | +axs = trim_axs(axs, len(cases)) |
| 81 | +for ax, case in zip(axs, cases): |
| 82 | + ax.set_title('markevery=%s' % str(case)) |
| 83 | + ax.plot(x, y, 'o', ls='-', ms=4, markevery=case) |
| 84 | + ax.set_xlim((6, 6.7)) |
| 85 | + ax.set_ylim((1.1, 1.7)) |
90 | 86 |
|
91 | 87 | # define data for polar plots |
92 | 88 | r = np.linspace(0, 3.0, 200) |
|
95 | 91 | ############################################################################### |
96 | 92 | # Plot each markevery case for polar plots |
97 | 93 |
|
98 | | -fig4 = plt.figure(num=4, figsize=figsize) |
99 | | -axpolar = [] |
100 | | -for i, case in enumerate(cases): |
101 | | - row = (i // cols) |
102 | | - col = i % cols |
103 | | - axpolar.append(fig4.add_subplot(gs[row, col], projection='polar')) |
104 | | - axpolar[-1].set_title('markevery=%s' % str(case)) |
105 | | - axpolar[-1].plot(theta, r, 'o', ls='-', ms=4, markevery=case) |
106 | | -fig4.tight_layout() |
| 94 | +fig4, axs = plt.subplots(rows, cols, figsize=figsize, |
| 95 | + subplot_kw={'projection': 'polar'}, constrained_layout=True) |
| 96 | +axs = trim_axs(axs, len(cases)) |
| 97 | +for ax, case in zip(axs, cases): |
| 98 | + ax.set_title('markevery=%s' % str(case)) |
| 99 | + ax.plot(theta, r, 'o', ls='-', ms=4, markevery=case) |
107 | 100 |
|
108 | 101 | plt.show() |
0 commit comments