Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 04492d9

Browse files
authored
Merge pull request matplotlib#24379 from timhoffm/doc-dropped-spines
DOC: Update dropped splines example
2 parents 7f843bb + e4b24fd commit 04492d9

File tree

2 files changed

+22
-59
lines changed

2 files changed

+22
-59
lines changed

galleries/examples/spines/spine_placement_demo.py

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -49,53 +49,4 @@
4949
ax.spines.bottom.set_position(('data', 2))
5050
ax.spines[['top', 'right']].set_visible(False)
5151

52-
# %%
53-
# Define a method that adjusts the location of the axis spines
54-
55-
56-
def adjust_spines(ax, spines):
57-
for loc, spine in ax.spines.items():
58-
if loc in spines:
59-
spine.set_position(('outward', 10)) # outward by 10 points
60-
else:
61-
spine.set_color('none') # don't draw spine
62-
63-
# turn off ticks where there is no spine
64-
if 'left' in spines:
65-
ax.yaxis.set_ticks_position('left')
66-
else:
67-
# no yaxis ticks
68-
ax.yaxis.set_ticks([])
69-
70-
if 'bottom' in spines:
71-
ax.xaxis.set_ticks_position('bottom')
72-
else:
73-
# no xaxis ticks
74-
ax.xaxis.set_ticks([])
75-
76-
77-
# %%
78-
# Create another figure using our new ``adjust_spines`` method
79-
80-
fig = plt.figure()
81-
82-
x = np.linspace(0, 2 * np.pi, 100)
83-
y = 2 * np.sin(x)
84-
85-
ax = fig.add_subplot(2, 2, 1)
86-
ax.plot(x, y, clip_on=False)
87-
adjust_spines(ax, ['left'])
88-
89-
ax = fig.add_subplot(2, 2, 2)
90-
ax.plot(x, y, clip_on=False)
91-
adjust_spines(ax, [])
92-
93-
ax = fig.add_subplot(2, 2, 3)
94-
ax.plot(x, y, clip_on=False)
95-
adjust_spines(ax, ['left', 'bottom'])
96-
97-
ax = fig.add_subplot(2, 2, 4)
98-
ax.plot(x, y, clip_on=False)
99-
adjust_spines(ax, ['bottom'])
100-
10152
plt.show()

galleries/examples/spines/spines_dropped.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,30 @@
88
import matplotlib.pyplot as plt
99
import numpy as np
1010

11-
# Fixing random state for reproducibility
12-
np.random.seed(19680801)
1311

14-
fig, ax = plt.subplots()
12+
def adjust_spines(ax, visible_spines):
13+
ax.label_outer(remove_inner_ticks=True)
14+
ax.grid(color='0.9')
1515

16-
image = np.random.uniform(size=(10, 10))
17-
ax.imshow(image, cmap=plt.cm.gray)
18-
ax.set_title('dropped spines')
16+
for loc, spine in ax.spines.items():
17+
if loc in visible_spines:
18+
spine.set_position(('outward', 10)) # outward by 10 points
19+
else:
20+
spine.set_visible(False)
1921

20-
# Move left and bottom spines outward by 10 points
21-
ax.spines[['left', 'bottom']].set_position(('outward', 10))
22-
# Hide the right and top spines
23-
ax.spines[['top', 'right']].set_visible(False)
22+
23+
x = np.linspace(0, 2 * np.pi, 100)
24+
25+
fig, axs = plt.subplots(2, 2)
26+
27+
axs[0, 0].plot(x, np.sin(x))
28+
axs[0, 1].plot(x, np.cos(x))
29+
axs[1, 0].plot(x, -np.cos(x))
30+
axs[1, 1].plot(x, -np.sin(x))
31+
32+
adjust_spines(axs[0, 0], ['left'])
33+
adjust_spines(axs[0, 1], [])
34+
adjust_spines(axs[1, 0], ['left', 'bottom'])
35+
adjust_spines(axs[1, 1], ['bottom'])
2436

2537
plt.show()

0 commit comments

Comments
 (0)