From: <lee...@us...> - 2009-09-13 05:44:31
|
Revision: 7753 http://matplotlib.svn.sourceforge.net/matplotlib/?rev=7753&view=rev Author: leejjoon Date: 2009-09-13 05:44:21 +0000 (Sun, 13 Sep 2009) Log Message: ----------- AxesGrid: add modified version of colorbar Modified Paths: -------------- trunk/matplotlib/CHANGELOG trunk/matplotlib/doc/mpl_toolkits/axes_grid/index.rst trunk/matplotlib/doc/mpl_toolkits/axes_grid/users/axes_divider.rst trunk/matplotlib/doc/mpl_toolkits/axes_grid/users/axislines.rst trunk/matplotlib/doc/mpl_toolkits/axes_grid/users/overview.rst trunk/matplotlib/examples/axes_grid/demo_axes_grid.py trunk/matplotlib/lib/mpl_toolkits/axes_grid/axes_divider.py trunk/matplotlib/lib/mpl_toolkits/axes_grid/axes_grid.py Added Paths: ----------- trunk/matplotlib/doc/mpl_toolkits/axes_grid/figures/ trunk/matplotlib/doc/mpl_toolkits/axes_grid/figures/demo_colorbar_of_inset_axes.py trunk/matplotlib/doc/mpl_toolkits/axes_grid/figures/demo_colorbar_with_axes_divider.py trunk/matplotlib/doc/mpl_toolkits/axes_grid/figures/demo_colorbar_with_inset_locator.py trunk/matplotlib/doc/mpl_toolkits/axes_grid/figures/demo_new_colorbar.py trunk/matplotlib/examples/axes_grid/demo_axes_grid2.py trunk/matplotlib/examples/axes_grid/demo_colorbar_with_inset_locator.py trunk/matplotlib/lib/mpl_toolkits/axes_grid/colorbar.py Modified: trunk/matplotlib/CHANGELOG =================================================================== --- trunk/matplotlib/CHANGELOG 2009-09-13 05:42:01 UTC (rev 7752) +++ trunk/matplotlib/CHANGELOG 2009-09-13 05:44:21 UTC (rev 7753) @@ -1,3 +1,6 @@ +2009-09-13 AxesGrid : add modified version of colorbar. Add colorbar + location howto. - JJL + 2009-09-07 AxesGrid : implemented axisline style. Added a demo examples/axes_grid/demo_axisline_style.py- JJL Added: trunk/matplotlib/doc/mpl_toolkits/axes_grid/figures/demo_colorbar_of_inset_axes.py =================================================================== --- trunk/matplotlib/doc/mpl_toolkits/axes_grid/figures/demo_colorbar_of_inset_axes.py (rev 0) +++ trunk/matplotlib/doc/mpl_toolkits/axes_grid/figures/demo_colorbar_of_inset_axes.py 2009-09-13 05:44:21 UTC (rev 7753) @@ -0,0 +1,49 @@ +import matplotlib.pyplot as plt + +from mpl_toolkits.axes_grid.inset_locator import inset_axes, zoomed_inset_axes +from mpl_toolkits.axes_grid.colorbar import colorbar + +def get_demo_image(): + from matplotlib.cbook import get_sample_data + import numpy as np + f = get_sample_data("axes_grid/bivariate_normal.npy", asfileobj=False) + z = np.load(f) + # z is a numpy array of 15x15 + return z, (-3,4,-4,3) + + +fig = plt.figure(1, [5,4]) +ax = fig.add_subplot(111) + +Z, extent = get_demo_image() + +ax.set(aspect=1, + xlim=(-15, 15), + ylim=(-20, 5)) + + +axins = zoomed_inset_axes(ax, 2, loc=2) # zoom = 6 +im = axins.imshow(Z, extent=extent, interpolation="nearest", + origin="lower") + +plt.xticks(visible=False) +plt.yticks(visible=False) + + +# colorbar +cax = inset_axes(axins, + width="5%", # width = 10% of parent_bbox width + height="100%", # height : 50% + loc=3, + bbox_to_anchor=(1.05, 0., 1, 1), + bbox_transform=axins.transAxes, + borderpad=0, + ) + + +colorbar(im, cax=cax) #, ticks=[1,2,3]) + + +plt.draw() +plt.show() + Added: trunk/matplotlib/doc/mpl_toolkits/axes_grid/figures/demo_colorbar_with_axes_divider.py =================================================================== --- trunk/matplotlib/doc/mpl_toolkits/axes_grid/figures/demo_colorbar_with_axes_divider.py (rev 0) +++ trunk/matplotlib/doc/mpl_toolkits/axes_grid/figures/demo_colorbar_with_axes_divider.py 2009-09-13 05:44:21 UTC (rev 7753) @@ -0,0 +1,25 @@ +import matplotlib.pyplot as plt +from mpl_toolkits.axes_grid.axes_divider import make_axes_locatable + +from mpl_toolkits.axes_grid.colorbar import colorbar +# from matplotlib.pyplot import colorbar + +fig = plt.figure(1, figsize=(6, 3)) +fig.subplots_adjust(wspace=0.5) + +ax1 = fig.add_subplot(121) +im1 = ax1.imshow([[1,2],[3,4]]) + +ax1_divider = make_axes_locatable(ax1) +cax1 = ax1_divider.append_axes("right", size="7%", pad="2%") +cb1 = colorbar(im1, cax=cax1) + +ax2 = fig.add_subplot(122) +im2 = ax2.imshow([[1,2],[3,4]]) + +ax2_divider = make_axes_locatable(ax2) +cax2 = ax2_divider.append_axes("top", size="7%", pad="2%") +cb2 = colorbar(im2, cax=cax2, orientation="horizontal") +cax2.xaxis.set_ticks_position("top") +plt.show() + Added: trunk/matplotlib/doc/mpl_toolkits/axes_grid/figures/demo_colorbar_with_inset_locator.py =================================================================== --- trunk/matplotlib/doc/mpl_toolkits/axes_grid/figures/demo_colorbar_with_inset_locator.py (rev 0) +++ trunk/matplotlib/doc/mpl_toolkits/axes_grid/figures/demo_colorbar_with_inset_locator.py 2009-09-13 05:44:21 UTC (rev 7753) @@ -0,0 +1,40 @@ +import matplotlib.pyplot as plt + +from mpl_toolkits.axes_grid.inset_locator import inset_axes +from mpl_toolkits.axes_grid.colorbar import colorbar + +fig = plt.figure(1, [6, 3]) + +# first subplot +ax1 = fig.add_subplot(121) + +axins1 = inset_axes(ax1, + width="50%", # width = 10% of parent_bbox width + height="5%", # height : 50% + loc=1) + +im1=ax1.imshow([[1,2],[2, 3]]) +colorbar(im1, cax=axins1, orientation="horizontal", ticks=[1,2,3]) +axins1.xaxis.set_ticks_position("bottom") + +# first subplot +ax = fig.add_subplot(122) + +axins = inset_axes(ax, + width="5%", # width = 10% of parent_bbox width + height="50%", # height : 50% + loc=3, + bbox_to_anchor=(1.05, 0., 1, 1), + bbox_transform=ax.transAxes, + borderpad=0, + ) + +# Controlling the placement of the inset axes is basically same as that +# of the legend. you may want to play with the borderpad value and +# the bbox_to_anchor coordinate. + +im=ax.imshow([[1,2],[2, 3]]) +colorbar(im, cax=axins, ticks=[1,2,3]) + +plt.draw() +plt.show() Added: trunk/matplotlib/doc/mpl_toolkits/axes_grid/figures/demo_new_colorbar.py =================================================================== --- trunk/matplotlib/doc/mpl_toolkits/axes_grid/figures/demo_new_colorbar.py (rev 0) +++ trunk/matplotlib/doc/mpl_toolkits/axes_grid/figures/demo_new_colorbar.py 2009-09-13 05:44:21 UTC (rev 7753) @@ -0,0 +1,21 @@ +import matplotlib.pyplot as plt + +plt.rcParams["text.usetex"]=False + +fig = plt.figure(1, figsize=(6, 3)) + +ax1 = fig.add_subplot(121) +im1 = ax1.imshow([[1,2],[3,4]]) +cb1 = plt.colorbar(im1) +cb1.ax.set_yticks([1, 3]) +ax1.set_title("Original MPL's colorbar w/\nset_yticks([1,3])", size=10) + +from mpl_toolkits.axes_grid.colorbar import colorbar +ax2 = fig.add_subplot(122) +im2 = ax2.imshow([[1,2],[3,4]]) +cb2 = colorbar(im2) +cb2.ax.set_yticks([1, 3]) +ax2.set_title("AxesGrid's colorbar w/\nset_yticks([1,3])", size=10) + +plt.show() + Modified: trunk/matplotlib/doc/mpl_toolkits/axes_grid/index.rst =================================================================== --- trunk/matplotlib/doc/mpl_toolkits/axes_grid/index.rst 2009-09-13 05:42:01 UTC (rev 7752) +++ trunk/matplotlib/doc/mpl_toolkits/axes_grid/index.rst 2009-09-13 05:44:21 UTC (rev 7753) @@ -8,10 +8,12 @@ ease displaying multiple images in matplotlib. While the aspect parameter in matplotlib adjust the position of the single axes, AxesGrid toolkit provides a framework to adjust the position of -multiple axes according to their aspects. +multiple axes according to their aspects. +.. image:: ../../_static/demo_axes_grid.png + Documentation ============= @@ -19,4 +21,5 @@ :maxdepth: 2 users/index.rst + howtos/index.rst api/index.rst Modified: trunk/matplotlib/doc/mpl_toolkits/axes_grid/users/axes_divider.rst =================================================================== --- trunk/matplotlib/doc/mpl_toolkits/axes_grid/users/axes_divider.rst 2009-09-13 05:42:01 UTC (rev 7752) +++ trunk/matplotlib/doc/mpl_toolkits/axes_grid/users/axes_divider.rst 2009-09-13 05:44:21 UTC (rev 7753) @@ -81,12 +81,12 @@ See the example, -.. plot:: mpl_toolkits/axes_grid/figures/simple_axes_divider2.py +.. plot:: mpl_toolkits/axes_grid/examples/simple_axes_divider2.py :include-source: You can adjust the size of the each axes accroding to their x or y data limits (AxesX and AxesY), similar to the axes aspect parameter. -.. plot:: mpl_toolkits/axes_grid/figures/simple_axes_divider3.py +.. plot:: mpl_toolkits/axes_grid/examples/simple_axes_divider3.py :include-source: Modified: trunk/matplotlib/doc/mpl_toolkits/axes_grid/users/axislines.rst =================================================================== --- trunk/matplotlib/doc/mpl_toolkits/axes_grid/users/axislines.rst 2009-09-13 05:42:01 UTC (rev 7752) +++ trunk/matplotlib/doc/mpl_toolkits/axes_grid/users/axislines.rst 2009-09-13 05:44:21 UTC (rev 7753) @@ -29,7 +29,7 @@ * a curvelinear grid. * a floating axis -.. plot:: mpl_toolkits/axes_grid/figures/demo_floating_axis.py +.. plot:: mpl_toolkits/axes_grid/examples/demo_floating_axis.py *axes_grid.axislines.Axes* defines a *axis* attribute, which is a @@ -211,7 +211,7 @@ ax1.parasites.append(ax2) -.. plot:: mpl_toolkits/axes_grid/figures/demo_curvelinear_grid.py +.. plot:: mpl_toolkits/axes_grid/examples/demo_curvelinear_grid.py Modified: trunk/matplotlib/doc/mpl_toolkits/axes_grid/users/overview.rst =================================================================== --- trunk/matplotlib/doc/mpl_toolkits/axes_grid/users/overview.rst 2009-09-13 05:42:01 UTC (rev 7752) +++ trunk/matplotlib/doc/mpl_toolkits/axes_grid/users/overview.rst 2009-09-13 05:44:21 UTC (rev 7753) @@ -35,7 +35,7 @@ padding between them cannot be easily done in matplotlib. AxesGrid is used in such case. -.. plot:: mpl_toolkits/axes_grid/figures/simple_axesgrid.py +.. plot:: mpl_examples/axes_grid/simple_axesgrid.py :include-source: * The postion of each axes is determined at the drawing time (see @@ -49,7 +49,7 @@ height. The widths (height) of the axes in the same row (column) are scaled according to their view limits (xlim or ylim). - .. plot:: mpl_toolkits/axes_grid/figures/simple_axesgrid2.py + .. plot:: mpl_toolkits/axes_grid/examples/simple_axesgrid2.py :include-source: * xaxis are shared among axes in a same column. Similarly, yaxis are @@ -137,7 +137,7 @@ The examples below show what you can do with AxesGrid. -.. plot:: mpl_toolkits/axes_grid/figures/demo_axes_grid.py +.. plot:: mpl_toolkits/axes_grid/examples/demo_axes_grid.py RGB Axes @@ -158,7 +158,7 @@ origin="lower", interpolation="nearest") -.. plot:: mpl_toolkits/axes_grid/figures/simple_rgb.py +.. plot:: mpl_toolkits/axes_grid/examples/simple_rgb.py @@ -219,7 +219,7 @@ See the full source code below. -.. plot:: mpl_toolkits/axes_grid/figures/scatter_hist.py +.. plot:: mpl_toolkits/axes_grid/examples/scatter_hist.py The scatter_hist using the AxesDivider has some advantage over the @@ -246,7 +246,7 @@ Example 1. twinx ---------------- -.. plot:: mpl_toolkits/axes_grid/figures/parasite_simple.py +.. plot:: mpl_toolkits/axes_grid/examples/parasite_simple.py :include-source: Example 2. twin @@ -257,7 +257,7 @@ accordingly. -.. plot:: mpl_toolkits/axes_grid/figures/parasite_simple2.py +.. plot:: mpl_toolkits/axes_grid/examples/parasite_simple2.py @@ -290,13 +290,13 @@ ax.axis["top"].set_visible(False) -.. plot:: mpl_toolkits/axes_grid/figures/simple_axisline3.py +.. plot:: mpl_toolkits/axes_grid/examples/simple_axisline3.py SubplotZero gives you two more additional (floating?) axis of x=0 and y=0 (in data coordinate) -.. plot:: mpl_toolkits/axes_grid/figures/simple_axisline2.py +.. plot:: mpl_toolkits/axes_grid/examples/simple_axisline2.py :include-source: @@ -315,7 +315,7 @@ r"$\pi$", r"$\frac{3}{2}\pi$", r"$2\pi$"]) -.. plot:: mpl_toolkits/axes_grid/figures/simple_axisline4.py +.. plot:: mpl_toolkits/axes_grid/examples/simple_axisline4.py AxisLine Axes lets you create a custom axis, :: @@ -330,7 +330,7 @@ And, you can use it with parasiteAxes. -.. plot:: mpl_toolkits/axes_grid/figures/demo_parasite_axes2.py +.. plot:: mpl_toolkits/axes_grid/examples/demo_parasite_axes2.py AnchoredArtists @@ -343,7 +343,7 @@ in the example below will have width and height in the data coordinate. -.. plot:: mpl_toolkits/axes_grid/figures/simple_anchored_artists.py +.. plot:: mpl_toolkits/axes_grid/examples/simple_anchored_artists.py :include-source: @@ -377,7 +377,7 @@ creates an inset axes whose data scale is half of the parent axes. Here is complete examples. -.. plot:: mpl_toolkits/axes_grid/figures/inset_locator_demo.py +.. plot:: mpl_toolkits/axes_grid/examples/inset_locator_demo.py For example, :func:`zoomed_inset_axes` can be used when you want the inset represents the zoom-up of the small portion in the parent axes. @@ -385,7 +385,7 @@ function :func:`mark_inset` to mark the location of the area represented by the inset axes. -.. plot:: mpl_toolkits/axes_grid/figures/inset_locator_demo2.py +.. plot:: mpl_toolkits/axes_grid/examples/inset_locator_demo2.py :include-source: @@ -395,6 +395,6 @@ You can draw a cuvelinear grid and ticks. Also a floating axis can be created. See :ref:`axislines-manual` for more details. -.. plot:: mpl_toolkits/axes_grid/figures/demo_floating_axis.py +.. plot:: mpl_toolkits/axes_grid/examples/demo_floating_axis.py Modified: trunk/matplotlib/examples/axes_grid/demo_axes_grid.py =================================================================== --- trunk/matplotlib/examples/axes_grid/demo_axes_grid.py 2009-09-13 05:42:01 UTC (rev 7752) +++ trunk/matplotlib/examples/axes_grid/demo_axes_grid.py 2009-09-13 05:44:21 UTC (rev 7753) @@ -44,7 +44,7 @@ Z, extent = get_demo_image() for i in range(4): im = grid[i].imshow(Z, extent=extent, interpolation="nearest") - plt.colorbar(im, cax = grid.cbar_axes[0]) + #plt.colorbar(im, cax = grid.cbar_axes[0]) grid.cbar_axes[0].colorbar(im) # This affects all axes as share_all = True. Added: trunk/matplotlib/examples/axes_grid/demo_axes_grid2.py =================================================================== --- trunk/matplotlib/examples/axes_grid/demo_axes_grid2.py (rev 0) +++ trunk/matplotlib/examples/axes_grid/demo_axes_grid2.py 2009-09-13 05:44:21 UTC (rev 7753) @@ -0,0 +1,124 @@ +import matplotlib.pyplot as plt +from mpl_toolkits.axes_grid import ImageGrid +import numpy as np + +def get_demo_image(): + from matplotlib.cbook import get_sample_data + f = get_sample_data("axes_grid/bivariate_normal.npy", asfileobj=False) + z = np.load(f) + # z is a numpy array of 15x15 + return z, (-3,4,-4,3) + + +def add_inner_title(ax, title, loc, size=None, **kwargs): + from matplotlib.offsetbox import AuxTransformBox, AnchoredOffsetbox + from matplotlib.font_manager import FontProperties + from matplotlib.patches import PathPatch + from matplotlib.textpath import TextPath + from matplotlib.transforms import IdentityTransform + if size is None: + size = FontProperties(size=plt.rcParams['legend.fontsize']) + text_path = TextPath((0, 0), title, size=10) + p1 = PathPatch(text_path, ec="w", lw=3, transform=IdentityTransform()) + p2 = PathPatch(text_path, ec="none", fc="k", transform=IdentityTransform()) + + offsetbox = AuxTransformBox(IdentityTransform()) + offsetbox.add_artist(p1) + offsetbox.add_artist(p2) + + ao = AnchoredOffsetbox(loc=loc, child=offsetbox, + pad=0., borderpad=0.5, + frameon=False, **kwargs) + ax.add_artist(ao) + + return ao + +if __name__ == "__main__": + F = plt.figure(1, (6, 6)) + F.clf() + + # prepare images + Z, extent = get_demo_image() + ZS = [Z[i::3,:] for i in range(3)] + extent = extent[0], extent[1]/3., extent[2], extent[3] + + # demo 1 : colorbar at each axes + + grid = ImageGrid(F, 211, # similar to subplot(111) + nrows_ncols = (1, 3), + direction="row", + axes_pad = 0.05, + add_all=True, + label_mode = "1", + share_all = True, + cbar_location="top", + cbar_mode="each", + cbar_size="7%", + cbar_pad="1%", + ) + + + for ax, z in zip(grid, ZS): + im = ax.imshow(z, origin="lower", extent=extent, interpolation="nearest") + ax.cax.colorbar(im) + + for ax, im_title in zip(grid, ["Image 1", "Image 2", "Image 3"]): + t = add_inner_title(ax, im_title, loc=3) + t.patch.set_alpha(0.5) + + for ax, z in zip(grid, ZS): + ax.cax.toggle_label(True) + axis = ax.cax.axis[ax.cax.orientation] + axis.label.set_text("counts s$^{-1}$") + axis.label.set_size(10) + axis.major_ticklabels.set_size(6) + + # changing the colorbar ticks + grid[1].cax.set_xticks([-1, 0, 1]) + grid[2].cax.set_xticks([-1, 0, 1]) + + grid[0].set_xticks([-2, 0]) + grid[0].set_yticks([-2, 0, 2]) + + + # demo 2 : shared colorbar + + grid2 = ImageGrid(F, 212, + nrows_ncols = (1, 3), + direction="row", + axes_pad = 0.05, + add_all=True, + label_mode = "1", + share_all = True, + cbar_location="right", + cbar_mode="single", + cbar_size="10%", + cbar_pad=0.05, + ) + + grid2[0].set_xlabel("X") + grid2[0].set_ylabel("Y") + + vmax, vmin = np.max(ZS), np.min(ZS) + import matplotlib.colors + norm = matplotlib.colors.normalize(vmax=vmax, vmin=vmin) + + for ax, z in zip(grid2, ZS): + im = ax.imshow(z, norm=norm, + origin="lower", extent=extent, + interpolation="nearest") + + # With cbar_mode="single", cax attribute of all axes are identical. + ax.cax.colorbar(im) + ax.cax.toggle_label(True) + + for ax, im_title in zip(grid2, ["(a)", "(b)", "(c)"]): + t = add_inner_title(ax, im_title, loc=2) + t.patch.set_ec("none") + t.patch.set_alpha(0.5) + + grid2[0].set_xticks([-2, 0]) + grid2[0].set_yticks([-2, 0, 2]) + + plt.draw() + plt.show() Added: trunk/matplotlib/examples/axes_grid/demo_colorbar_with_inset_locator.py =================================================================== --- trunk/matplotlib/examples/axes_grid/demo_colorbar_with_inset_locator.py (rev 0) +++ trunk/matplotlib/examples/axes_grid/demo_colorbar_with_inset_locator.py 2009-09-13 05:44:21 UTC (rev 7753) @@ -0,0 +1,44 @@ +import matplotlib.pyplot as plt + +from mpl_toolkits.axes_grid.inset_locator import inset_axes +#from mpl_toolkits.axes_grid.colorbar import colorbar + +fig = plt.figure(1, [6, 3]) + +# first subplot +ax1 = fig.add_subplot(121) + +axins1 = inset_axes(ax1, + width="50%", # width = 10% of parent_bbox width + height="5%", # height : 50% + loc=1) + +locator1=axins1.get_axes_locator() + +im1=ax1.imshow([[1,2],[2, 3]]) +colorbar(im1, cax=axins1, orientation="horizontal", ticks=[1,2,3]) +axins1.xaxis.set_ticks_position("bottom") + +# first subplot +ax = fig.add_subplot(122) + +axins = inset_axes(ax, + width="5%", # width = 10% of parent_bbox width + height="50%", # height : 50% + loc=3, + bbox_to_anchor=(1.05, 0., 1, 1), + bbox_transform=ax.transAxes, + borderpad=0, + ) + + +locator=axins.get_axes_locator() +# Controlling the placement of the inset axes is basically same as that +# of the legend. you may want to play with the borderpad value and +# the bbox_to_anchor coordinate. + +im=ax.imshow([[1,2],[2, 3]]) +colorbar(im, cax=axins) + +plt.draw() +plt.show() Modified: trunk/matplotlib/lib/mpl_toolkits/axes_grid/axes_divider.py =================================================================== --- trunk/matplotlib/lib/mpl_toolkits/axes_grid/axes_divider.py 2009-09-13 05:42:01 UTC (rev 7752) +++ trunk/matplotlib/lib/mpl_toolkits/axes_grid/axes_divider.py 2009-09-13 05:44:21 UTC (rev 7753) @@ -502,6 +502,31 @@ return ax + def append_axes(self, position, size, pad=None, **kwargs): + """ + create an axes at the given *position* with the same height + (or width) of the main axes. + + *position* + ["left"|"right"|"bottom"|"top"] + + *size* and *pad* should be axes_grid.axes_size compatible. + """ + + if position == "left": + ax = self.new_horizontal(size, pad, pack_start=True, **kwargs) + elif position == "right": + ax = self.new_horizontal(size, pad, pack_start=False, **kwargs) + elif position == "bottom": + ax = self.new_vertical(size, pad, pack_start=True, **kwargs) + elif position == "top": + ax = self.new_vertical(size, pad, pack_start=False, **kwargs) + else: + raise ValueError("the position must be one of left, right, bottom, or top") + + self._fig.add_axes(ax) + return ax + def get_aspect(self): if self._aspect is None: aspect = self._axes.get_aspect() Modified: trunk/matplotlib/lib/mpl_toolkits/axes_grid/axes_grid.py =================================================================== --- trunk/matplotlib/lib/mpl_toolkits/axes_grid/axes_grid.py 2009-09-13 05:42:01 UTC (rev 7752) +++ trunk/matplotlib/lib/mpl_toolkits/axes_grid/axes_grid.py 2009-09-13 05:44:21 UTC (rev 7753) @@ -2,7 +2,8 @@ import matplotlib.pyplot as plt import matplotlib.axes as maxes -import matplotlib.colorbar as mcolorbar +#import matplotlib.colorbar as mcolorbar +import colorbar as mcolorbar import matplotlib as mpl import matplotlib.patches as mpatches import matplotlib.lines as mlines @@ -10,8 +11,8 @@ from axes_divider import Size, SubplotDivider, LocatableAxes, Divider +import numpy as np - def _tick_only(ax, bottom_on, left_on): bottom_off = not bottom_on left_off = not left_on @@ -28,7 +29,7 @@ ax.axis["left"].label.set_visible(left_off) class Colorbar(mcolorbar.Colorbar): - def _config_axes(self, X, Y): + def _config_axes_deprecated(self, X, Y): ''' Make an axes patch and outline. ''' @@ -431,6 +432,7 @@ cbar_location="right", cbar_pad=None, cbar_size="5%", + cbar_set_cax=True, axes_class=None, ): """ @@ -456,9 +458,13 @@ cbar_location "right" [ "right" | "top" ] cbar_pad None cbar_size "5%" + cbar_set_cax True [ True | False ] axes_class None a type object which must be a subclass of :class:`~matplotlib.axes.Axes` ================ ======== ========================================= + + *cbar_set_cax* : if True, each axes in the grid has a cax + attribute that is bind to associated cbar_axes. """ self._nrows, self._ncols = nrows_ncols @@ -568,6 +574,14 @@ for ax in self.axes_all+self.cbar_axes: fig.add_axes(ax) + if cbar_set_cax: + if self._colorbar_mode == "single": + for ax in self.axes_all: + ax.cax = self.cbar_axes[0] + else: + for ax, cax in zip(self.axes_all, self.cbar_axes): + ax.cax = cax + self.set_label_mode(label_mode) @@ -683,8 +697,8 @@ -if __name__ == "__main__": -#if 0: +#if __name__ == "__main__": +if 0: from axes_divider import get_demo_image F = plt.figure(1, (9, 3.5)) F.clf() @@ -761,3 +775,5 @@ plt.ion() plt.draw() + + Added: trunk/matplotlib/lib/mpl_toolkits/axes_grid/colorbar.py =================================================================== --- trunk/matplotlib/lib/mpl_toolkits/axes_grid/colorbar.py (rev 0) +++ trunk/matplotlib/lib/mpl_toolkits/axes_grid/colorbar.py 2009-09-13 05:44:21 UTC (rev 7753) @@ -0,0 +1,816 @@ +''' +Colorbar toolkit with two classes and a function: + + :class:`ColorbarBase` + the base class with full colorbar drawing functionality. + It can be used as-is to make a colorbar for a given colormap; + a mappable object (e.g., image) is not needed. + + :class:`Colorbar` + the derived class for use with images or contour plots. + + :func:`make_axes` + a function for resizing an axes and adding a second axes + suitable for a colorbar + +The :meth:`~matplotlib.figure.Figure.colorbar` method uses :func:`make_axes` +and :class:`Colorbar`; the :func:`~matplotlib.pyplot.colorbar` function +is a thin wrapper over :meth:`~matplotlib.figure.Figure.colorbar`. + +''' + +import numpy as np +import matplotlib as mpl +import matplotlib.colors as colors +import matplotlib.cm as cm +from matplotlib import docstring +import matplotlib.ticker as ticker +import matplotlib.cbook as cbook +import matplotlib.collections as collections +import matplotlib.contour as contour +from matplotlib.path import Path +from matplotlib.patches import PathPatch +from matplotlib.transforms import Bbox + + +make_axes_kw_doc = ''' + + ============= ==================================================== + Property Description + ============= ==================================================== + *orientation* vertical or horizontal + *fraction* 0.15; fraction of original axes to use for colorbar + *pad* 0.05 if vertical, 0.15 if horizontal; fraction + of original axes between colorbar and new image axes + *shrink* 1.0; fraction by which to shrink the colorbar + *aspect* 20; ratio of long to short dimensions + ============= ==================================================== + +''' + +colormap_kw_doc = ''' + + =========== ==================================================== + Property Description + =========== ==================================================== + *extend* [ 'neither' | 'both' | 'min' | 'max' ] + If not 'neither', make pointed end(s) for out-of- + range values. These are set for a given colormap + using the colormap set_under and set_over methods. + *spacing* [ 'uniform' | 'proportional' ] + Uniform spacing gives each discrete color the same + space; proportional makes the space proportional to + the data interval. + *ticks* [ None | list of ticks | Locator object ] + If None, ticks are determined automatically from the + input. + *format* [ None | format string | Formatter object ] + If None, the + :class:`~matplotlib.ticker.ScalarFormatter` is used. + If a format string is given, e.g. '%.3f', that is + used. An alternative + :class:`~matplotlib.ticker.Formatter` object may be + given instead. + *drawedges* [ False | True ] If true, draw lines at color + boundaries. + =========== ==================================================== + + The following will probably be useful only in the context of + indexed colors (that is, when the mappable has norm=NoNorm()), + or other unusual circumstances. + + ============ =================================================== + Property Description + ============ =================================================== + *boundaries* None or a sequence + *values* None or a sequence which must be of length 1 less + than the sequence of *boundaries*. For each region + delimited by adjacent entries in *boundaries*, the + color mapped to the corresponding value in values + will be used. + ============ =================================================== + +''' + +colorbar_doc = ''' + +Add a colorbar to a plot. + +Function signatures for the :mod:`~matplotlib.pyplot` interface; all +but the first are also method signatures for the +:meth:`~matplotlib.figure.Figure.colorbar` method:: + + colorbar(**kwargs) + colorbar(mappable, **kwargs) + colorbar(mappable, cax=cax, **kwargs) + colorbar(mappable, ax=ax, **kwargs) + +arguments: + + *mappable* + the :class:`~matplotlib.image.Image`, + :class:`~matplotlib.contour.ContourSet`, etc. to + which the colorbar applies; this argument is mandatory for the + :meth:`~matplotlib.figure.Figure.colorbar` method but optional for the + :func:`~matplotlib.pyplot.colorbar` function, which sets the + default to the current image. + +keyword arguments: + + *cax* + None | axes object into which the colorbar will be drawn + *ax* + None | parent axes object from which space for a new + colorbar axes will be stolen + + +Additional keyword arguments are of two kinds: + + axes properties: +%s + colorbar properties: +%s + +If *mappable* is a :class:`~matplotlib.contours.ContourSet`, its *extend* +kwarg is included automatically. + +Note that the *shrink* kwarg provides a simple way to keep a vertical +colorbar, for example, from being taller than the axes of the mappable +to which the colorbar is attached; but it is a manual method requiring +some trial and error. If the colorbar is too tall (or a horizontal +colorbar is too wide) use a smaller value of *shrink*. + +For more precise control, you can manually specify the positions of +the axes objects in which the mappable and the colorbar are drawn. In +this case, do not use any of the axes properties kwargs. + +returns: + :class:`~matplotlib.colorbar.Colorbar` instance; see also its base class, + :class:`~matplotlib.colorbar.ColorbarBase`. Call the + :meth:`~matplotlib.colorbar.ColorbarBase.set_label` method + to label the colorbar. + + +The transData of the *cax* is adjusted so that the limits in the +longest axis actually corresponds to the limits in colorbar range. On +the other hand, the shortest axis has a data limits of [1,2], whose +unconventional value is to prevent underflow when log scale is used. +''' % (make_axes_kw_doc, colormap_kw_doc) + +docstring.interpd.update(colorbar_doc=colorbar_doc) + + +class CbarAxesLocator(object): + """ + CbarAxesLocator is a axes_locator for colobar axes. It adjust the + position of the axes to make a room for extended ends, i.e., the + extended ends are located outside the axes area. + """ + + def __init__(self, locator=None, extend="neither", orientation="vertical"): + """ + *locator* : the bbox returned from the locator is used as a + initial axes location. If None, axes.bbox is used. + + *extend* : same as in ColorbarBase + *orientation* : same as in ColorbarBase + + """ + self._locator = locator + self.extesion_fraction = 0.05 + self.extend = extend + self.orientation = orientation + + def get_original_position(self, axes, renderer): + """ + get the original position of the axes. + """ + if self._locator is None: + bbox = axes.get_position(original=True) + else: + bbox = self._locator(axes, renderer) + return bbox + + def get_end_vertices(self): + """ + return a tuple of two vertices for the colorbar extended ends. + The first vertives is for min. end, and the second is for + max. end. + """ + # Note that concatenating two vertices needs to make a + # vertices for the frame. + extesion_fraction = self.extesion_fraction + + corx = extesion_fraction*2. + cory = 1./(1. - corx) + x1, y1, w, h = 0, 0, 1, 1 + x2, y2 = x1 + w, y1 + h + dw, dh = w*extesion_fraction, h*extesion_fraction*cory + + if self.extend in ["min", "both"]: + bottom = [(x1, y1), + (x1+w/2., y1-dh), + (x2, y1)] + else: + bottom = [(x1, y1), + (x2, y1)] + + if self.extend in ["max", "both"]: + top = [(x2, y2), + (x1+w/2., y2+dh), + (x1, y2)] + else: + top = [(x2, y2), + (x1, y2)] + + if self.orientation == "horizontal": + bottom = [(y,x) for (x,y) in bottom] + top = [(y,x) for (x,y) in top] + + return bottom, top + + + def get_path_patch(self): + """ + get the path for axes patch + """ + end1, end2 = self.get_end_vertices() + + verts = [] + end1 + end2 + end1[:1] + + return Path(verts) + + + def get_path_ends(self): + """ + get the paths for extended ends + """ + + end1, end2 = self.get_end_vertices() + + return Path(end1), Path(end2) + + + def __call__(self, axes, renderer): + """ + Return the adjusted position of the axes + """ + bbox0 = self.get_original_position(axes, renderer) + bbox = bbox0 + + x1, y1, w, h = bbox.bounds + extesion_fraction = self.extesion_fraction + dw, dh = w*extesion_fraction, h*extesion_fraction + + if self.extend in ["min", "both"]: + if self.orientation == "horizontal": + x1 = x1 + dw + else: + y1 = y1+dh + + if self.extend in ["max", "both"]: + if self.orientation == "horizontal": + w = w-2*dw + else: + h = h-2*dh + + return Bbox.from_bounds(x1, y1, w, h) + + + +class ColorbarBase(cm.ScalarMappable): + ''' + Draw a colorbar in an existing axes. + + This is a base class for the :class:`Colorbar` class, which is the + basis for the :func:`~matplotlib.pyplot.colorbar` method and pylab + function. + + It is also useful by itself for showing a colormap. If the *cmap* + kwarg is given but *boundaries* and *values* are left as None, + then the colormap will be displayed on a 0-1 scale. To show the + under- and over-value colors, specify the *norm* as:: + + colors.Normalize(clip=False) + + To show the colors versus index instead of on the 0-1 scale, + use:: + + norm=colors.NoNorm. + + Useful attributes: + + :attr:`ax` + the Axes instance in which the colorbar is drawn + + :attr:`lines` + a LineCollection if lines were drawn, otherwise None + + :attr:`dividers` + a LineCollection if *drawedges* is True, otherwise None + + Useful public methods are :meth:`set_label` and :meth:`add_lines`. + + ''' + + def __init__(self, ax, cmap=None, + norm=None, + alpha=1.0, + values=None, + boundaries=None, + orientation='vertical', + extend='neither', + spacing='uniform', # uniform or proportional + ticks=None, + format=None, + drawedges=False, + filled=True, + ): + self.ax = ax + + if cmap is None: cmap = cm.get_cmap() + if norm is None: norm = colors.Normalize() + self.alpha = alpha + cm.ScalarMappable.__init__(self, cmap=cmap, norm=norm) + self.values = values + self.boundaries = boundaries + self.extend = extend + self.spacing = spacing + self.orientation = orientation + self.drawedges = drawedges + self.filled = filled + + # artists + self.solids = None + self.lines = None + self.dividers = None + self.extension_patch1 = None + self.extension_patch2 = None + + if orientation == "vertical": + self.cbar_axis = self.ax.yaxis + else: + self.cbar_axis = self.ax.xaxis + + + if format is None: + if isinstance(self.norm, colors.LogNorm): + # change both axis for proper aspect + self.ax.xaxis.set_scale("log") + self.ax.yaxis.set_scale("log") + self.ax._update_transScale() + self.cbar_axis.set_minor_locator(ticker.NullLocator()) + formatter = ticker.LogFormatter() + else: + formatter = None + elif cbook.is_string_like(format): + formatter = ticker.FormatStrFormatter(format) + else: + formatter = format # Assume it is a Formatter + + if formatter is None: + formatter = self.cbar_axis.get_major_formatter() + else: + self.cbar_axis.set_major_formatter(formatter) + + if cbook.iterable(ticks): + self.cbar_axis.set_ticks(ticks) + elif ticks is not None: + self.cbar_axis.set_major_locator(ticks) + else: + self._select_locator(formatter) + + + self._config_axes() + + self.update_artists() + + self.set_label_text('') + + + def _get_colorbar_limits(self): + """ + initial limits for colorbar range. The returne min, max values + will be used to create colorbar solid(?) and etc. + """ + if self.boundaries is not None: + C = self.boundaries + if self.extend in ["min", "both"]: + C = C[1:] + + if self.extend in ["max", "both"]: + C = C[:-1] + return min(C), max(C) + else: + return self.get_clim() + + + def _config_axes(self): + ''' + Adjust the properties of the axes to be adquate for colorbar display. + ''' + ax = self.ax + + axes_locator = CbarAxesLocator(ax.get_axes_locator(), + extend=self.extend, + orientation=self.orientation) + ax.set_axes_locator(axes_locator) + + # overide the get_data_ratio for the aspect works. + def _f(): + return 1. + ax.get_data_ratio = _f + ax.get_data_ratio_log = _f + + ax.set_frame_on(True) + ax.set_navigate(False) + + self.ax.set_autoscalex_on(False) + self.ax.set_autoscaley_on(False) + + if self.orientation == 'horizontal': + ax.xaxis.set_label_position('bottom') + ax.set_yticks([]) + else: + ax.set_xticks([]) + ax.yaxis.set_label_position('right') + ax.yaxis.set_ticks_position('right') + + + + def update_artists(self): + """ + Update the colorbar associated artists, *filled* and + *ends*. Note that *lines* are not updated. This needs to be + called whenever clim of associated image changes. + """ + self._process_values() + self._add_ends() + + X, Y = self._mesh() + if self.filled: + C = self._values[:,np.newaxis] + self._add_solids(X, Y, C) + + ax = self.ax + vmin, vmax = self._get_colorbar_limits() + if self.orientation == 'horizontal': + ax.set_ylim(1, 2) + ax.set_xlim(vmin, vmax) + else: + ax.set_xlim(1, 2) + ax.set_ylim(vmin, vmax) + + + def _add_ends(self): + """ + Create patches from extended ends and add them to the axes. + """ + + del self.extension_patch1 + del self.extension_patch2 + + path1, path2 = self.ax.get_axes_locator().get_path_ends() + fc=mpl.rcParams['axes.facecolor'] + ec=mpl.rcParams['axes.edgecolor'] + linewidths=0.5*mpl.rcParams['axes.linewidth'] + self.extension_patch1 = PathPatch(path1, + fc=fc, ec=ec, lw=linewidths, + zorder=2., + transform=self.ax.transAxes, + clip_on=False) + self.extension_patch2 = PathPatch(path2, + fc=fc, ec=ec, lw=linewidths, + zorder=2., + transform=self.ax.transAxes, + clip_on=False) + self.ax.add_artist(self.extension_patch1) + self.ax.add_artist(self.extension_patch2) + + + + def _set_label_text(self): + """ + set label. + """ + self.cbar_axis.set_label_text(self._label, **self._labelkw) + + def set_label_text(self, label, **kw): + ''' + Label the long axis of the colorbar + ''' + self._label = label + self._labelkw = kw + self._set_label_text() + + + def _edges(self, X, Y): + ''' + Return the separator line segments; helper for _add_solids. + ''' + N = X.shape[0] + # Using the non-array form of these line segments is much + # simpler than making them into arrays. + if self.orientation == 'vertical': + return [zip(X[i], Y[i]) for i in range(1, N-1)] + else: + return [zip(Y[i], X[i]) for i in range(1, N-1)] + + def _add_solids(self, X, Y, C): + ''' + Draw the colors using :meth:`~matplotlib.axes.Axes.pcolor`; + optionally add separators. + ''' + ## Change to pcolorfast after fixing bugs in some backends... + + if self.extend in ["min", "both"]: + cc = self.to_rgba([C[0][0]]) + self.extension_patch1.set_fc(cc[0]) + X, Y, C = X[1:], Y[1:], C[1:] + + if self.extend in ["max", "both"]: + cc = self.to_rgba([C[-1][0]]) + self.extension_patch2.set_fc(cc[0]) + X, Y, C = X[:-1], Y[:-1], C[:-1] + + if self.orientation == 'vertical': + args = (X, Y, C) + else: + args = (np.transpose(Y), np.transpose(X), np.transpose(C)) + kw = {'cmap':self.cmap, 'norm':self.norm, + 'shading':'flat', 'alpha':self.alpha, + } + + del self.solids + del self.dividers + + col = self.ax.pcolor(*args, **kw) + self.solids = col + if self.drawedges: + self.dividers = collections.LineCollection(self._edges(X,Y), + colors=(mpl.rcParams['axes.edgecolor'],), + linewidths=(0.5*mpl.rcParams['axes.linewidth'],), + ) + self.ax.add_collection(self.dividers) + else: + self.dividers = None + + def add_lines(self, levels, colors, linewidths): + ''' + Draw lines on the colorbar. It deletes preexting lines. + ''' + del self.lines + + N = len(levels) + x = np.array([1.0, 2.0]) + X, Y = np.meshgrid(x,levels) + if self.orientation == 'vertical': + xy = [zip(X[i], Y[i]) for i in range(N)] + else: + xy = [zip(Y[i], X[i]) for i in range(N)] + col = collections.LineCollection(xy, linewidths=linewidths, + ) + self.lines = col + col.set_color(colors) + self.ax.add_collection(col) + + + def _select_locator(self, formatter): + ''' + select a suitable locator + ''' + if self.boundaries is None: + if isinstance(self.norm, colors.NoNorm): + nv = len(self._values) + base = 1 + int(nv/10) + locator = ticker.IndexLocator(base=base, offset=0) + elif isinstance(self.norm, colors.BoundaryNorm): + b = self.norm.boundaries + locator = ticker.FixedLocator(b, nbins=10) + elif isinstance(self.norm, colors.LogNorm): + locator = ticker.LogLocator() + else: + locator = ticker.MaxNLocator(nbins=5) + else: + b = self._boundaries[self._inside] + locator = ticker.FixedLocator(b) #, nbins=10) + + self.cbar_axis.set_major_locator(locator) + + + def _process_values(self, b=None): + ''' + Set the :attr:`_boundaries` and :attr:`_values` attributes + based on the input boundaries and values. Input boundaries + can be *self.boundaries* or the argument *b*. + ''' + if b is None: + b = self.boundaries + if b is not None: + self._boundaries = np.asarray(b, dtype=float) + if self.values is None: + self._values = 0.5*(self._boundaries[:-1] + + self._boundaries[1:]) + if isinstance(self.norm, colors.NoNorm): + self._values = (self._values + 0.00001).astype(np.int16) + return + self._values = np.array(self.values) + return + if self.values is not None: + self._values = np.array(self.values) + if self.boundaries is None: + b = np.zeros(len(self.values)+1, 'd') + b[1:-1] = 0.5*(self._values[:-1] - self._values[1:]) + b[0] = 2.0*b[1] - b[2] + b[-1] = 2.0*b[-2] - b[-3] + self._boundaries = b + return + self._boundaries = np.array(self.boundaries) + return + # Neither boundaries nor values are specified; + # make reasonable ones based on cmap and norm. + if isinstance(self.norm, colors.NoNorm): + b = self._uniform_y(self.cmap.N+1) * self.cmap.N - 0.5 + v = np.zeros((len(b)-1,), dtype=np.int16) + v = np.arange(self.cmap.N, dtype=np.int16) + self._boundaries = b + self._values = v + return + elif isinstance(self.norm, colors.BoundaryNorm): + b = np.array(self.norm.boundaries) + v = np.zeros((len(b)-1,), dtype=float) + bi = self.norm.boundaries + v = 0.5*(bi[:-1] + bi[1:]) + self._boundaries = b + self._values = v + return + else: + b = self._uniform_y(self.cmap.N+1) + + self._process_values(b) + + + def _uniform_y(self, N): + ''' + Return colorbar data coordinates for *N* uniformly + spaced boundaries. + ''' + vmin, vmax = self._get_colorbar_limits() + if isinstance(self.norm, colors.LogNorm): + y = np.logspace(np.log10(vmin), np.log10(vmax), N) + else: + y = np.linspace(vmin, vmax, N) + return y + + def _mesh(self): + ''' + Return X,Y, the coordinate arrays for the colorbar pcolormesh. + These are suitable for a vertical colorbar; swapping and + transposition for a horizontal colorbar are done outside + this function. + ''' + x = np.array([1.0, 2.0]) + if self.spacing == 'uniform': + y = self._uniform_y(len(self._boundaries)) + else: + y = self._boundaries + self._y = y + + X, Y = np.meshgrid(x,y) + return X, Y + + + def set_alpha(self, alpha): + """ + set alpha value. + """ + self.alpha = alpha + + +class Colorbar(ColorbarBase): + def __init__(self, ax, mappable, **kw): + mappable.autoscale_None() # Ensure mappable.norm.vmin, vmax + # are set when colorbar is called, + # even if mappable.draw has not yet + # been called. This will not change + # vmin, vmax if they are already set. + self.mappable = mappable + kw['cmap'] = mappable.cmap + kw['norm'] = mappable.norm + kw['alpha'] = mappable.get_alpha() + if isinstance(mappable, contour.ContourSet): + CS = mappable + kw['boundaries'] = CS._levels + kw['values'] = CS.cvalues + kw['extend'] = CS.extend + #kw['ticks'] = CS._levels + kw.setdefault('ticks', ticker.FixedLocator(CS.levels, nbins=10)) + kw['filled'] = CS.filled + ColorbarBase.__init__(self, ax, **kw) + if not CS.filled: + self.add_lines(CS) + else: + ColorbarBase.__init__(self, ax, **kw) + + + def add_lines(self, CS): + ''' + Add the lines from a non-filled + :class:`~matplotlib.contour.ContourSet` to the colorbar. + ''' + if not isinstance(CS, contour.ContourSet) or CS.filled: + raise ValueError('add_lines is only for a ContourSet of lines') + tcolors = [c[0] for c in CS.tcolors] + tlinewidths = [t[0] for t in CS.tlinewidths] + # The following was an attempt to get the colorbar lines + # to follow subsequent changes in the contour lines, + # but more work is needed: specifically, a careful + # look at event sequences, and at how + # to make one object track another automatically. + #tcolors = [col.get_colors()[0] for col in CS.collections] + #tlinewidths = [col.get_linewidth()[0] for lw in CS.collections] + #print 'tlinewidths:', tlinewidths + ColorbarBase.add_lines(self, CS.levels, tcolors, tlinewidths) + + def update_bruteforce(self, mappable): + """ + Update the colorbar artists to reflect the change of the + assocaited mappable. + """ + self.update_artists() + + if isinstance(mappable, contour.ContourSet): + if not mappable.filled: + self.add_lines(mappable) + +...@do...bstitution(make_axes_kw_doc) +def make_axes(parent, **kw): + ''' + Resize and reposition a parent axes, and return a child + axes suitable for a colorbar:: + + cax, kw = make_axes(parent, **kw) + + Keyword arguments may include the following (with defaults): + + *orientation* + 'vertical' or 'horizontal' + + %s + + All but the first of these are stripped from the input kw set. + + Returns (cax, kw), the child axes and the reduced kw dictionary. + ''' + orientation = kw.setdefault('orientation', 'vertical') + fraction = kw.pop('fraction', 0.15) + shrink = kw.pop('shrink', 1.0) + aspect = kw.pop('aspect', 20) + #pb = transforms.PBox(parent.get_position()) + pb = parent.get_position(original=True).frozen() + if orientation == 'vertical': + pad = kw.pop('pad', 0.05) + x1 = 1.0-fraction + pb1, pbx, pbcb = pb.splitx(x1-pad, x1) + pbcb = pbcb.shrunk(1.0, shrink).anchored('C', pbcb) + anchor = (0.0, 0.5) + panchor = (1.0, 0.5) + else: + pad = kw.pop('pad', 0.15) + pbcb, pbx, pb1 = pb.splity(fraction, fraction+pad) + pbcb = pbcb.shrunk(shrink, 1.0).anchored('C', pbcb) + aspect = 1.0/aspect + anchor = (0.5, 1.0) + panchor = (0.5, 0.0) + parent.set_position(pb1) + parent.set_anchor(panchor) + fig = parent.get_figure() + cax = fig.add_axes(pbcb) + cax.set_aspect(aspect, anchor=anchor, adjustable='box') + return cax, kw + + +def colorbar(mappable, cax=None, ax=None, **kw): + """ + Create a colorbar for a ScalarMappable instance. + + Documentation for the pylab thin wrapper: + %(colorbar_doc)s + """ + import matplotlib.pyplot as plt + if ax is None: + ax = plt.gca() + if cax is None: + cax, kw = make_axes(ax, **kw) + cax.hold(True) + cb = Colorbar(cax, mappable, **kw) + + def on_changed(m): + cb.set_cmap(m.get_cmap()) + cb.set_clim(m.get_clim()) + cb.update_bruteforce(m) + + cbid = mappable.callbacksSM.connect('changed', on_changed) + mappable.set_colorbar(cb, cax) + ax.figure.sca(ax) + return cb This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |