|
| 1 | +import matplotlib.pyplot as plt |
| 2 | +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.iteritems(): |
| 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') |
| 26 | + |
| 27 | +# ---------------------------------------------------- |
| 28 | + |
| 29 | +fig = plt.figure() |
| 30 | +x = np.linspace(-np.pi,np.pi,100) |
| 31 | +y = 2*np.sin(x) |
| 32 | + |
| 33 | +ax = fig.add_subplot(2,2,1) |
| 34 | +ax.set_title('centered spines') |
| 35 | +ax.plot(x,y) |
| 36 | +ax.spines['left'].set_position('center') |
| 37 | +ax.spines['right'].set_color('none') |
| 38 | +ax.spines['bottom'].set_position('center') |
| 39 | +ax.spines['top'].set_color('none') |
| 40 | +ax.xaxis.set_ticks_position('bottom') |
| 41 | +ax.yaxis.set_ticks_position('left') |
| 42 | + |
| 43 | +ax = fig.add_subplot(2,2,2) |
| 44 | +ax.set_title('zeroed spines') |
| 45 | +ax.plot(x,y) |
| 46 | +ax.spines['left'].set_position('zero') |
| 47 | +ax.spines['right'].set_color('none') |
| 48 | +ax.spines['bottom'].set_position('zero') |
| 49 | +ax.spines['top'].set_color('none') |
| 50 | +ax.xaxis.set_ticks_position('bottom') |
| 51 | +ax.yaxis.set_ticks_position('left') |
| 52 | + |
| 53 | +ax = fig.add_subplot(2,2,3) |
| 54 | +ax.set_title('spines at axes (0.6, 0.1)') |
| 55 | +ax.plot(x,y) |
| 56 | +ax.spines['left'].set_position(('axes',0.6)) |
| 57 | +ax.spines['right'].set_color('none') |
| 58 | +ax.spines['bottom'].set_position(('axes',0.1)) |
| 59 | +ax.spines['top'].set_color('none') |
| 60 | +ax.xaxis.set_ticks_position('bottom') |
| 61 | +ax.yaxis.set_ticks_position('left') |
| 62 | + |
| 63 | +ax = fig.add_subplot(2,2,4) |
| 64 | +ax.set_title('spines at data (1,2)') |
| 65 | +ax.plot(x,y) |
| 66 | +ax.spines['left'].set_position(('data',1)) |
| 67 | +ax.spines['right'].set_color('none') |
| 68 | +ax.spines['bottom'].set_position(('data',2)) |
| 69 | +ax.spines['top'].set_color('none') |
| 70 | +ax.xaxis.set_ticks_position('bottom') |
| 71 | +ax.yaxis.set_ticks_position('left') |
| 72 | + |
| 73 | +# ---------------------------------------------------- |
| 74 | + |
| 75 | +def adjust_spines(ax,spines): |
| 76 | + for loc, spine in ax.spines.iteritems(): |
| 77 | + if loc in spines: |
| 78 | + spine.set_position(('outward',10)) # outward by 10 points |
| 79 | + else: |
| 80 | + spine.set_color('none') # don't draw spine |
| 81 | + |
| 82 | + # turn off ticks where there is no spine |
| 83 | + if 'left' in spines: |
| 84 | + ax.yaxis.set_ticks_position('left') |
| 85 | + else: |
| 86 | + # no yaxis ticks |
| 87 | + ax.yaxis.set_ticks([]) |
| 88 | + |
| 89 | + if 'bottom' in spines: |
| 90 | + ax.xaxis.set_ticks_position('bottom') |
| 91 | + else: |
| 92 | + # no xaxis ticks |
| 93 | + ax.xaxis.set_ticks([]) |
| 94 | + |
| 95 | +fig = plt.figure() |
| 96 | + |
| 97 | +x = np.linspace(0,2*np.pi,100) |
| 98 | +y = 2*np.sin(x) |
| 99 | + |
| 100 | +ax = fig.add_subplot(2,2,1) |
| 101 | +ax.plot(x,y) |
| 102 | +adjust_spines(ax,['left']) |
| 103 | + |
| 104 | +ax = fig.add_subplot(2,2,2) |
| 105 | +ax.plot(x,y) |
| 106 | +adjust_spines(ax,[]) |
| 107 | + |
| 108 | +ax = fig.add_subplot(2,2,3) |
| 109 | +ax.plot(x,y) |
| 110 | +adjust_spines(ax,['left','bottom']) |
| 111 | + |
| 112 | +ax = fig.add_subplot(2,2,4) |
| 113 | +ax.plot(x,y) |
| 114 | +adjust_spines(ax,['bottom']) |
| 115 | + |
| 116 | +show() |
0 commit comments