28
28
29
29
GRIDLINE_INTERPOLATION_STEPS = 180
30
30
31
+ # This list is being used for compatibility with Axes.grid, which
32
+ # allows all Line2D kwargs.
33
+ _line_AI = artist .ArtistInspector (mlines .Line2D )
34
+ _line_param_names = _line_AI .get_setters ()
35
+ _line_param_aliases = [list (d .keys ())[0 ] for d in _line_AI .aliasd .values ()]
36
+ _gridline_param_names = ['grid_' + name
37
+ for name in _line_param_names + _line_param_aliases ]
38
+
31
39
32
40
class Tick (artist .Artist ):
33
41
"""
@@ -86,6 +94,11 @@ def __init__(self, axes, loc, label,
86
94
label2On = False ,
87
95
major = True ,
88
96
labelrotation = 0 ,
97
+ grid_color = None ,
98
+ grid_linestyle = None ,
99
+ grid_linewidth = None ,
100
+ grid_alpha = None ,
101
+ ** kw # Other Line2D kwargs applied to gridlines.
89
102
):
90
103
"""
91
104
bbox is the Bound2D bounding box in display coords of the Axes
@@ -153,6 +166,17 @@ def __init__(self, axes, loc, label,
153
166
zorder = mlines .Line2D .zorder
154
167
self ._zorder = zorder
155
168
169
+ self ._grid_color = (rcParams ['grid.color' ]
170
+ if grid_color is None else grid_color )
171
+ self ._grid_linestyle = (rcParams ['grid.linestyle' ]
172
+ if grid_linestyle is None else grid_linestyle )
173
+ self ._grid_linewidth = (rcParams ['grid.linewidth' ]
174
+ if grid_linewidth is None else grid_linewidth )
175
+ self ._grid_alpha = (rcParams ['grid.alpha' ]
176
+ if grid_alpha is None else grid_alpha )
177
+
178
+ self ._grid_kw = {k [5 :]: v for k , v in kw .items ()}
179
+
156
180
self .apply_tickdir (tickdir )
157
181
158
182
self .tick1line = self ._get_tick1line ()
@@ -368,6 +392,14 @@ def _apply_params(self, **kw):
368
392
v = getattr (self .label1 , 'get_' + k )()
369
393
setattr (self , '_label' + k , v )
370
394
395
+ grid_list = [k for k in six .iteritems (kw )
396
+ if k [0 ] in _gridline_param_names ]
397
+ if grid_list :
398
+ grid_kw = {k [5 :]: v for k , v in grid_list }
399
+ self .gridline .set (** grid_kw )
400
+ for k , v in six .iteritems (grid_kw ):
401
+ setattr (self , '_grid_' + k , v )
402
+
371
403
def update_position (self , loc ):
372
404
'Set the location of tick in data coords with scalar *loc*'
373
405
raise NotImplementedError ('Derived must override' )
@@ -469,11 +501,12 @@ def _get_gridline(self):
469
501
'Get the default line2D instance'
470
502
# x in data coords, y in axes coords
471
503
l = mlines .Line2D (xdata = (0.0 , 0.0 ), ydata = (0 , 1.0 ),
472
- color = rcParams ['grid.color' ],
473
- linestyle = rcParams ['grid.linestyle' ],
474
- linewidth = rcParams ['grid.linewidth' ],
475
- alpha = rcParams ['grid.alpha' ],
476
- markersize = 0 )
504
+ color = self ._grid_color ,
505
+ linestyle = self ._grid_linestyle ,
506
+ linewidth = self ._grid_linewidth ,
507
+ alpha = self ._grid_alpha ,
508
+ markersize = 0 ,
509
+ ** self ._grid_kw )
477
510
l .set_transform (self .axes .get_xaxis_transform (which = 'grid' ))
478
511
l .get_path ()._interpolation_steps = GRIDLINE_INTERPOLATION_STEPS
479
512
self ._set_artist_props (l )
@@ -592,12 +625,12 @@ def _get_gridline(self):
592
625
'Get the default line2D instance'
593
626
# x in axes coords, y in data coords
594
627
l = mlines .Line2D (xdata = (0 , 1 ), ydata = (0 , 0 ),
595
- color = rcParams [ 'grid.color' ] ,
596
- linestyle = rcParams [ 'grid.linestyle' ] ,
597
- linewidth = rcParams [ 'grid.linewidth' ] ,
598
- alpha = rcParams [ 'grid.alpha' ] ,
599
- markersize = 0 )
600
-
628
+ color = self . _grid_color ,
629
+ linestyle = self . _grid_linestyle ,
630
+ linewidth = self . _grid_linewidth ,
631
+ alpha = self . _grid_alpha ,
632
+ markersize = 0 ,
633
+ ** self . _grid_kw )
601
634
l .set_transform (self .axes .get_yaxis_transform (which = 'grid' ))
602
635
l .get_path ()._interpolation_steps = GRIDLINE_INTERPOLATION_STEPS
603
636
self ._set_artist_props (l )
@@ -650,13 +683,6 @@ def __init__(self, axes, pickradius=15):
650
683
artist .Artist .__init__ (self )
651
684
self .set_figure (axes .figure )
652
685
653
- # Keep track of setting to the default value, this allows use to know
654
- # if any of the following values is explicitly set by the user, so as
655
- # to not overwrite their settings with any of our 'auto' settings.
656
- self .isDefault_majloc = True
657
- self .isDefault_minloc = True
658
- self .isDefault_majfmt = True
659
- self .isDefault_minfmt = True
660
686
self .isDefault_label = True
661
687
662
688
self .axes = axes
@@ -745,22 +771,10 @@ def get_children(self):
745
771
746
772
def cla (self ):
747
773
'clear the current axis'
748
- self .set_major_locator (mticker .AutoLocator ())
749
- self .set_major_formatter (mticker .ScalarFormatter ())
750
- self .set_minor_locator (mticker .NullLocator ())
751
- self .set_minor_formatter (mticker .NullFormatter ())
752
774
753
- self .set_label_text ('' )
754
- self ._set_artist_props (self .label )
775
+ self .label .set_text ('' ) # self.set_label_text would change isDefault_
755
776
756
- # Keep track of setting to the default value, this allows use to know
757
- # if any of the following values is explicitly set by the user, so as
758
- # to not overwrite their settings with any of our 'auto' settings.
759
- self .isDefault_majloc = True
760
- self .isDefault_minloc = True
761
- self .isDefault_majfmt = True
762
- self .isDefault_minfmt = True
763
- self .isDefault_label = True
777
+ self ._set_scale ('linear' )
764
778
765
779
# Clear the callback registry for this axis, or it may "leak"
766
780
self .callbacks = cbook .CallbackRegistry ()
@@ -771,9 +785,6 @@ def cla(self):
771
785
self ._gridOnMinor = (rcParams ['axes.grid' ] and
772
786
rcParams ['axes.grid.which' ] in ('both' , 'minor' ))
773
787
774
- self .label .set_text ('' )
775
- self ._set_artist_props (self .label )
776
-
777
788
self .reset_ticks ()
778
789
779
790
self .converter = None
@@ -782,9 +793,11 @@ def cla(self):
782
793
self .stale = True
783
794
784
795
def reset_ticks (self ):
785
- # build a few default ticks; grow as necessary later; only
786
- # define 1 so properties set on ticks will be copied as they
787
- # grow
796
+ """
797
+ Re-initialize the major and minor Tick lists.
798
+
799
+ Each list starts with a single fresh Tick.
800
+ """
788
801
del self .majorTicks [:]
789
802
del self .minorTicks [:]
790
803
@@ -793,6 +806,11 @@ def reset_ticks(self):
793
806
self ._lastNumMajorTicks = 1
794
807
self ._lastNumMinorTicks = 1
795
808
809
+ try :
810
+ self .set_clip_path (self .axes .patch )
811
+ except AttributeError :
812
+ pass
813
+
796
814
def set_tick_params (self , which = 'major' , reset = False , ** kw ):
797
815
"""
798
816
Set appearance parameters for ticks and ticklabels.
@@ -810,6 +828,7 @@ def set_tick_params(self, which='major', reset=False, **kw):
810
828
if reset :
811
829
d .clear ()
812
830
d .update (kwtrans )
831
+
813
832
if reset :
814
833
self .reset_ticks ()
815
834
else :
@@ -833,7 +852,8 @@ def _translate_tick_kw(kw, to_init_kw=True):
833
852
kwkeys1 = ['length' , 'direction' , 'left' , 'bottom' , 'right' , 'top' ,
834
853
'labelleft' , 'labelbottom' , 'labelright' , 'labeltop' ,
835
854
'labelrotation' ]
836
- kwkeys = kwkeys0 + kwkeys1
855
+ kwkeys2 = _gridline_param_names
856
+ kwkeys = kwkeys0 + kwkeys1 + kwkeys2
837
857
kwtrans = dict ()
838
858
if to_init_kw :
839
859
if 'length' in kw :
@@ -975,7 +995,7 @@ def _update_ticks(self, renderer):
975
995
"""
976
996
977
997
interval = self .get_view_interval ()
978
- tick_tups = list (self .iter_ticks ())
998
+ tick_tups = list (self .iter_ticks ()) # iter_ticks calls the locator
979
999
if self ._smart_bounds and tick_tups :
980
1000
# handle inverted limits
981
1001
view_low , view_high = sorted (interval )
@@ -1401,30 +1421,21 @@ def grid(self, b=None, which='major', **kwargs):
1401
1421
if len (kwargs ):
1402
1422
b = True
1403
1423
which = which .lower ()
1424
+ gridkw = {'grid_' + item [0 ]: item [1 ] for item in kwargs .items ()}
1404
1425
if which in ['minor' , 'both' ]:
1405
1426
if b is None :
1406
1427
self ._gridOnMinor = not self ._gridOnMinor
1407
1428
else :
1408
1429
self ._gridOnMinor = b
1409
- for tick in self .minorTicks : # don't use get_ticks here!
1410
- if tick is None :
1411
- continue
1412
- tick .gridOn = self ._gridOnMinor
1413
- if len (kwargs ):
1414
- tick .gridline .update (kwargs )
1415
- self ._minor_tick_kw ['gridOn' ] = self ._gridOnMinor
1430
+ self .set_tick_params (which = 'minor' , gridOn = self ._gridOnMinor ,
1431
+ ** gridkw )
1416
1432
if which in ['major' , 'both' ]:
1417
1433
if b is None :
1418
1434
self ._gridOnMajor = not self ._gridOnMajor
1419
1435
else :
1420
1436
self ._gridOnMajor = b
1421
- for tick in self .majorTicks : # don't use get_ticks here!
1422
- if tick is None :
1423
- continue
1424
- tick .gridOn = self ._gridOnMajor
1425
- if len (kwargs ):
1426
- tick .gridline .update (kwargs )
1427
- self ._major_tick_kw ['gridOn' ] = self ._gridOnMajor
1437
+ self .set_tick_params (which = 'major' , gridOn = self ._gridOnMajor ,
1438
+ ** gridkw )
1428
1439
self .stale = True
1429
1440
1430
1441
def update_units (self , data ):
@@ -1454,11 +1465,11 @@ def _update_axisinfo(self):
1454
1465
check the axis converter for the stored units to see if the
1455
1466
axis info needs to be updated
1456
1467
"""
1457
-
1458
1468
if self .converter is None :
1459
1469
return
1460
1470
1461
1471
info = self .converter .axisinfo (self .units , self )
1472
+
1462
1473
if info is None :
1463
1474
return
1464
1475
if info .majloc is not None and \
0 commit comments