8000 Arbitrary spine placement · matplotlib/matplotlib@43ca13b · GitHub
[go: up one dir, main page]

Skip to content

Commit 43ca13b

Browse files
committed
Arbitrary spine placement
svn path=/trunk/matplotlib/; revision=7144
1 parent 030d8fe commit 43ca13b

File tree

11 files changed

+491
-33
lines changed

11 files changed

+491
-33
lines changed

CHANGELOG

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
2009-05-26 Add support for "axis spines" to have arbitrary location. -ADS
2+
13
2009-05-20 Add an empty matplotlibrc to the tests/ directory so that running
24
tests will use the default set of rcparams rather than the user's
35
config. - RMM

doc/api/api_changes.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ list may help describe what changes may be necessary in your code.
2020
Changes beyond 0.98.x
2121
=====================
2222

23+
* Axes instanaces no longer have a "frame" attribute. Instead, use the
24+
new "spines" attribute. Spines is a dictionary where the keys are
25+
the names of the spines (e.g. 'left','right' and so on) and the
26+
values are the artists that draw the spines. For normal
27+
(rectilinear) axes, these artists are Line2D instances. For other
28+
axes (such as polar axes), these artists may be Patch instances.
29+
2330
* Polar plots no longer accept a resolution kwarg. Instead, each Path
2431
must specify its own number of interpolation steps. This is
2532
unlikely to be a user-visible change -- if interpolation of data is

doc/users/whats_new.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,18 @@
44
What's new in matplotlib
55
***************************
66

7+
.. _whats-new-svn:
8+
9+
What new in svn
10+
===============
11+
12+
Axis spine placement
13+
--------------------
14+
15+
Andrew Straw has added the ability to place "axis spines" -- the lines
16+
that denote the data limits -- in various arbitrary locations. See
17+
:class:`matplotlib.spines.Spine`.
18+
719
.. _whats-new-0-98-4:
820

921
What new in 0.98.4

examples/api/custom_projection_example.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
from matplotlib.transforms import Affine2D, Affine2DBase, Bbox, \
77
BboxTransformTo, IdentityTransform, Transform, TransformWrapper
88
from matplotlib.projections import register_projection
9+
import matplotlib.spines as mspines
10+
import matplotlib.axis as maxis
911

1012
import numpy as np
1113

@@ -32,6 +34,14 @@ def __init__(self, *args, **kwargs):
3234
self.set_aspect(0.5, adjustable='box', anchor='C')
3335
self.cla()
3436

37+
def _init_axis(self):
38+
self.xaxis = maxis.XAxis(self)
39+
self.yaxis = maxis.YAxis(self)
40+
# Do not register xaxis or yaxis with spines -- as done in
41+
# Axes._init_axis() -- until HammerAxes.xaxis.cla() works.
42+
# self.spines['hammer'].register_axis(self.yaxis)
43+
self._update_transScale()
44+
3545
def cla(self):
3646
"""
3747
Override to set up some reasonable defaults.
@@ -163,11 +173,12 @@ def _set_lim_and_transforms(self):
163173
yaxis_text_base + \
164174
Affine2D().translate(8.0, 0.0)
165175

166-
def get_xaxis_transform(self):
176+
def get_xaxis_transform(self,which=None):
167177
"""
168178
Override this method to provide a transformation for the
169179
x-axis grid and ticks.
170180
"""
181+
assert which in ['tick1','tick2','grid']
171182
return self._xaxis_transform
172183

173184
def get_xaxis_text1_transform(self, pixelPad):
@@ -188,11 +199,12 @@ def get_xaxis_text2_transform(self, pixelPad):
188199
"""
189200
return self._xaxis_text2_transform, 'top', 'center'
190201

191-
def get_yaxis_transform(self):
202+
def get_yaxis_transform(self,which=None):
192203
"""
193204
Override this method to provide a transformation for the
194205
y-axis grid and ticks.
195206
"""
207+
assert which in ['tick1','tick2','grid']
196208
return self._yaxis_transform
197209

198210
def get_yaxis_text1_transform(self, pixelPad):
@@ -224,6 +236,9 @@ def _gen_axes_patch(self):
224236
"""
225237
return Circle((0.5, 0.5), 0.5)
226238

239+
def _gen_axes_spines(self):
240+
return {'hammer':mspines.Spine(self,'hammer',Circle((0.5, 0.5), 0.5))}
241+
227242
# Prevent the user from applying scales to one or both of the
228243
# axes. In this particular case, scaling the axes wouldn't make
229244
# sense, so we don't allow it.
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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()

examples/tests/backend_driver.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@
173173
'simple_plot.py',
174174
'simplification_clipping_test.py',
175175
'specgram_demo.py',
176+
'spine_placement_demo.py',
176177
'spy_demos.py',
177178
'stem_plot.py',
178179
'step_demo.py',

0 commit comments

Comments
 (0)
0