10000 Extract spines_demo from spine_placement_demo · matplotlib/matplotlib@800deb4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 800deb4

Browse files
committed
Extract spines_demo from spine_placement_demo
1 parent acc69cc commit 800deb4

File tree

3 files changed

+55
-26
lines changed

3 files changed

+55
-26
lines changed

examples/pylab_examples/spine_placement_demo.py

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,6 @@
1-
import matplotlib.pyplot as plt
21
import numpy as np
3-
from matplotlib.pyplot import show
4-
5-
fig = plt.figure()
6-
x = np.linspace(0,2*np.pi,100)
7-
y = 2*np.sin(x)
8-
ax = fig.add_subplot(1,2,1)
9-
ax.set_title('dropped spines')
10-
ax.plot(x,y)
11-
for loc, spine in ax.spines.items():
12-
if loc in ['left','bottom']:
13-
spine.set_position(('outward',10)) # outward by 10 points
14-
elif loc in ['right','top']:
15-
spine.set_color('none') # don't draw spine
16-
else:
17-
raise ValueError('unknown spine location: %s'%loc)
18-
19-
# turn off ticks where there is no spine
20-
ax.xaxis.set_ticks_position('bottom')
21-
ax.yaxis.set_ticks_position('left')
22-
23-
ax = fig.add_subplot(1,2,2,sharex=ax)
24-
ax.plot(x,y)
25-
ax.set_title('normal spines')
2+
import matplotlib.pyplot as plt
263

27-
# ----------------------------------------------------
284

295
fig = plt.figure()
306
x = np.linspace(-np.pi,np.pi,100)
@@ -121,4 +97,4 @@ def adjust_spines(ax,spines):
12197
ax.plot(x,y)
12298
adjust_spines(ax,['bottom'])
12399

124-
show()
100+
plt.show()
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"""
2+
Basic demo of axis spines.
3+
4+
This demo compares a normal axes, with spines on all four sides, and an axes
5+
with spines only on the left and bottom.
6+
"""
7+
import numpy as np
8+
import matplotlib.pyplot as plt
9+
10+
11+
x = np.linspace(0, 2 * np.pi, 100)
12+
y = 2 * np.sin(x)
13+
14+
fig, (ax0, ax1) = plt.subplots(ncols=2)
15+
16+
ax0.plot(x, y)
17+
ax0.set_title('normal spines')
18+
19+
ax1.plot(x, y)
20+
ax1.set_title('bottom-left spines')
21+
22+
# Hide the right and top spines
23+
ax1.spines['right'].set_visible(False)
24+
ax1.spines['top'].set_visible(False)
25+
# Only show ticks on the left and bottom spines
26+
ax1.yaxis.set_ticks_position('left')
27+
ax1.xaxis.set_ticks_position('bottom')
28+
29+
plt.show()
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""
2+
Demo of spines offset from the axes (a.k.a. "dropped spines").
3+
"""
4+
import numpy as np
5+
import matplotlib.pyplot as plt
6+
7+
8+
fig, ax = plt.subplots()
9+
10+
image = np.random.uniform(size=(10, 10))
11+
ax.imshow(image, cmap=plt.cm.gray, interpolation='nearest')
12+
ax.set_title('dropped spines')
13+
14+
# Move left and bottom spines outward by 10 points
15+
ax.spines['left'].set_position(('outward', 10))
16+
ax.spines['bottom'].set_position(('outward', 10))
17+
# Hide the right and top spines
18+
ax.spines['right'].set_visible(False)
19+
ax.spines['top'].set_visible(False)
20+
# Only show ticks on the left and bottom spines
21+
ax.yaxis.set_ticks_position('left')
22+
ax.xaxis.set_ticks_position('bottom')
23+
24+
plt.show()

0 commit comments

Comments
 (0)
0