Skip to content

Commit f039a02

Browse files
authored
Merge pull request #21923 from meeseeksmachine/auto-backport-of-pr-21753-on-v3.5.0-doc
Backport PR #21753 on branch v3.5.0-doc (DOC: update anatomy of figure)
2 parents 144e6ee + 9274d0d commit f039a02

File tree

1 file changed

+74
-67
lines changed

1 file changed

+74
-67
lines changed

examples/showcase/anatomy.py

Lines changed: 74 additions & 67 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.15
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,110 +46,107 @@ 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)
65+
66+
ax.legend(loc="upper right", fontsize=14)
5467

55-
ax.legend(loc="upper right")
68+
# Annotate the figure
5669

5770

58-
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)
71+
def just_circle(x, y, radius=0.15):
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', 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

72-
# Minor tick
73-
circle(0.50, -0.10)
74-
text(0.50, -0.32, "Minor tick label")
85+
def code(x, y, text):
86+
ax.text(x, y, text, zorder=100,
87+
ha='center', va='top', weight='normal', color='0.0',
88+
fontfamily='Courier New', fontsize='medium',
89+
path_effects=[withStroke(linewidth=7, foreground=(1, 1, 1, 1))])
90+
91+
92+
def circle(x, y, txt, cde, radius=0.15):
93+
just_circle(x, y, radius=radius)
94+
text(x, y-0.2, txt)
95+
code(x, y-0.33, cde)
96+
97+
# Minor tick label
98+
circle(3.25, -0.10, "Minor tick label",
99+
"ax.xaxis.set_minor_formatter")
75100

76101
# Major tick
77-
circle(-0.03, 4.00)
78-
text(0.03, 3.80, "Major tick")
102+
circle(-0.03, 1.05, "Major tick", "ax.yaxis.set_major_locator")
79103

80104
# Minor tick
81-
circle(0.00, 3.50)
82-
text(0.00, 3.30, "Minor tick")
105+
y = 3.75
106+
circle(0.00, 3.75, "Minor tick", "ax.yaxis.set_minor_locator")
83107

84108
# Major tick label
85-
circle(-0.15, 3.00)
86-
text(-0.15, 2.80, "Major tick label")
109+
circle(-0.15, 3.00, "Major tick label", "ax.yaxis.set_major_formatter")
87110

88111
# X Label
89-
circle(1.80, -0.27)
90-
text(1.80, -0.45, "X axis label")
112+
circle(1.90, -0.32, "xlabel", "ax.set_xlabel")
91113

92114
# Y Label
93-
circle(-0.27, 1.80)
94-
text(-0.27, 1.6, "Y axis label")
115+
circle(-0.27, 1.68, "ylabel", "ax.set_ylabel")
95116

96117
# Title
97-
circle(1.60, 4.13)
98-
text(1.60, 3.93, "Title")
118+
circle(1.58, 4.13, "Title", "ax.set_title")
99119

100120
# Blue plot
101-
circle(1.75, 2.80)
102-
text(1.75, 2.60, "Line\n(line plot)")
103-
104-
# Red plot
105-
circle(1.20, 0.60)
106-
text(1.20, 0.40, "Line\n(line plot)")
121+
circle(1.75, 2.80, "Line", "ax.plot")
107122

108123
# Scatter plot
109-
circle(3.20, 1.75)
110-
text(3.20, 1.55, "Markers\n(scatter plot)")
124+
circle(2.25, 1.54, "Markers", "ax.scatter")
111125

112126
# Grid
113-
circle(3.00, 3.00)
114-
text(3.00, 2.80, "Grid")
127+
circle(3.00, 3.00, "Grid", "ax.grid")
115128

116129
# Legend
117-
circle(3.70, 3.80)
118-
text(3.70, 3.60, "Legend")
130+
circle(3.60, 3.65, "Legend", "ax.legend")
119131

120132
# Axes
121-
circle(0.5, 0.5)
122-
text(0.5, 0.3, "Axes")
133+
circle(2.5, 0.55, "Axes", "fig.subplots")
123134

124135
# 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')
136+
circle(4.185, 4.3, "Figure", "plt.figure")
137+
138+
# x Axis
139+
circle(0.65, 0.01, "x Axis", "ax.xaxis")
140+
141+
# y Axis
142+
circle(0, 0.44, "y Axis", "ax.yaxis")
143+
144+
# Spine
145+
circle(4.0, 0.7, "Spine", "ax.spines")
146+
147+
# frame around figure...
148+
fig.add_artist(Rectangle((0, 0), width=1, height=1, facecolor='none',
149+
edgecolor='0.5', linewidth=10))
143150

144151
plt.show()
145152

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy