|
25 | 25 | Y2 = 1+np.cos(1+X/0.75)/2 |
26 | 26 | Y3 = np.random.uniform(Y1, Y2, len(X)) |
27 | 27 |
|
28 | | -fig = plt.figure(figsize=(8, 8)) |
29 | | -marg = 0.15 |
30 | | -ax = fig.add_axes([marg, marg, 1-1.8*marg, 1-1.8*marg], aspect=1) |
| 28 | +fig = plt.figure(figsize=(7.5, 7.5)) |
| 29 | +ax = fig.add_axes([0.2, 0.17, 0.68, 0.7], aspect=1) |
31 | 30 |
|
32 | 31 | ax.xaxis.set_major_locator(MultipleLocator(1.000)) |
33 | 32 | ax.xaxis.set_minor_locator(AutoMinorLocator(4)) |
|
53 | 52 | ax.set_title("Anatomy of a figure", fontsize=20, verticalalignment='bottom') |
54 | 53 | ax.set_xlabel("x Axis label", fontsize=14) |
55 | 54 | ax.set_ylabel("y Axis label", fontsize=14) |
56 | | - |
57 | 55 | ax.legend(loc="upper right", fontsize=14) |
58 | 56 |
|
59 | 57 |
|
60 | 58 | # Annotate the figure |
61 | 59 |
|
62 | | -def annotate(x, y, text, code, radius=0.15): |
| 60 | +def annotate(x, y, text, code): |
63 | 61 | # Circle marker |
64 | | - c = Circle((x, y), radius, clip_on=False, zorder=10, linewidth=2.5, |
| 62 | + c = Circle((x, y), radius=0.15, clip_on=False, zorder=10, linewidth=2.5, |
65 | 63 | edgecolor=royal_blue + [0.6], facecolor='none', |
66 | | - path_effects=[withStroke(linewidth=7, foreground=(1, 1, 1, 1))]) |
| 64 | + path_effects=[withStroke(linewidth=7, foreground='white')]) |
67 | 65 | ax.add_artist(c) |
68 | 66 |
|
69 | | - ax.text(x, y-0.2, text, zorder=100, |
70 | | - ha='center', va='top', weight='bold', color=royal_blue, |
71 | | - style='italic', fontfamily='monospace', |
72 | | - path_effects=[withStroke(linewidth=7, foreground=(1, 1, 1, 1))]) |
73 | | - |
74 | | - ax.text(x, y-0.33, code, zorder=100, |
75 | | - ha='center', va='top', weight='normal', color='0.0', |
76 | | - fontfamily='Courier New', fontsize='medium', |
77 | | - path_effects=[withStroke(linewidth=7, foreground=(1, 1, 1, 1))]) |
78 | | - |
79 | | - |
80 | | -annotate(3.25, -0.10, "Minor tick label", "ax.xaxis.set_minor_formatter") |
81 | | -annotate(-0.03, 1.05, "Major tick", "ax.yaxis.set_major_locator") |
| 67 | + # use path_effects as a background for the texts |
| 68 | + # draw the path_effects and the colored text separately so that the |
| 69 | + # path_effects cannot clip other texts |
| 70 | + for path_effects in [[withStroke(linewidth=7, foreground='white')], []]: |
| 71 | + color = 'white' if path_effects else royal_blue |
| 72 | + ax.text(x, y-0.2, text, zorder=100, |
| 73 | + ha='center', va='top', weight='bold', color=color, |
| 74 | + style='italic', fontfamily='Courier New', |
| 75 | + path_effects=path_effects) |
| 76 | + |
| 77 | + color = 'white' if path_effects else 'black' |
| 78 | + ax.text(x, y-0.33, code, zorder=100, |
| 79 | + ha='center', va='top', weight='normal', color=color, |
| 80 | + fontfamily='monospace', fontsize='medium', |
| 81 | + path_effects=path_effects) |
| 82 | + |
| 83 | + |
| 84 | +annotate(3.5, -0.13, "Minor tick label", "ax.xaxis.set_minor_formatter") |
| 85 | +annotate(-0.03, 1.0, "Major tick", "ax.yaxis.set_major_locator") |
82 | 86 | annotate(0.00, 3.75, "Minor tick", "ax.yaxis.set_minor_locator") |
83 | 87 | annotate(-0.15, 3.00, "Major tick label", "ax.yaxis.set_major_formatter") |
84 | | -annotate(1.90, -0.32, "xlabel", "ax.set_xlabel") |
85 | | -annotate(-0.27, 1.68, "ylabel", "ax.set_ylabel") |
86 | | -annotate(1.58, 4.13, "Title", "ax.set_title") |
| 88 | +annotate(1.68, -0.39, "xlabel", "ax.set_xlabel") |
| 89 | +annotate(-0.38, 1.67, "ylabel", "ax.set_ylabel") |
| 90 | +annotate(1.52, 4.15, "Title", "ax.set_title") |
87 | 91 | annotate(1.75, 2.80, "Line", "ax.plot") |
88 | 92 | annotate(2.25, 1.54, "Markers", "ax.scatter") |
89 | 93 | annotate(3.00, 3.00, "Grid", "ax.grid") |
90 | | -annotate(3.60, 3.65, "Legend", "ax.legend") |
| 94 | +annotate(3.60, 3.58, "Legend", "ax.legend") |
91 | 95 | annotate(2.5, 0.55, "Axes", "fig.subplots") |
92 | | -annotate(4.185, 4.3, "Figure", "plt.figure") |
| 96 | +annotate(4, 4.5, "Figure", "plt.figure") |
93 | 97 | annotate(0.65, 0.01, "x Axis", "ax.xaxis") |
94 | | -annotate(0, 0.44, "y Axis", "ax.yaxis") |
| 98 | +annotate(0, 0.36, "y Axis", "ax.yaxis") |
95 | 99 | annotate(4.0, 0.7, "Spine", "ax.spines") |
96 | 100 |
|
97 | 101 | # frame around figure |
98 | | -fig.patch.set(linewidth=10, edgecolor='0.5') |
| 102 | +fig.patch.set(linewidth=4, edgecolor='0.5') |
99 | 103 | plt.show() |
100 | 104 |
|
101 | 105 |
|
|
0 commit comments