@@ -373,6 +373,32 @@ def plotting():
373
373
"""
374
374
pass
375
375
376
+
377
+ def colorbar (tickfmt = '%1.1f' , cax = None , orientation = 'vertical' ):
378
+ """
379
+ colorbar(tickfmt='%1.1f', cax=None, orientation='vertical'):
380
+
381
+ Create a colorbar for mappable image
382
+
383
+ tickfmt is a format string to format the colorbar ticks
384
+
385
+ cax is a colorbar axes instance in which the colorbar will be
386
+ placed. If None, as default axesd will be created resizing the
387
+ current aqxes to make room for it. If not None, the supplied axes
388
+ will be used and the other axes positions will be unchanged.
389
+
390
+ orientation is the colorbar orientation: one of 'vertical' | 'horizontal'
391
+ return value is the colorbar axes instance
392
+ """
393
+ mappable = gci ()
394
+ if mappable is None :
395
+ raise RuntimeError ('No image is defined' )
396
+ fig = gcf ()
397
+
398
+ ret = fig .colorbar (mappable , tickfmt = '%1.1f' , cax = None , orientation = 'vertical' )
399
+ draw_if_interactive ()
400
+ return ret
401
+
376
402
def colormaps ():
377
403
"""
378
404
matplotlib provides the following colormaps.
@@ -622,84 +648,6 @@ def clf():
622
648
gcf ().clf ()
623
649
draw_if_interactive ()
624
650
625
- def colorbar (tickfmt = '%1.1f' , cax = None , orientation = 'vertical' ):
626
- """
627
- Create a colorbar for current mappable image (see gci)
628
-
629
- tickfmt is a format string to format the colorbar ticks
630
-
631
- cax is a colorbar axes instance in which the colorbar will be
632
- placed. If None, as default axesd will be created resizing the
633
- current aqxes to make room for it. If not None, the supplied axes
634
- will be used and the other axes positions will be unchanged.
635
-
636
- orientation is the colorbar orientation: one of 'vertical' | 'horizontal'
637
- return value is the colorbar axes instance
638
- """
639
-
640
- if orientation not in ('horizontal' , 'vertical' ):
641
- raise ValueError ('Orientation must be horizontal or vertical' )
642
-
643
- mappable = gci ()
644
- if mappable is None :
645
- raise RuntimeError ('First define a mappable image (eg imshow, figimage, pcolor, scatter' )
646
-
647
-
648
- if isinstance (mappable , image .FigureImage ):
649
- raise TypeError ('Colorbars for figure images currently not supported' )
650
-
651
-
652
- ax = gca ()
653
-
654
- cmap = mappable .cmap
655
- norm = mappable .norm
656
-
657
- if norm .vmin is None or norm .vmax is None :
658
- mappable .autoscale ()
659
- cmin = norm .vmin
660
- cmax = norm .vmax
661
-
662
- if cax is None :
663
- l ,b ,w ,h = ax .get_position ()
664
- if orientation == 'vertical' :
665
- neww = 0.8 * w
666
- ax .set_position ((l ,b ,neww ,h ))
667
- cax = axes ([l + 0.9 * w , b , 0.1 * w , h ])
668
- else :
669
- newh = 0.8 * h
670
- ax .set_position ((l ,b + 0.2 * h ,w ,newh ))
671
- cax = axes ([l , b , w , 0.1 * h ])
672
-
673
- else :
674
- if not isinstance (cax , Axes ):
675
- raise TypeError ('Expected an Axes instance for cax' )
676
-
677
- N = cmap .N
678
-
679
- c = linspace (cmin , cmax , N )
680
- C = array ([c ,c ])
681
-
682
- if orientation == 'vertical' :
683
- C = transpose (C )
684
-
685
- coll = cax .imshow (C ,
686
- interpolation = 'nearest' ,
687
- origin = 'lower' ,
688
- cmap = cmap , norm = norm ,
689
- extent = (0 , 1 , cmin , cmax ))
690
- mappable .add_observer (coll )
691
- mappable .set_colorbar (coll , cax )
692
- if orientation == 'vertical' :
693
- cax .set_xticks ([])
694
- cax .yaxis .tick_right ()
695
- cax .yaxis .set_major_formatter (FormatStrFormatter (tickfmt ))
696
- else :
697
- cax .set_yticks ([])
698
- cax .xaxis .set_major_formatter (FormatStrFormatter (tickfmt ))
699
-
700
- # restore the current axes
701
- axes (ax )
702
- return cax
703
651
704
652
705
653
0 commit comments