8000 DOC: update anatomy of figure · matplotlib/matplotlib@bbbce2b · GitHub
[go: up one dir, main page]

Skip to content

Commit bbbce2b

Browse files
committed
DOC: update anatomy of figure
1 parent 756d1d4 commit bbbce2b

File tree

1 file changed

+61
-53
lines changed

1 file changed

+61
-53
lines changed

examples/showcase/anatomy.py

Lines changed: 61 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,29 @@
66
This figure shows the name of several matplotlib elements composing a figure
77
"""
88

9+
910
import numpy as np
1011
import matplotlib.pyplot as plt
12+
from matplotlib.patches import Circle, Rectangle
13+
from matplotlib.patheffects import withStroke
1114
from matplotlib.ticker import AutoMinorLocator, MultipleLocator
1215

16+
royal_blue = "#002082"
17+
royal_blue = [0, 20/256, 82/256]
18+
19+
# make the figure
20+
1321
np.random.seed(19680801)
1422

1523
X = np.linspace(0.5, 3.5, 100)
1624
Y1 = 3+np.cos(X)
1725
Y2 = 1+np.cos(1+X/0.75)/2
1826
Y3 = np.random.uniform(Y1, Y2, len(X))
1927

20-
fig = plt.figure(figsize=(8, 8))
21-
ax = fig.add_subplot(1, 1, 1, aspect=1)
28+
fig = plt.figure(figsize=(8, 8), facecolor='1')
29+
marg = 0.13
30+
ax = fig.add_axes([marg, marg, 1-1.8*marg, 1-1.8*marg], aspect=1,
31+
facecolor='1')
2232

2333

2434
def minor_tick(x, pos):
@@ -36,42 +46,45 @@ def minor_tick(x, pos):
3646
ax.set_xlim(0, 4)
3747
ax.set_ylim(0, 4)
3848

39-
ax.tick_params(which='major', width=1.0)
40-
ax.tick_params(which='major', length=10)
49+
ax.tick_params(which='major', width=1.0, labelsize=14)
50+
ax.tick_params(which='major', length=10, labelsize=14)
4151
ax.tick_params(which='minor', width=1.0, labelsize=10)
4252
ax.tick_params(which='minor', length=5, labelsize=10, labelcolor='0.25')
4353

4454
ax.grid(linestyle="--", linewidth=0.5, color='.25', zorder=-10)
4555

46-
ax.plot(X, Y1, c=(0.25, 0.25, 1.00), lw=2, label="Blue signal", zorder=10)
47-
ax.plot(X, Y2, c=(1.00, 0.25, 0.25), lw=2, label="Red signal")
48-
ax.plot(X, Y3, linewidth=0,
49-
marker='o', markerfacecolor='w', markeredgecolor='k')
56+
ax.plot(X, Y1, c='C0', lw=2.5, label="Blue signal", zorder=10)
57+
ax.plot(X, Y2, c='C1', lw=2.5, label="Orange signal")
58+
ax.plot(X[::3], Y3[::3], linewidth=0, markersize=9,
59+
marker='s', markerfacecolor='none', markeredgecolor='C4',
60+
markeredgewidth=2.5)
5061

5162
ax.set_title("Anatomy of a figure", fontsize=20, verticalalignment='bottom')
52-
ax.set_xlabel("X axis label")
53-
ax.set_ylabel("Y axis label")
63+
ax.set_xlabel("x Axis label", fontsize=14)
64+
ax.set_ylabel("y Axis label", fontsize=14)
5465

55-
ax.legend(loc="upper right")
66+
ax.legend(loc="upper right", fontsize=14)
67+
68+
# Annotate the figure
5669

5770

5871
def circle(x, y, radius=0.15):
59-
from matplotlib.patches import Circle
60-
from matplotlib.patheffects import withStroke
61-
circle = Circle((x, y), radius, clip_on=False, zorder=10, linewidth=1,
62-
edgecolor='black', facecolor=(0, 0, 0, .0125),
63-
path_effects=[withStroke(linewidth=5, foreground='w')])
64-
ax.add_artist(circle)
72+
c = Circle((x, y), radius, clip_on=False, zorder=10, linewidth=2.5,
73+
edgecolor=royal_blue + [0.6], facecolor='none',
74+
path_effects=[withStroke(linewidth=7, foreground=(1, 1, 1, 1))])
75+
ax.add_artist(c)
6576

6677

6778
def text(x, y, text):
68-
ax.text(x, y, text, backgroundcolor="white",
69-
ha='center', va='top', weight='bold', color='blue')
79+
ax.text(x, y, text, zorder=100,
80+
ha='center' 6D47 , va='top', weight='bold', color=royal_blue,
81+
style='italic', fontfamily='monospace',
82+
path_effects=[withStroke(linewidth=7, foreground=(1, 1, 1, 1))])
7083

7184

7285
# Minor tick
73-
circle(0.50, -0.10)
74-
text(0.50, -0.32, "Minor tick label")
86+
circle(3.25, -0.10)
87+
text(3.25, -0.32, "Minor tick label")
7588

7689
# Major tick
7790
circle(-0.03, 4.00)
@@ -86,60 +99,55 @@ def text(x, y, text):
8699
text(-0.15, 2.80, "Major tick label")
87100

88101
# X Label
89-
circle(1.80, -0.27)
90-
text(1.80, -0.45, "X axis label")
102+
circle(1.90, -0.27)
103+
text(1.90, -0.47, "xlabel")
91104

92105
# Y Label
93-
circle(-0.27, 1.80)
94-
text(-0.27, 1.6, "Y axis label")
106+
circle(-0.27, 1.75)
107+
text(-0.27, 1.55, "ylabel")
95108

96109
# Title
97-
circle(1.60, 4.13)
98-
text(1.60, 3.93, "Title")
110+
circle(1.58, 4.13)
111+
text(1.58, 3.93, "Title")
99112

100113
# Blue plot
101114
circle(1.75, 2.80)
102115
text(1.75, 2.60, "Line\n(line plot)")
103116

104-
# Red plot
105-
circle(1.20, 0.60)
106-
text(1.20, 0.40, "Line\n(line plot)")
107-
108117
# Scatter plot
109-
circle(3.20, 1.75)
110-
text(3.20, 1.55, "Markers\n(scatter plot)")
118+
circle(2.25, 1.54)
119+
text(2.25, 1.34, "Markers\n(scatter plot)")
111120

112121
# Grid
113122
circle(3.00, 3.00)
114123
text(3.00, 2.80, "Grid")
115124

116125
# Legend
117-
circle(3.70, 3.80)
118-
text(3.70, 3.60, "Legend")
126+
circle(3.60, 3.65)
127+
text(3.60, 3.45, "Legend")
119128

120129
# Axes
121130
circle(0.5, 0.5)
122131
text(0.5, 0.3, "Axes")
123132

124133
# Figure
125-
circle(-0.3, 0.65)
126-
text(-0.3, 0.45, "Figure")
127-
128-
color = 'blue'
129-
ax.annotate('Spines', xy=(4.0, 0.35), xytext=(3.3, 0.5),
130-
weight='bold', color=color,
131-
arrowprops=dict(arrowstyle='->',
132-
connectionstyle="arc3",
133-
color=color))
134-
135-
ax.annotate('', xy=(3.15, 0.0), xytext=(3.45, 0.45),
136-
weight='bold', color=color,
137-
arrowprops=dict(arrowstyle='->',
138-
connectionstyle="arc3",
139-
color=color))
140-
141-
ax.text(4.0, -0.4, "Made with https://matplotlib.org",
142-
fontsize=10, ha="right", color='.5')
134+
circle(4.185, 4.3)
135+
text(4.185, 4.1, "Figure")
136+
137+
# Axis
138+
circle(0.65, 0.01)
139+
text(0.65, -0.2, "x Axis")
140+
141+
# Axis
142+
circle(0, 0.7)
143+
text(0, 0.7-0.2, "y Axis")
144+
145+
# Spine
146+
circle(4.0, 0.7)
147+
text(4.0, 0.7-0.2, "Spine")
148+
149+
fig.add_artist(Rectangle((0, 0), width=1, height=1, facecolor='none',
150+
edgecolor='0.5', linewidth=10))
143151

144152
plt.show()
145153

0 commit comments

Comments
 (0)
0