From ae0b6f3aaf13fc49728308887f8c17ff8dd57b1b Mon Sep 17 00:00:00 2001 From: Eric Dill Date: Sat, 27 Jun 2015 18:35:30 -0400 Subject: [PATCH 1/7] DOC/MNT: Throwing some docstrings at axes_rgb.py In my learning how to use axes_rgb.py, I ended up writing some docstrings. Hope this is useful. --- lib/mpl_toolkits/axes_grid1/axes_rgb.py | 109 +++++++++++++++++++----- lib/mpl_toolkits/axes_grid1/mpl_axes.py | 9 +- 2 files changed, 90 insertions(+), 28 deletions(-) diff --git a/lib/mpl_toolkits/axes_grid1/axes_rgb.py b/lib/mpl_toolkits/axes_grid1/axes_rgb.py index d1f655851581..285491585618 100644 --- a/lib/mpl_toolkits/axes_grid1/axes_rgb.py +++ b/lib/mpl_toolkits/axes_grid1/axes_rgb.py @@ -5,6 +5,8 @@ import numpy as np from .axes_divider import make_axes_locatable, Size, locatable_axes_factory +import sys +import warnings def make_rgb_axes(ax, pad=0.01, axes_class=None, add_all=True): """ @@ -75,17 +77,59 @@ def imshow_rgb(ax, r, g, b, **kwargs): from .mpl_axes import Axes class RGBAxesBase(object): - + """base class for a 4-panel imshow (RGB, R, G, B) + + Layout: + +---------------+-----+ + | | R | + + +-----+ + | RGB | G | + + +-----+ + | | B | + +---------------+-----+ + + Attributes + ---------- + _defaultAxesClass : matplotlib.axes.Axes + defaults to 'Axes' in RGBAxes child class. + No default in abstract base class + RGB : _defaultAxesClass + The axes object for the three-channel imshow + R : _defaultAxesClass + The axes object for the red channel imshow + G : _defaultAxesClass + The axes object for the green channel imshow + B : _defaultAxesClass + The axes object for the blue channel imshow + """ def __init__(self, *kl, **kwargs): + """ + Parameters + ---------- + pad : float + fraction of the axes height to put as padding. + defaults to 0.0 + add_all : bool + True: Add the {rgb, r, g, b} axes to the figure + defaults to True. + axes_class : matplotlib.axes.Axes + + kl : + Unpacked into axes_class() init for RGB + kwargs : + Unpacked into axes_class() init for RGB, R, G, B axes + """ pad = kwargs.pop("pad", 0.0) add_all = kwargs.pop("add_all", True) - axes_class = kwargs.pop("axes_class", None) - - - - - if axes_class is None: - axes_class = self._defaultAxesClass + try: + axes_class = kwargs.pop("axes_class", self._defaultAxesClass) + except AttributeError: + new_msg = ("A subclass of RGBAxesBase must have a " + "_defaultAxesClass attribute. If you are not sure which " + "axes class to use, consider using " + "mpl_toolkits.axes_grid1.mpl_axes.Axes.") + six.reraise(KeyError, AttributeError(new_msg), + sys.exc_info()[2]) ax = axes_class(*kl, **kwargs) @@ -109,11 +153,6 @@ def __init__(self, *kl, **kwargs): locator = divider.new_locator(nx=2, ny=ny) ax1.set_axes_locator(locator) ax1.axis[:].toggle(ticklabels=False) - #for t in ax1.yaxis.get_ticklabels() + ax1.xaxis.get_ticklabels(): - # t.set_visible(False) - #if hasattr(ax1, "_axislines"): - # for axisline in ax1._axislines.values(): - # axisline.major_ticklabels.set_visible(False) ax_rgb.append(ax1) self.RGB = ax @@ -126,25 +165,49 @@ def __init__(self, *kl, **kwargs): self._config_axes() - def _config_axes(self): - for ax1 in [self.RGB, self.R, self.G, self.B]: - #for sp1 in ax1.spines.values(): - # sp1.set_color("w") - ax1.axis[:].line.set_color("w") - ax1.axis[:].major_ticks.set_mec("w") - # for tick in ax1.xaxis.get_major_ticks() + ax1.yaxis.get_major_ticks(): - # tick.tick1line.set_mec("w") - # tick.tick2line.set_mec("w") - + def _config_axes(self, line_color='w', marker_edge_color='w'): + """Set the line color and ticks for the axes + Parameters + ---------- + line_color : any matplotlib color + marker_edge_color : any matplotlib color + """ + for ax1 in [self.RGB, self.R, self.G, self.B]: + ax1.axis[:].line.set_color(line_color) + ax1.axis[:].major_ticks.set_markeredgecolor(marker_edge_color) def add_RGB_to_figure(self): + """Add the red, green and blue axes to the RGB composite's axes figure + """ self.RGB.get_figure().add_axes(self.R) self.RGB.get_figure().add_axes(self.G) self.RGB.get_figure().add_axes(self.B) def imshow_rgb(self, r, g, b, **kwargs): + """Create the four images {rgb, r, g, b} + + Parameters + ---------- + r : array-like + The red array + g : array-like + The green array + b : array-like + The blue array + kwargs : imshow kwargs + kwargs get unpacked into the imshow calls for the four images + + Returns + ------- + rgb : matplotlib.image.AxesImage + r : matplotlib.image.AxesImage + g : matplotlib.image.AxesImage + b : matplotlib.image.AxesImage + """ ny, nx = r.shape + assert((nx, ny) == g.shape == b.shape) + R = np.zeros([ny, nx, 3], dtype="d") R[:,:,0] = r G = np.zeros_like(R) diff --git a/lib/mpl_toolkits/axes_grid1/mpl_axes.py b/lib/mpl_toolkits/axes_grid1/mpl_axes.py index 9235897d5121..72b58e4cdf41 100644 --- a/lib/mpl_toolkits/axes_grid1/mpl_axes.py +++ b/lib/mpl_toolkits/axes_grid1/mpl_axes.py @@ -33,10 +33,12 @@ def __init__(self, axes): def __getitem__(self, k): if isinstance(k, tuple): - r = SimpleChainedObjects([dict.__getitem__(self, k1) for k1 in k]) + r = SimpleChainedObjects( + [super(Axes.AxisDict, self).__getitem__(self, k1) + for k1 in k]) return r elif isinstance(k, slice): - if k.start == None and k.stop == None and k.step == None: + if k.start is None and k.stop is None and k.step is None: r = SimpleChainedObjects(list(six.itervalues(self))) return r else: @@ -47,12 +49,9 @@ def __getitem__(self, k): def __call__(self, *v, **kwargs): return maxes.Axes.axis(self.axes, *v, **kwargs) - def __init__(self, *kl, **kw): super(Axes, self).__init__(*kl, **kw) - - def _init_axis_artists(self, axes=None): if axes is None: axes = self From fdfb92b22734f278e65709481b4a24be5f3cb383 Mon Sep 17 00:00:00 2001 From: Eric Dill Date: Sat, 27 Jun 2015 19:30:08 -0400 Subject: [PATCH 2/7] MNT: Don't need the self in super __getitem__ --- lib/mpl_toolkits/axes_grid1/mpl_axes.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/mpl_toolkits/axes_grid1/mpl_axes.py b/lib/mpl_toolkits/axes_grid1/mpl_axes.py index 72b58e4cdf41..c3d604754248 100644 --- a/lib/mpl_toolkits/axes_grid1/mpl_axes.py +++ b/lib/mpl_toolkits/axes_grid1/mpl_axes.py @@ -34,8 +34,7 @@ def __init__(self, axes): def __getitem__(self, k): if isinstance(k, tuple): r = SimpleChainedObjects( - [super(Axes.AxisDict, self).__getitem__(self, k1) - for k1 in k]) + [super(Axes.AxisDict, self).__getitem__(k1) for k1 in k]) return r elif isinstance(k, slice): if k.start is None and k.stop is None and k.step is None: From 2ce78a70f90508331db21ea10ee991ac11300021 Mon Sep 17 00:00:00 2001 From: Eric Dill Date: Sun, 28 Jun 2015 09:28:05 -0400 Subject: [PATCH 3/7] MNT: Reraise the same exception Fixed a sloppy copy/paste in py2/py3 compatible reraising --- lib/mpl_toolkits/axes_grid1/axes_rgb.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/mpl_toolkits/axes_grid1/axes_rgb.py b/lib/mpl_toolkits/axes_grid1/axes_rgb.py index 285491585618..94b262b19c1e 100644 --- a/lib/mpl_toolkits/axes_grid1/axes_rgb.py +++ b/lib/mpl_toolkits/axes_grid1/axes_rgb.py @@ -6,7 +6,6 @@ import numpy as np from .axes_divider import make_axes_locatable, Size, locatable_axes_factory import sys -import warnings def make_rgb_axes(ax, pad=0.01, axes_class=None, add_all=True): """ @@ -128,7 +127,7 @@ def __init__(self, *kl, **kwargs): "_defaultAxesClass attribute. If you are not sure which " "axes class to use, consider using " "mpl_toolkits.axes_grid1.mpl_axes.Axes.") - six.reraise(KeyError, AttributeError(new_msg), + six.reraise(AttributeError, AttributeError(new_msg), sys.exc_info()[2]) ax = axes_class(*kl, **kwargs) From 0d1dde70b191291321a71488de8a34434ea138d5 Mon Sep 17 00:00:00 2001 From: Eric Dill Date: Mon, 29 Jun 2015 10:39:24 -0400 Subject: [PATCH 4/7] MNT: Move import to top of module --- lib/mpl_toolkits/axes_grid1/axes_rgb.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/mpl_toolkits/axes_grid1/axes_rgb.py b/lib/mpl_toolkits/axes_grid1/axes_rgb.py index 94b262b19c1e..1934c07495ff 100644 --- a/lib/mpl_toolkits/axes_grid1/axes_rgb.py +++ b/lib/mpl_toolkits/axes_grid1/axes_rgb.py @@ -6,6 +6,8 @@ import numpy as np from .axes_divider import make_axes_locatable, Size, locatable_axes_factory import sys +from .mpl_axes import Axes + def make_rgb_axes(ax, pad=0.01, axes_class=None, add_all=True): """ @@ -54,8 +56,6 @@ def make_rgb_axes(ax, pad=0.01, axes_class=None, add_all=True): return ax_rgb -#import matplotlib.axes as maxes - def imshow_rgb(ax, r, g, b, **kwargs): ny, nx = r.shape @@ -73,8 +73,6 @@ def imshow_rgb(ax, r, g, b, **kwargs): return im_rgb -from .mpl_axes import Axes - class RGBAxesBase(object): """base class for a 4-panel imshow (RGB, R, G, B) From 740b2a0838be02273fca1ccb9c425049e534e667 Mon Sep 17 00:00:00 2001 From: Eric Dill Date: Mon, 29 Jun 2015 10:44:50 -0400 Subject: [PATCH 5/7] MNT: assert -> ValueError --- lib/mpl_toolkits/axes_grid1/axes_rgb.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/mpl_toolkits/axes_grid1/axes_rgb.py b/lib/mpl_toolkits/axes_grid1/axes_rgb.py index 1934c07495ff..dfb309858dd9 100644 --- a/lib/mpl_toolkits/axes_grid1/axes_rgb.py +++ b/lib/mpl_toolkits/axes_grid1/axes_rgb.py @@ -203,7 +203,12 @@ def imshow_rgb(self, r, g, b, **kwargs): b : matplotlib.image.AxesImage """ ny, nx = r.shape - assert((nx, ny) == g.shape == b.shape) + if not ((nx, ny) == g.shape == b.shape): + raise ValueError('Input shapes do not match.'' + '\nr.shape = {}' + '\ng.shape = {}' + '\nb.shape = {}' + ''.format(r.shape, g.shape, b.shape)) R = np.zeros([ny, nx, 3], dtype="d") R[:,:,0] = r From 2d56e75300c91598211ba3a2353260e0c4be1355 Mon Sep 17 00:00:00 2001 From: Eric Dill Date: Wed, 1 Jul 2015 07:07:01 -0400 Subject: [PATCH 6/7] MNT: At least it will run now Not entirely clear on what the code at the bottom of lib/mpl_toolkits/axes_grid1/mpl_axes.py is supposed to do, but at least it will run now... --- lib/mpl_toolkits/axes_grid1/mpl_axes.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/mpl_toolkits/axes_grid1/mpl_axes.py b/lib/mpl_toolkits/axes_grid1/mpl_axes.py index c3d604754248..68eb9eeea1dc 100644 --- a/lib/mpl_toolkits/axes_grid1/mpl_axes.py +++ b/lib/mpl_toolkits/axes_grid1/mpl_axes.py @@ -151,7 +151,8 @@ def toggle(self, all=None, ticks=None, ticklabels=None, label=None): if __name__ == '__main__': - fig = figure() + import matplotlib.pyplot as plt + fig = plt.figure() ax = Axes(fig, [0.1, 0.1, 0.8, 0.8]) fig.add_axes(ax) ax.cla() From 640743fb4ee2ec550d662d93572c76f3b2936be5 Mon Sep 17 00:00:00 2001 From: Eric Dill Date: Wed, 1 Jul 2015 07:13:26 -0400 Subject: [PATCH 7/7] MNT: Remove trailing apostrophe --- lib/mpl_toolkits/axes_grid1/axes_rgb.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/mpl_toolkits/axes_grid1/axes_rgb.py b/lib/mpl_toolkits/axes_grid1/axes_rgb.py index dfb309858dd9..e87b59cc556e 100644 --- a/lib/mpl_toolkits/axes_grid1/axes_rgb.py +++ b/lib/mpl_toolkits/axes_grid1/axes_rgb.py @@ -204,7 +204,7 @@ def imshow_rgb(self, r, g, b, **kwargs): """ ny, nx = r.shape if not ((nx, ny) == g.shape == b.shape): - raise ValueError('Input shapes do not match.'' + raise ValueError('Input shapes do not match.' '\nr.shape = {}' '\ng.shape = {}' '\nb.shape = {}'