Skip to content

Commit 50a480a

Browse files
committed
Improve formatting of "Anatomy of a figure"
- Adapt margins - Reduce figure border width (still thick but not massive) - fontfamily: make code monospace - slightly move annotations to better places
1 parent 03f153e commit 50a480a

File tree

2 files changed

+31
-27
lines changed

2 files changed

+31
-27
lines changed

doc/_static/anatomy.png

-169 KB
Loading

examples/showcase/anatomy.py

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,8 @@
2525
Y2 = 1+np.cos(1+X/0.75)/2
2626
Y3 = np.random.uniform(Y1, Y2, len(X))
2727

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)
3130

3231
ax.xaxis.set_major_locator(MultipleLocator(1.000))
3332
ax.xaxis.set_minor_locator(AutoMinorLocator(4))
@@ -53,49 +52,54 @@
5352
ax.set_title("Anatomy of a figure", fontsize=20, verticalalignment='bottom')
5453
ax.set_xlabel("x Axis label", fontsize=14)
5554
ax.set_ylabel("y Axis label", fontsize=14)
56-
5755
ax.legend(loc="upper right", fontsize=14)
5856

5957

6058
# Annotate the figure
6159

62-
def annotate(x, y, text, code, radius=0.15):
60+
def annotate(x, y, text, code):
6361
# 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,
6563
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')])
6765
ax.add_artist(c)
6866

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")
8286
annotate(0.00, 3.75, "Minor tick", "ax.yaxis.set_minor_locator")
8387
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")
8791
annotate(1.75, 2.80, "Line", "ax.plot")
8892
annotate(2.25, 1.54, "Markers", "ax.scatter")
8993
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")
9195
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")
9397
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")
9599
annotate(4.0, 0.7, "Spine", "ax.spines")
96100

97101
# frame around figure
98-
fig.patch.set(linewidth=10, edgecolor='0.5')
102+
fig.patch.set(linewidth=4, edgecolor='0.5')
99103
plt.show()
100104

101105

0 commit comments

Comments
 (0)