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

Skip to content

Commit 102cc69

Browse files
committed
support for shared axes
svn path=/trunk/matplotlib/; revision=892
1 parent d9c909d commit 102cc69

4 files changed

Lines changed: 68 additions & 57 deletions

File tree

examples/shared_axis_demo.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
If you want to turn off the ticklabels for a given axes (eg on
2020
subplot(211) or subplot(212), you cannot do the standard trick
2121
22-
set(ax2, =[])
22+
set(ax2, xticklabels=[])
2323
2424
because this changes the tick Formatter, which is shared among all
2525
axes. But you can alter the visibility of the labels, which is a
@@ -37,12 +37,11 @@
3737
s3 = sin(4*pi*t)
3838
ax1 = subplot(311)
3939
plot(t,s1)
40-
plot(t, s2)
4140
set( ax1.get_xticklabels(), fontsize=6)
4241

4342
## share x only
4443
ax2 = subplot(312, sharex=ax1)
45-
44+
plot(t, s2)
4645
# make these tick labels invisible
4746
set( ax2.get_xticklabels(), visible=False)
4847

lib/matplotlib/axes.py

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from artist import Artist
1313
from axis import XAxis, YAxis
1414
from cbook import iterable, is_string_like, flatten, enumerate, \
15-
allequal, dict_delall, strip_math, popd, silent_list
15+
allequal, dict_delall, strip_math, popd, popall, silent_list
1616
from collections import RegularPolyCollection, PolyCollection, LineCollection
1717
from colors import colorConverter, normalize, Colormap, LinearSegmentedColormap
1818
import cm
@@ -319,11 +319,15 @@ def __init__(self, fig, rect,
319319
Artist.__init__(self)
320320
self._position = map(makeValue, rect)
321321
# must be set before set_figure
322-
self._sharex = sharex # I use sharex's
322+
self._sharex = sharex
323323
self._sharey = sharey
324324

325-
326325
self.set_figure(fig)
326+
327+
self.xaxis = XAxis(self)
328+
self.yaxis = YAxis(self)
329+
330+
327331

328332
if axisbg is None: axisbg = rcParams['axes.facecolor']
329333
self._axisbg = axisbg
@@ -600,18 +604,16 @@ def _set_artist_props(self, a):
600604
def cla(self):
601605
'Clear the current axes'
602606

603-
604-
# TODO: don't recreate x and y axis instances on cla, simply
605-
# clear them
606-
self.xaxis = XAxis(self)
607-
self.yaxis = YAxis(self)
607+
self.xaxis.cla()
608+
self.yaxis.cla()
609+
608610

609611
if self._sharex is not None:
610-
self.xaxis._major = self._sharex.xaxis._major
611-
self.xaxis._minor = self._sharex.xaxis._minor
612+
self.xaxis.major = self._sharex.xaxis.major
613+
self.xaxis.minor = self._sharex.xaxis.minor
612614
if self._sharey is not None:
613-
self.yaxis._major = self._sharey.yaxis._major
614-
self.yaxis._minor = self._sharey.yaxis._minor
615+
self.yaxis.major = self._sharey.yaxis.major
616+
self.yaxis.minor = self._sharey.yaxis.minor
615617

616618
self._get_lines = _process_plot_var_args()
617619
self._get_patches_for_fill = _process_plot_var_args('fill')
@@ -3391,10 +3393,6 @@ def __init__(self, *args, **kwarg):
33913393
self.rgridlines = []
33923394
Axes.__init__(self, *args, **kwarg)
33933395

3394-
def _popall(self, seq):
3395-
'empty a list'
3396-
for i in range(len(seq)): seq.pop()
3397-
33983396
def _set_lim_and_transforms(self):
33993397
"""
34003398
set the dataLim and viewLim BBox attributes and the
@@ -3513,7 +3511,7 @@ def set_rgrids(self, radii, labels=None, angle=22.5, **kwargs):
35133511
ACCEPTS: sequence of floats
35143512
"""
35153513

3516-
self._popall(self.rgridlines)
3514+
popall(self.rgridlines)
35173515
theta = linspace(0,2*math.pi, self.RESOLUTION)
35183516
ls = rcParams['grid.linestyle']
35193517
color = rcParams['grid.color']
@@ -3526,7 +3524,7 @@ def set_rgrids(self, radii, labels=None, angle=22.5, **kwargs):
35263524
self.rgridlines.append(line)
35273525

35283526

3529-
self._popall(self.rgridlabels)
3527+
popall(self.rgridlabels)
35303528

35313529

35323530
color = rcParams['tick.color']
@@ -3572,7 +3570,7 @@ def set_thetagrids(self, angles, labels=None, fmt='%d', frac = 1.1,
35723570
35733571
ACCEPTS: sequence of floats
35743572
"""
3575-
self._popall(self.thetagridlines)
3573+
popall(self.thetagridlines)
35763574
ox, oy = 0,0
35773575
ls = rcParams['grid.linestyle']
35783576
color = rcParams['grid.color']
@@ -3585,7 +3583,7 @@ def set_thetagrids(self, angles, labels=None, fmt='%d', frac = 1.1,
35853583
line.set_transform(self.transData)
35863584
self.thetagridlines.append(line)
35873585

3588-
self._popall(self.thetagridlabels)
3586+
popall(self.thetagridlabels)
35893587

35903588
color = rcParams['tick.color']
35913589

lib/matplotlib/axis.py

Lines changed: 45 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
nonzero, take, Float, log10, logical_and
88

99
from artist import Artist
10-
from cbook import enumerate, silent_list
10+
from cbook import enumerate, silent_list, popall
1111
from lines import Line2D, TICKLEFT, TICKRIGHT, TICKUP, TICKDOWN
1212
from mlab import linspace
1313
from matplotlib import rcParams
@@ -441,9 +441,17 @@ def __init__(self, axes):
441441
self.set_figure(axes.figure)
442442

443443
self.axes = axes
444+
self.major = Ticker()
445+
self.minor = Ticker()
446+
self.label = self._get_label()
447+
self.majorTicks = []
448+
self.minorTicks = []
449+
444450

445-
self._major = Ticker()
446-
self._minor = Ticker()
451+
self.cla()
452+
453+
def cla(self):
454+
'clear the current axis'
447455
self.set_major_locator(AutoLocator())
448456
self.set_major_formatter(ScalarFormatter())
449457
self.set_minor_locator(NullLocator())
@@ -452,15 +460,18 @@ def __init__(self, axes):
452460
# whether the grids are on
453461
self._gridOnMajor = rcParams['axes.grid']
454462
self._gridOnMinor = False
455-
456-
self.label = self._get_label()
463+
464+
self.label.set_text('')
457465
self._set_artist_props(self.label)
458466

459467
# build a few default ticks; grow as necessary later; only
460468
# define 1 so properties set on ticks will be copied as they
461469
# grow
462-
self.majorTicks = [self._get_tick(major=True) for i in range(1)]
463-
self.minorTicks = [self._get_tick(major=False) for i in range(1)]
470+
471+
popall(self.majorTicks)
472+
popall(self.minorTicks)
473+
self.majorTicks.extend([self._get_tick(major=True) for i in range(1)])
474+
self.minorTicks.extend([self._get_tick(major=False) for i in range(1)])
464475

465476
def get_view_interval(self):
466477
'return the Interval instance for this axis view limits'
@@ -482,9 +493,9 @@ def draw(self, renderer, *args, **kwargs):
482493
ticklabelBoxes2 = []
483494

484495
majorTicks = self.get_major_ticks()
485-
majorLocs = self._major.locator()
486-
self._major.formatter.set_locs(majorLocs)
487-
majorLabels = [self._major.formatter(val, i) for i, val in enumerate(majorLocs)]
496+
majorLocs = self.major.locator()
497+
self.major.formatter.set_locs(majorLocs)
498+
majorLabels = [self.major.formatter(val, i) for i, val in enumerate(majorLocs)]
488499

489500

490501
seen = {}
@@ -506,9 +517,9 @@ def draw(self, renderer, *args, **kwargs):
506517
ticklabelBoxes2.append(extent)
507518

508519
minorTicks = self.get_minor_ticks()
509-
minorLocs = self._minor.locator()
510-
self._minor.formatter.set_locs(minorLocs)
511-
minorLabels = [self._minor.formatter(val, i) for i, val in enumerate(minorLocs)]
520+
minorLocs = self.minor.locator()
521+
self.minor.formatter.set_locs(minorLocs)
522+
minorLabels = [self.minor.formatter(val, i) for i, val in enumerate(minorLocs)]
512523

513524

514525
for tick, loc, label in zip(minorTicks, minorLocs, minorLabels):
@@ -575,7 +586,7 @@ def get_ticklines(self):
575586

576587
def get_ticklocs(self):
577588
"Get the tick locations in data coordinates as a Numeric array"
578-
return self._major.locator()
589+
return self.major.locator()
579590

580591
def _get_tick(self, major):
581592
'return the default tick intsance'
@@ -598,24 +609,24 @@ def _copy_tick_props(self, src, dest):
598609

599610
def get_major_locator(self):
600611
'Get the locator of the major ticker'
601-
return self._major.locator
612+
return self.major.locator
602613

603614
def get_minor_locator(self):
604615
'Get the locator of the minor ticker'
605-
return self._minor.locator
616+
return self.minor.locator
606617

607618
def get_major_formatter(self):
608619
'Get the formatter of the major ticker'
609-
return self._major.formatter
620+
return self.major.formatter
610621

611622
def get_minor_formatter(self):
612623
'Get the formatter of the minor ticker'
613-
return self._minor.formatter
624+
return self.minor.formatter
614625

615626
def get_major_ticks(self):
616627
'get the tick instances; grow as necessary'
617628

618-
numticks = len(self._major.locator())
629+
numticks = len(self.major.locator())
619630

620631
if len(self.majorTicks)<numticks:
621632
# update the new tick label properties from the old
@@ -633,7 +644,7 @@ def get_major_ticks(self):
633644

634645
def get_minor_ticks(self):
635646
'get the minor tick instances; grow as necessary'
636-
numticks = len(self._minor.locator())
647+
numticks = len(self.minor.locator())
637648
if len(self.minorTicks)<numticks:
638649
protoTick = self.minorTicks[0]
639650
for i in range(numticks-len(self.minorTicks)):
@@ -671,38 +682,38 @@ def set_major_formatter(self, formatter):
671682
Set the formatter of the major ticker
672683
673684
ACCEPTS: A Formatter instance"""
674-
self._major.formatter = formatter
675-
self._major.formatter.set_view_interval( self.get_view_interval() )
676-
self._major.formatter.set_data_interval( self.get_data_interval() )
685+
self.major.formatter = formatter
686+
self.major.formatter.set_view_interval( self.get_view_interval() )
687+
self.major.formatter.set_data_interval( self.get_data_interval() )
677688

678689
def set_minor_formatter(self, formatter):
679690
"""
680691
Set the formatter of the minor ticker
681692
682693
ACCEPTS: A Formatter instance"""
683-
self._minor.formatter = formatter
684-
self._minor.formatter.set_view_interval( self.get_view_interval() )
685-
self._minor.formatter.set_data_interval( self.get_data_interval() )
694+
self.minor.formatter = formatter
695+
self.minor.formatter.set_view_interval( self.get_view_interval() )
696+
self.minor.formatter.set_data_interval( self.get_data_interval() )
686697

687698

688699
def set_major_locator(self, locator):
689700
"""
690701
Set the locator of the major ticker
691702
692703
ACCEPTS: a Locator instance"""
693-
self._major.locator = locator
694-
self._major.locator.set_view_interval( self.get_view_interval() )
695-
self._major.locator.set_data_interval( self.get_data_interval() )
704+
self.major.locator = locator
705+
self.major.locator.set_view_interval( self.get_view_interval() )
706+
self.major.locator.set_data_interval( self.get_data_interval() )
696707

697708

698709
def set_minor_locator(self, locator):
699710
"""
700711
Set the locator of the minor ticker
701712
702713
ACCEPTS: a Locator instance"""
703-
self._minor.locator = locator
704-
self._minor.locator.set_view_interval( self.get_view_interval() )
705-
self._minor.locator.set_data_interval( self.get_data_interval() )
714+
self.minor.locator = locator
715+
self.minor.locator.set_view_interval( self.get_view_interval() )
716+
self.minor.locator.set_data_interval( self.get_data_interval() )
706717

707718
def set_ticklabels(self, ticklabels, *args, **kwargs):
708719
"""
@@ -740,11 +751,11 @@ def _update_label_postion(self, bboxes):
740751

741752
def pan(self, numsteps):
742753
'Pan numticks (can be positive or negative)'
743-
self._major.locator.pan(numsteps)
754+
self.major.locator.pan(numsteps)
744755

745756
def zoom(self, direction):
746757
"Zoom in/out on axis; if direction is >0 zoom in, else zoom out"
747-
self._major.locator.zoom(direction)
758+
self.major.locator.zoom(direction)
748759

749760
class XAxis(Axis):
750761
__name__ = 'xaxis'

lib/matplotlib/cbook.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,3 +528,6 @@ def remove(self, o):
528528
if thiso==o: continue
529529
else: self.push(thiso)
530530

531+
def popall(seq):
532+
'empty a list'
533+
for i in xrange(len(seq)): seq.pop()

0 commit comments

Comments
 (0)