8000 moved colorbar to API · matplotlib/matplotlib@50632d2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 50632d2

Browse files
committed
moved colorbar to API
svn path=/trunk/matplotlib/; revision=1056
1 parent da410af commit 50632d2

File tree

3 files changed

+106
-80
lines changed

3 files changed

+106
-80
lines changed

CHANGELOG

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
New entries should be added at the top
22

3+
2005-03-09 Moved colorbar to figure.Figure to expose it for API developers
4+
- JDH
5+
36
2005-03-09 backend_cairo.py: implemented draw_markers() - SC
47

58
2005-03-09 cbook.py: only use enumerate() (the python version) if the builtin

lib/matplotlib/figure.py

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
from matplotlib import rcParams
99
from patches import Rectangle
1010
from text import Text, _process_text_args
11+
1112
from legend import Legend
1213
from transforms import Bbox, Value, Point, get_bbox_transform, unit_bbox
13-
from numerix import array, clip
14-
14+
from numerix import array, clip, transpose
15+
from mlab import linspace
16+
from ticker import FormatStrFormatter
1517

1618
class Figure(Artist):
1719

@@ -455,6 +457,79 @@ def savefig(self, *args, **kwargs):
455457
self.canvas.print_figure(*args, **kwargs)
456458

457459

460+
def colorbar(self, mappable, tickfmt='%1.1f', cax=None, orientation='vertical'):
461+
"""
462+
Create a colorbar for mappable image
463+
464+
tickfmt is a format string to format the colorbar ticks
465+
466+
cax is a colorbar axes instance in which the colorbar will be
467+
placed. If None, as default axesd will be created resizing the
468+
current aqxes to make room for it. If not None, the supplied axes
469+
will be used and the other axes positions will be unchanged.
470+
471+
orientation is the colorbar orientation: one of 'vertical' | 'horizontal'
472+
return value is the colorbar axes instance
473+
"""
474+
475+
if orientation not in ('horizontal', 'vertical'):
476+
raise ValueError('Orientation must be horizontal or vertical')
477+
478+
if isinstance(mappable, FigureImage) and cax is None:
479+
raise TypeError('Colorbars for figure images currently not supported unless you provide a colorbar axes in cax')
480+
481+
482+
ax = self.gca()
483+
484+
cmap = mappable.cmap
485+
norm = mappable.norm
486+
487+
if norm.vmin is None or norm.vmax is None:
488+
mappable.autoscale()
489+
cmin = norm.vmin
490+
cmax = norm.vmax
491+
492+
if cax is None:
493+
l,b,w,h = ax.get_position()
494+
if orientation=='vertical':
495+
neww = 0.8*w
496+
ax.set_position((l,b,neww,h))
497+
cax = self.add_axes([l + 0.9*w, b, 0.1*w, h])
498+
else:
499+
newh = 0.8*h
500+
ax.set_position((l,b+0.2*h,w,newh))
501+
cax = self.add_axes([l, b, w, 0.1*h])
502+
503+
else:
504+
if not isinstance(cax, Axes):
505+
raise TypeError('Expected an Axes instance for cax')
506+
507+
N = cmap.N
508+
509+
c = linspace(cmin, cmax, N)
510+
C = array([c,c])
511+
512+
if orientation=='vertical':
513+
C = transpose(C)
514+
515+
coll = cax.imshow(C,
516+
interpolation='nearest',
517+
origin='lower',
518+
cmap=cmap, norm=norm,
519+
extent=(0, 1, cmin, cmax))
520+
mappable.add_observer(coll)
521+
mappable.set_colorbar(coll, cax)
522+
if orientation=='vertical':
523+
cax.set_xticks([])
524+
cax.yaxis.tick_right()
525+
cax.yaxis.set_major_formatter(FormatStrFormatter(tickfmt))
526+
else:
527+
cax.set_yticks([])
528+
cax.xaxis.set_major_formatter(FormatStrFormatter(tickfmt))
529+
530+
return cax
531+
532+
458533
def figaspect(arr):
459534
"""
460535
Determine the width and height for a figure that would fit array

lib/matplotlib/pylab.py

Lines changed: 26 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,32 @@ def plotting():
373373
"""
374374
pass
375375

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+
376402
def colormaps():
377403
"""
378404
matplotlib provides the following colormaps.
@@ -622,84 +648,6 @@ def clf():
622648
gcf().clf()
623649
draw_if_interactive()
624650

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
703651

704652

705653

0 commit comments

Comments
 (0)
0