@@ -393,6 +393,7 @@ def __init__(self, ax, cmap=None,
393
393
_api .check_in_list (
394
394
['uniform' , 'proportional' ], spacing = spacing )
395
395
396
+ # wrap the axes so that it can be positioned as an inset axes:
396
397
ax = ColorbarAxes (ax )
397
398
self .ax = ax
398
399
# Bind some methods to the axes to warn users against using them.
@@ -477,87 +478,19 @@ def _extend_upper(self):
477
478
return self .extend in ('both' , 'max' )
478
479
479
480
def _long_axis (self ):
481
+ """Return the long axis"""
480
482
if self .orientation == 'vertical' :
481
483
return self .ax .yaxis
482
484
else :
483
485
return self .ax .xaxis
484
486
485
487
def _short_axis (self ):
488
+ """Return the short axis"""
486
489
if self .orientation == 'vertical' :
487
490
return self .ax .xaxis
488
491
else :
489
492
return self .ax .yaxis
490
493
491
- def _do_extends (self , extendlen ):
492
- """
493
- Make adjustments for the extend triangles (or rectanges)
494
- """
495
- # extend lengths are fraction of the *inner* part of colorbar,
496
- # not the total colorbar:
497
- el = extendlen [0 ] if self ._extend_lower () else 0
498
- eu = extendlen [1 ] if self ._extend_upper () else 0
499
- tot = eu + el + 1
500
- el = el / tot
501
- eu = eu / tot
502
- width = 1 / tot
503
-
504
- bounds = np .array ([0.0 , 0.0 , 1.0 , 1.0 ])
505
- bounds [1 ] = el
506
- bounds [3 ] = width
507
-
508
- # make the inner axes smaller to make room for the extend rectangle
509
- top = bounds [1 ] + bounds [3 ]
510
- if not self .extendrect :
511
- xyout = np .array ([[0 , bounds [1 ]], [0.5 , 0 ], [1 , bounds [1 ]],
512
- [1 , top ], [0.5 , 1 ], [0 , top ], [0 , bounds [1 ]]])
513
- else :
514
- xyout = np .array ([[0 , bounds [1 ]], [0 , 0 ], [1 , 0 ], [1 , bounds [1 ]],
515
- [1 , top ], [1 , 1 ], [0 , 1 ], [0 , top ],
516
- [0 , bounds [1 ]]])
517
- if self .orientation == 'horizontal' :
518
- bounds = bounds [[1 , 0 , 3 , 2 ]]
519
- xyout = xyout [:, ::- 1 ]
520
- self .ax ._set_inner_bounds (bounds )
521
-
522
- if not self .filled :
523
- return xyout
524
- # Make extend triangles or rectangles. These are defined in the
525
- # outer parent axes' co-ordinates:
526
- mappable = getattr (self , 'mappable' , None )
527
- if (isinstance (mappable , contour .ContourSet )
528
- and any (hatch is not None for hatch in mappable .hatches )):
529
- hatches = mappable .hatches
530
- else :
531
- hatches = [None ]
532
- if self ._extend_lower :
533
-
534
- if not self .extendrect :
535
- xy = np .array ([[0.5 , 0 ], [1 , el ], [0 , el ]])
536
- else :
537
- xy = np .array ([[0 , 0 ], [1. , 0 ], [1 , el ], [0 , el ]])
538
- if self .orientation == 'horizontal' :
539
- xy = xy [:, ::- 1 ]
540
- color = self .cmap (self .norm (self ._values [0 ]))
541
- patch = mpatches .PathPatch (
542
- mpath .Path (xy ), facecolor = color , linewidth = 0 ,
543
- antialiased = False , transform = self .ax .parent_ax .transAxes ,
544
- hatch = hatches [0 ])
545
- self .ax .parent_ax .add_patch (patch )
546
- if self ._extend_upper :
547
- if not self .extendrect :
548
- xy = np .array ([[0.5 , 1 ], [1 , 1 - eu ], [0 , 1 - eu ]])
549
- else :
550
- xy = np .array ([[0 , 1 ], [1 , 1 ], [1 , 1 - eu ], [0 , 1 - eu ]])
551
- if self .orientation == 'horizontal' :
552
- xy = xy [:, ::- 1 ]
553
- color = self .cmap (self .norm (self ._values [- 1 ]))
554
- patch = mpatches .PathPatch (
555
- mpath .Path (xy ), facecolor = color ,
556
- linewidth = 0 , antialiased = False ,
557
- transform = self .ax .parent_ax .transAxes , hatch = hatches [- 1 ])
558
- self .ax .parent_ax .add_patch (patch )
559
- return xyout
560
-
561
494
def draw_all (self ):
562
495
"""
563
496
Calculate any free parameters based on the current cmap and norm,
@@ -615,48 +548,45 @@ def _get_ticker_locator_formatter(self):
615
548
"""
616
549
Return the ``locator`` and ``formatter`` of the colorbar.
617
550
618
- If they have not been defined (i.e. are *None*), suitable formatter
619
- and locator instances will be created, attached to the respective
620
- attributes and returned.
551
+ If they have not been defined (i.e. are *None*), the formatter and
552
+ locator are retrieved from the axis, or from the value of the
553
+ boundaries for a boundary norm.
554
+
555
+ Called by update_ticks...
621
556
"""
622
557
locator = self .locator
623
558
formatter = self .formatter
624
559
minorlocator = self .minorlocator
625
- if locator is None :
626
- if (self .boundaries is None and
627
- not isinstance ( self . norm , colors . BoundaryNorm )) :
628
- if locator is None :
629
- locator = self . _long_axis (). get_major_locator ()
630
- if minorlocator is None :
631
- minorlocator = self ._long_axis (). get_minor_locator ()
632
-
633
- if isinstance (self .norm , colors . NoNorm ):
634
- nv = len ( self . _values )
635
- base = 1 + int ( nv / 10 )
636
- locator = ticker . IndexLocator ( base = base , offset = 0 )
637
- elif isinstance ( self .norm , colors . BoundaryNorm ):
638
- b = self . norm . boundaries
560
+ if ( self . boundaries is None and
561
+ not isinstance (self .norm , colors . BoundaryNorm )):
562
+ if locator is None :
563
+ locator = self . _long_axis (). get_major_locator ()
564
+ if minorlocator is None :
565
+ minorlocator = self . _long_axis (). get_minor_locator ()
566
+ if isinstance ( self .norm , colors . NoNorm ):
567
+ # default locator:
568
+ nv = len (self ._values )
569
+ base = 1 + int ( nv / 10 )
570
+ locator = ticker . IndexLocator ( base = base , offset = 0 )
571
+ elif isinstance ( self . norm , colors . BoundaryNorm ):
572
+ b = self .norm . boundaries
573
+ if locator is None :
639
574
locator = ticker .FixedLocator (b , nbins = 10 )
640
- else :
641
- b = self ._boundaries [self ._inside ]
575
+ else :
576
+ b = self ._boundaries [self ._inside ]
577
+ if locator is None :
642
578
locator = ticker .FixedLocator (b , nbins = 10 )
643
579
580
+ if minorlocator is None :
581
+ minorlocator = ticker .NullLocator ()
582
+
644
583
if formatter is None :
645
- if isinstance (self .norm , colors .LogNorm ):
646
- formatter = ticker .LogFormatterSciNotation ()
647
- elif isinstance (self .norm , colors .SymLogNorm ):
648
- formatter = ticker .LogFormatterSciNotation (
649
- linthresh = self .norm .linthresh )
650
- else :
651
- formatter = ticker .ScalarFormatter ()
652
- else :
653
- formatter = self .formatter
584
+ formatter = self ._long_axis ().get_major_formatter ()
654
585
655
586
self .locator = locator
656
587
self .formatter = formatter
657
588
self .minorlocator = minorlocator
658
589
_log .debug ('locator: %r' , locator )
659
- return locator , formatter
660
590
661
591
def _use_auto_colorbar_locator (self ):
662
592
"""
@@ -699,18 +629,22 @@ def update_ticks(self):
699
629
"""
700
630
ax = self .ax
701
631
# Get the locator and formatter; defaults to self.locator if not None.
702
- locator , formatter = self ._get_ticker_locator_formatter ()
703
- long_axis = ax . yaxis if self . orientation == 'vertical' else ax . xaxis
632
+ self ._get_ticker_locator_formatter ()
633
+
704
634
if self ._use_auto_colorbar_locator ():
705
- _log .debug ('Using auto colorbar locator %r on colorbar' , locator )
706
- long_axis .set_major_locator (locator )
707
- long_axis .set_major_formatter (formatter )
635
+ _log .debug ('Using auto colorbar locator %r on colorbar' ,
636
+ self .locator )
637
+ self ._long_axis ().set_major_locator (self .locator )
638
+ self ._long_axis ().set_minor_locator (self .minorlocator )
639
+ self ._long_axis ().set_major_formatter (self .formatter )
708
640
else :
709
641
_log .debug ('Using fixed locator on colorbar' )
710
- ticks , ticklabels , offset_string = self ._ticker (locator , formatter )
711
- long_axis .set_ticks (ticks )
712
- long_axis .set_ticklabels (ticklabels )
713
- long_axis .get_major_formatter ().set_offset_string (offset_string )
642
+ ticks , ticklabels , offset_string = self ._ticker (self .locator ,
643
+ self .formatter )
644
+ self ._long_axis ().set_ticks (ticks )
645
+ self ._long_axis ().set_ticklabels (ticklabels )
646
+ fmt = self ._long_axis ().get_major_formatter ()
647
+ fmt .set_offset_string (offset_string )
714
648
715
649
def set_ticks (self , ticks , update_ticks = True ):
716
650
"""
@@ -766,16 +700,16 @@ def set_ticklabels(self, ticklabels, update_ticks=True):
766
700
767
701
def minorticks_on (self ):
768
702
"""
769
- Turn the minor ticks of the colorbar on without extruding
770
- into the "extend regions".
703
+ Turn the minor ticks of the colorbar.
771
704
"""
772
705
self .ax .minorticks_on ()
706
+ self .minorlocator = self ._long_axis ().get_minor_locator ()
773
707
self ._short_axis ().set_minor_locator (ticker .NullLocator ())
774
708
775
709
def minorticks_off (self ):
776
710
"""Turn the minor ticks of the colorbar off."""
777
-
778
- self ._long_axis ().set_minor_locator (ticker . NullLocator () )
711
+ self . minorlocator = ticker . NullLocator ()
712
+ self ._long_axis ().set_minor_locator (self . minorlocator )
779
713
780
714
def set_label (self , label , * , loc = None , ** kwargs ):
781
715
"""
@@ -847,6 +781,76 @@ def _add_solids_patches(self, X, Y, C, mappable):
847
781
patches .append (patch )
848
782
self .solids_patches = patches
849
783
784
+ def _do_extends (self , extendlen ):
785
+ """
786
+ Make adjustments for the extend triangles (or rectanges)
787
+ """
788
+ # extend lengths are fraction of the *inner* part of colorbar,
789
+ # not the total colorbar:
790
+ el = extendlen [0 ] if self ._extend_lower () else 0
791
+ eu = extendlen [1 ] if self ._extend_upper () else 0
792
+ tot = eu + el + 1
793
+ el = el / tot
794
+ eu = eu / tot
795
+ width = 1 / tot
796
+
797
+ bounds = np .array ([0.0 , 0.0 , 1.0 , 1.0 ])
798
+ bounds [1 ] = el
799
+ bounds [3 ] = width
800
+
801
+ # make the inner axes smaller to make room for the extend rectangle
802
+ top = bounds [1 ] + bounds [3 ]
803
+ if not self .extendrect :
804
+ xyout = np .array ([[0 , bounds [1 ]], [0.5 , 0 ], [1 , bounds [1 ]],
805
+ [1 , top ], [0.5 , 1 ], [0 , top ], [0 , bounds [1 ]]])
806
+ else :
807
+ xyout = np .array ([[0 , bounds [1 ]], [0 , 0 ], [1 , 0 ], [1 , bounds [1 ]],
808
+ [1 , top ], [1 , 1 ], [0 , 1 ], [0 , top ],
809
+ [0 , bounds [1 ]]])
810
+ if self .orientation == 'horizontal' :
811
+ bounds = bounds [[1 , 0 , 3 , 2 ]]
812
+ xyout = xyout [:, ::- 1 ]
813
+ self .ax ._set_inner_bounds (bounds )
814
+
815
+ if not self .filled :
816
+ return xyout
817
+ # Make extend triangles or rectangles. These are defined in the
818
+ # outer parent axes' co-ordinates:
819
+ mappable = getattr (self , 'mappable' , None )
820
+ if (isinstance (mappable , contour .ContourSet )
821
+ and any (hatch is not None for hatch in mappable .hatches )):
822
+ hatches = mappable .hatches
823
+ else :
824
+ hatches = [None ]
825
+ if self ._extend_lower :
826
+
827
+ if not self .extendrect :
828
+ xy = np .array ([[0.5 , 0 ], [1 , el ], [0 , el ]])
829
+ else :
830
+ xy = np .array ([[0 , 0 ], [1. , 0 ], [1 , el ], [0 , el ]])
831
+ if self .orientation == 'horizontal' :
832
+ xy = xy [:, ::- 1 ]
833
+ color = self .cmap (self .norm (self ._values [0 ]))
834
+ patch = mpatches .PathPatch (
835
+ mpath .Path (xy ), facecolor = color , linewidth = 0 ,
836
+ antialiased = False , transform = self .ax .parent_ax .transAxes ,
837
+ hatch = hatches [0 ])
838
+ self .ax .parent_ax .add_patch (patch )
839
+ if self ._extend_upper :
840
+ if not self .extendrect :
841
+ xy = np .array ([[0.5 , 1 ], [1 , 1 - eu ], [0 , 1 - eu ]])
842
+ else :
843
+ xy = np .array ([[0 , 1 ], [1 , 1 ], [1 , 1 - eu ], [0 , 1 - eu ]])
844
+ if self .orientation == 'horizontal' :
845
+ xy = xy [:, ::- 1 ]
846
+ color = self .cmap (self .norm (self ._values [- 1 ]))
847
+ patch = mpatches .PathPatch (
848
+ mpath .Path (xy ), facecolor = color ,
849
+ linewidth = 0 , antialiased = False ,
850
+ transform = self .ax .parent_ax .transAxes , hatch = hatches [- 1 ])
851
+ self .ax .parent_ax .add_patch (patch )
852
+ return xyout
853
+
850
854
def add_lines (self , levels , colors , linewidths , erase = True ):
851
855
"""
852
856
Draw lines on the colorbar.
0 commit comments