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

Skip to content

Commit acc69cc

Browse files
committed
Extract spines_demo_bounds from spine_placement_demo
1 parent ec8a487 commit acc69cc

File tree

3 files changed

+33
-37
lines changed

3 files changed

+33
-37
lines changed

examples/pylab_examples/spine_placement_demo.py

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import sys
21
import matplotlib.pyplot as plt
32
import numpy as np
43
from matplotlib.pyplot import show
@@ -122,40 +121,4 @@ def adjust_spines(ax,spines):
122121
ax.plot(x,y)
123122
adjust_spines(ax,['bottom'])
124123

125-
# ----------------------------------------------------
126-
127-
fig = plt.figure()
128-
129-
x = np.linspace(0,2*np.pi,50)
130-
y = np.sin(x)
131-
y2 = y + 0.1*np.random.normal( size=x.shape )
132-
133-
# plot data
134-
ax = fig.add_subplot(1,1,1)
135-
line1,=ax.plot(x,y,'--')
136-
line2,=ax.plot(x,y2,'bo')
137-
138-
# adjust the spines
139-
adjust_spines(ax,['left','bottom'])
140-
141-
# set ticks and tick labels
142-
# x
143-
ax.set_xlim((0,2*np.pi))
144-
ax.set_xticks([0,np.pi,2*np.pi])
145-
if sys.version_info[0] < 3:
146-
pichr = unichr(0x03C0)
147-
else:
148-
pichr = chr(0x03C0)
149-
ax.set_xticklabels(['0',pichr,'2 '+pichr])
150-
151-
# y
152-
ax.set_yticks([-1,0,1])
153-
154-
# disable clipping of data points by axes range
155-
for artist in (line1,line2):
156-
artist.set_clip_on(False)
157-
158-
# adjust spine to be within ticks
159-
ax.spines['left'].set_bounds( -1, 1 )
160-
161124
show()

examples/tests/backend_driver.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
]
8585

8686
files['ticks_and_spines'] = [
87+
'spines_demo_bounds.py',
8788
'ticklabels_demo_rotation.py',
8889
]
8990

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"""
2+
Demo of spines using custom bounds to limit the extent of the spine.
3+
"""
4+
import numpy as np
5+
import matplotlib.pyplot as plt
6+
7+
8+
x = np.linspace(0, 2*np.pi, 50)
9+
y = np.sin(x)
10+
y2 = y + 0.1 * np.random.normal(size=x.shape)
11+
12+
fig, ax = plt.subplots()
13+
ax.plot(x, y, 'k--')
14+
ax.plot(x, y2, 'ro')
15+
16+
# set ticks and tick labels
17+
ax.set_xlim((0, 2*np.pi))
18+
ax.set_xticks([0, np.pi, 2*np.pi])
19+
ax.set_xticklabels(['0', '$\pi$','2$\pi$'])
20+
ax.set_ylim((-1.5, 1.5))
21+
ax.set_yticks([-1, 0, 1])
22+
23+
# Only draw spine between the y-ticks
24+
ax.spines['left'].set_bounds(-1, 1)
25+
# Hide the right and top spines
26+
ax.spines['right'].set_visible(False)
27+
ax.spines['top'].set_visible(False)
28+
# Only show ticks on the left and bottom spines
29+
ax.yaxis.set_ticks_position('left')
30+
ax.xaxis.set_ticks_position('bottom')
31+
32+
plt.show()

0 commit comments

Comments
 (0)