-
Notifications
You must be signed in to change notification settings - Fork 8
/
adjust_spines.py
51 lines (47 loc) · 1.73 KB
/
adjust_spines.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# http://matplotlib.org/examples/pylab_examples/spine_placement_demo.html
def adjust_spines(ax,spines, offset=10):
for loc, spine in ax.spines.items():
if loc in spines:
spine.set_position(('outward', offset)) # outward
# by 10 points
#spine.set_smart_bounds(True)
else:
spine.set_color('none') # don't
# draw spine
# turn off ticks where there
# is no spine
if 'left' in spines:
ax.yaxis.set_tick_params(length=5)
ax.yaxis.set_tick_params(direction='out')
ax.yaxis.set_ticks_position('left')
ax.yaxis.set_label_position('left')
elif 'right' in spines:
ax.yaxis.set_tick_params(length=5)
ax.yaxis.set_tick_params(direction='out')
ax.yaxis.set_ticks_position('right')
ax.yaxis.set_label_position('right')
else:
# no yaxis ticks
ax.yaxis.set_ticks([])
if 'bottom' in spines:
ax.xaxis.set_ticks_position('bottom')
ax.xaxis.set_tick_params(length=5)
ax.xaxis.set_tick_params(direction='out')
ax.xaxis.set_label_position('bottom')
elif 'top' in spines:
ax.xaxis.set_ticks_position('top')
ax.xaxis.set_tick_params(length=5)
ax.xaxis.set_tick_params(direction='out')
ax.xaxis.set_label_position('top')
else:
# no xaxis
# ticks
ax.xaxis.set_ticks([])
if __name__ == "__main__":
import numpy as np
x = np.random.random(100)
fig = plt.figure(100)
fig.clf()
ax = fig.add_axes([0.1,0.1,0.8,0.8])
ax.plot(x)
adjust_spines(ax,["left","bottom"])