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

Skip to content

Commit 5448a1f

Browse files
committed
added support for foreign sharex and sharey and removed custom Axes callback handling
svn path=/trunk/matplotlib/; revision=3404
1 parent 6355160 commit 5448a1f

File tree

3 files changed

+89
-28
lines changed

3 files changed

+89
-28
lines changed

API_CHANGES

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
Made the emit property of set_xlim and set_ylim true by default;
2+
removed the Axes custom callback handling into a 'callbacks'
3+
attribute which is a cbook.CallbackRegistry instance. This now
4+
supports the xlim_changed and ylim_changed Axes events.
5+
16
0.90.1 released
27

38
The file dviread.py has a (very limited and fragile) dvi reader

lib/matplotlib/axes.py

Lines changed: 83 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from axis import XAxis, YAxis
1818
from cbook import iterable, is_string_like, flatten, enumerate, \
1919
allequal, dict_delall, popd, popall, silent_list, is_numlike, dedent
20+
2021
from collections import RegularPolyCollection, PolyCollection, LineCollection, \
2122
QuadMesh, StarPolygonCollection, BrokenBarHCollection
2223
from colors import colorConverter, Normalize, Colormap, \
@@ -53,6 +54,8 @@
5354

5455
import matplotlib
5556

57+
from matplotlib import cbook
58+
5659
if matplotlib._havedate:
5760
from dates import AutoDateFormatter, AutoDateLocator, DateLocator, DateFormatter
5861

@@ -457,6 +460,13 @@ class Axes(Artist):
457460
"""
458461
The Axes contains most of the figure elements: Axis, Tick, Line2D,
459462
Text, Polygon etc, and sets the coordinate system
463+
464+
The Axes instance supports callbacks through a callbacks attribute
465+
which is a cbook.CallbackRegistry instance. The events you can
466+
connect to are 'xlim_changed' and 'ylim_changed' and the callback
467+
will be called with func(ax) where ax is the Axes instance
468+
469+
460470
"""
461471

462472
scaled = {IDENTITY : 'linear',
@@ -567,6 +577,69 @@ def _init_axis(self):
567577
self.yaxis = YAxis(self)
568578

569579

580+
def sharex_foreign(self, axforeign):
581+
"""
582+
You can share your x-axis view limits with another Axes in the
583+
same Figure by using the sharex and sharey property of the
584+
Axes. But this doesn't work for Axes in a different figure.
585+
This function sets of the callbacks so that when the xaxis of
586+
this Axes or the Axes in a foreign figure are changed, both
587+
will be synchronized.
588+
589+
The connection ids for the self.callbacks and
590+
axforeign.callbacks cbook.CallbackRegistry instances are
591+
returned in case you want to disconnect the coupling
592+
"""
593+
594+
def follow_foreign_xlim(ax):
595+
xmin, xmax = axforeign.get_xlim()
596+
# do not emit here or we'll get a ping png effect
597+
self.set_xlim(xmin, xmax, emit=False)
598+
self.figure.canvas.draw_idle()
599+
600+
def follow_self_xlim(ax):
601+
xmin, xmax = self.get_xlim()
602+
# do not emit here or we'll get a ping png effect
603+
axforeign.set_xlim(xmin, xmax, emit=False)
604+
axforeign.figure.canvas.draw_idle()
605+
606+
607+
cidForeign = axforeign.callbacks.connect('xlim_changed', follow_foreign_xlim)
608+
cidSelf = self.callbacks.connect('xlim_changed', follow_self_xlim)
609+
return cidSelf, cidForeign
610+
611+
612+
def sharey_foreign(self, axforeign):
613+
"""
614+
You can share your y-axis view limits with another Axes in the
615+
same Figure by using the sharey and sharey property of the
616+
Axes. But this doesn't work for Axes in a different figure.
617+
This function sets of the callbacks so that when the yaxis of
618+
this Axes or the Axes in a foreign figure are changed, both
619+
will be synchronized.
620+
621+
The connection ids for the self.callbacks and
622+
axforeign.callbacks cbook.CallbackRegistry instances are
623+
returned in case you want to disconnect the coupling
624+
"""
625+
626+
def follow_foreign_ylim(ax):
627+
ymin, ymax = axforeign.get_ylim()
628+
# do not emit here or we'll get a ping png effect
629+
self.set_ylim(ymin, ymax, emit=False)
630+
self.figure.canvas.draw_idle()
631+
632+
def follow_self_ylim(ax):
633+
ymin, ymax = self.get_ylim()
634+
# do not emit here or we'll get a ping png effect
635+
axforeign.set_ylim(ymin, ymax, emit=False)
636+
axforeign.figure.canvas.draw_idle()
637+
638+
639+
cidForeign = axforeign.callbacks.connect('ylim_changed', follow_foreign_ylim)
640+
cidSelf = self.callbacks.connect('ylim_changed', follow_self_ylim)
641+
return cidSelf, cidForeign
642+
570643
def set_figure(self, fig):
571644
"""
572645
Set the Axes figure
@@ -678,6 +751,8 @@ def cla(self):
678751
self.yaxis.cla()
679752

680753
self.dataLim.ignore(1)
754+
self.callbacks = cbook.CallbackRegistry(('xlim_changed', 'ylim_changed'))
755+
681756
if self._sharex is not None:
682757
self.xaxis.major = self._sharex.xaxis.major
683758
self.xaxis.minor = self._sharex.xaxis.minor
@@ -1456,7 +1531,7 @@ def get_xlim(self):
14561531
return self.viewLim.intervalx().get_bounds()
14571532

14581533

1459-
def set_xlim(self, xmin=None, xmax=None, emit=False, **kwargs):
1534+
def set_xlim(self, xmin=None, xmax=None, emit=True, **kwargs):
14601535
"""
14611536
set_xlim(self, *args, **kwargs):
14621537
@@ -1501,7 +1576,7 @@ def set_xlim(self, xmin=None, xmax=None, emit=False, **kwargs):
15011576

15021577
xmin, xmax = nonsingular(xmin, xmax, increasing=False)
15031578
self.viewLim.intervalx().set_bounds(xmin, xmax)
1504-
if emit: self._send_xlim_event()
1579+
if emit: self.callbacks.process('xlim_changed', self)
15051580

15061581
return xmin, xmax
15071582

@@ -1580,7 +1655,7 @@ def get_ylim(self):
15801655
'Get the y axis range [ymin, ymax]'
15811656
return self.viewLim.intervaly().get_bounds()
15821657

1583-
def set_ylim(self, ymin=None, ymax=None, emit=False, **kwargs):
1658+
def set_ylim(self, ymin=None, ymax=None, emit=True, **kwargs):
15841659
"""
15851660
set_ylim(self, *args, **kwargs):
15861661
@@ -1621,7 +1696,8 @@ def set_ylim(self, ymin=None, ymax=None, emit=False, **kwargs):
16211696

16221697
ymin, ymax = nonsingular(ymin, ymax, increasing=False)
16231698
self.viewLim.intervaly().set_bounds(ymin, ymax)
1624-
if emit: self._send_ylim_event()
1699+
if emit: self.callbacks.process('ylim_changed', self)
1700+
16251701
return ymin, ymax
16261702

16271703
def get_yscale(self):
@@ -1819,13 +1895,6 @@ def set_cursor_props(self, *args):
18191895
c =colorConverter.to_rgba(c)
18201896
self._cursorProps = lw, c
18211897

1822-
def _send_xlim_event(self):
1823-
for cid, func in self._connected.get('xlim_changed', []):
1824-
func(self)
1825-
1826-
def _send_ylim_event(self):
1827-
for cid, func in self._connected.get('ylim_changed', []):
1828-
func(self)
18291898

18301899
def panx(self, numsteps):
18311900
'Pan the x axis numsteps (plus pan right, minus pan left)'
@@ -1849,9 +1918,8 @@ def zoomy(self, numsteps):
18491918
self.yaxis.zoom(numsteps)
18501919
self._send_ylim_event()
18511920

1852-
_cid = 0
1853-
_events = ('xlim_changed', 'ylim_changed')
18541921

1922+
18551923
def connect(self, s, func):
18561924
"""
18571925
Register observers to be notified when certain events occur. Register
@@ -1868,23 +1936,11 @@ def connect(self, s, func):
18681936
disconnect to disconnect from the axes event
18691937
18701938
"""
1871-
1872-
if s not in Axes._events:
1873-
raise ValueError('You can only connect to the following axes events: %s' % ', '.join(Axes._events))
1874-
1875-
cid = Axes._cid
1876-
self._connected.setdefault(s, []).append((cid, func))
1877-
Axes._cid += 1
1878-
return cid
1939+
raise DeprecationWarning('use the callbacks CallbackRegistry instance instead')
18791940

18801941
def disconnect(self, cid):
18811942
'disconnect from the Axes event.'
1882-
for key, val in self._connected.items():
1883-
for item in val:
1884-
if item[0] == cid:
1885-
self._connected[key].remove(item)
1886-
return
1887-
1943+
raise DeprecationWarning('use the callbacks CallbackRegistry instance instead')
18881944
def get_children(self):
18891945
'return a list of child artists'
18901946
children = []

lib/matplotlib/cbook.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ def _check_signal(self, s):
123123
def connect(self, s, func):
124124
"""
125125
register func to be called when a signal s is generated
126-
func will be called with args and kwargs
126+
func will be called
127127
"""
128128
self._check_signal(s)
129129
self._cid +=1

0 commit comments

Comments
 (0)