From b9ed974d116946bf5d0b2439eff0126d8bd53834 Mon Sep 17 00:00:00 2001 From: none Date: Tue, 4 Aug 2015 19:48:00 +0200 Subject: [PATCH 1/5] Allow "figure" kwarg for host functions in parasite_axes. --- lib/mpl_toolkits/axes_grid1/parasite_axes.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/mpl_toolkits/axes_grid1/parasite_axes.py b/lib/mpl_toolkits/axes_grid1/parasite_axes.py index 1241c93a0d67..4672a4336969 100644 --- a/lib/mpl_toolkits/axes_grid1/parasite_axes.py +++ b/lib/mpl_toolkits/axes_grid1/parasite_axes.py @@ -487,7 +487,10 @@ def host_axes(*args, **kwargs): import matplotlib.pyplot as plt axes_class = kwargs.pop("axes_class", None) host_axes_class = host_axes_class_factory(axes_class) - fig = plt.gcf() + if "figure" in kwargs: + fig = kwargs["figure"] + else: + fig = plt.gcf() ax = host_axes_class(fig, *args, **kwargs) fig.add_axes(ax) plt.draw_if_interactive() @@ -497,7 +500,10 @@ def host_subplot(*args, **kwargs): import matplotlib.pyplot as plt axes_class = kwargs.pop("axes_class", None) host_subplot_class = host_subplot_class_factory(axes_class) - fig = plt.gcf() + if "figure" in kwargs: + fig = kwargs["figure"] + else: + fig = plt.gcf() ax = host_subplot_class(fig, *args, **kwargs) fig.add_subplot(ax) plt.draw_if_interactive() From 172893dda9d58b9d8b07d7dd0c39fb3a6a80e0d2 Mon Sep 17 00:00:00 2001 From: none Date: Tue, 4 Aug 2015 21:55:56 +0200 Subject: [PATCH 2/5] Probably better this way. --- lib/mpl_toolkits/axes_grid1/parasite_axes.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/mpl_toolkits/axes_grid1/parasite_axes.py b/lib/mpl_toolkits/axes_grid1/parasite_axes.py index 4672a4336969..a55877e94a37 100644 --- a/lib/mpl_toolkits/axes_grid1/parasite_axes.py +++ b/lib/mpl_toolkits/axes_grid1/parasite_axes.py @@ -487,7 +487,7 @@ def host_axes(*args, **kwargs): import matplotlib.pyplot as plt axes_class = kwargs.pop("axes_class", None) host_axes_class = host_axes_class_factory(axes_class) - if "figure" in kwargs: + if kwargs.get("figure") != None: fig = kwargs["figure"] else: fig = plt.gcf() @@ -500,7 +500,7 @@ def host_subplot(*args, **kwargs): import matplotlib.pyplot as plt axes_class = kwargs.pop("axes_class", None) host_subplot_class = host_subplot_class_factory(axes_class) - if "figure" in kwargs: + if kwargs.get("figure") != None: fig = kwargs["figure"] else: fig = plt.gcf() From 0e77b8024d191a05bd8771919dc65bf7976d6851 Mon Sep 17 00:00:00 2001 From: none Date: Tue, 4 Aug 2015 23:16:03 +0200 Subject: [PATCH 3/5] Don't do equality comparison to None. --- lib/mpl_toolkits/axes_grid1/parasite_axes.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/mpl_toolkits/axes_grid1/parasite_axes.py b/lib/mpl_toolkits/axes_grid1/parasite_axes.py index a55877e94a37..c3fad8b0288e 100644 --- a/lib/mpl_toolkits/axes_grid1/parasite_axes.py +++ b/lib/mpl_toolkits/axes_grid1/parasite_axes.py @@ -487,7 +487,7 @@ def host_axes(*args, **kwargs): import matplotlib.pyplot as plt axes_class = kwargs.pop("axes_class", None) host_axes_class = host_axes_class_factory(axes_class) - if kwargs.get("figure") != None: + if kwargs.get("figure") is not None: fig = kwargs["figure"] else: fig = plt.gcf() @@ -500,7 +500,7 @@ def host_subplot(*args, **kwargs): import matplotlib.pyplot as plt axes_class = kwargs.pop("axes_class", None) host_subplot_class = host_subplot_class_factory(axes_class) - if kwargs.get("figure") != None: + if kwargs.get("figure") is not None: fig = kwargs["figure"] else: fig = plt.gcf() From 9c2488772c1d70d67396c110b857e5c6cc72c2f8 Mon Sep 17 00:00:00 2001 From: none Date: Wed, 5 Aug 2015 02:40:40 +0200 Subject: [PATCH 4/5] Added doc strings. --- lib/mpl_toolkits/axes_grid1/parasite_axes.py | 24 ++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/lib/mpl_toolkits/axes_grid1/parasite_axes.py b/lib/mpl_toolkits/axes_grid1/parasite_axes.py index c3fad8b0288e..9af320ff0672 100644 --- a/lib/mpl_toolkits/axes_grid1/parasite_axes.py +++ b/lib/mpl_toolkits/axes_grid1/parasite_axes.py @@ -484,6 +484,18 @@ def host_subplot_class_factory(axes_class): def host_axes(*args, **kwargs): + """ + Create axes that can act as a hosts to parasitic axes. + + Parameters + ---------- + figure : `matplotlib.figure.Figure` + Figure to which the axes will be added. Defaults to the current figure + `pyplot.gcf()`. + + *args, **kwargs : + Will be passed on to the underlying ``Axes`` object creation. + """ import matplotlib.pyplot as plt axes_class = kwargs.pop("axes_class", None) host_axes_class = host_axes_class_factory(axes_class) @@ -497,6 +509,18 @@ def host_axes(*args, **kwargs): return ax def host_subplot(*args, **kwargs): + """ + Create a subplot that can act as a host to parasitic axes. + + Parameters + ---------- + figure : `matplotlib.figure.Figure` + Figure to which the subplot will be added. Defaults to the current + figure `pyplot.gcf()`. + + *args, **kwargs : + Will be passed on to the underlying ``Axes`` object creation. + """ import matplotlib.pyplot as plt axes_class = kwargs.pop("axes_class", None) host_subplot_class = host_subplot_class_factory(axes_class) From 25105cfe0d2a4a944672f69b01ceb47a68f6b583 Mon Sep 17 00:00:00 2001 From: none Date: Wed, 5 Aug 2015 03:58:59 +0200 Subject: [PATCH 5/5] Slightly more elegant. --- lib/mpl_toolkits/axes_grid1/parasite_axes.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/lib/mpl_toolkits/axes_grid1/parasite_axes.py b/lib/mpl_toolkits/axes_grid1/parasite_axes.py index 9af320ff0672..85c5a7b05585 100644 --- a/lib/mpl_toolkits/axes_grid1/parasite_axes.py +++ b/lib/mpl_toolkits/axes_grid1/parasite_axes.py @@ -499,9 +499,8 @@ def host_axes(*args, **kwargs): import matplotlib.pyplot as plt axes_class = kwargs.pop("axes_class", None) host_axes_class = host_axes_class_factory(axes_class) - if kwargs.get("figure") is not None: - fig = kwargs["figure"] - else: + fig = kwargs.get("figure", None) + if fig is None: fig = plt.gcf() ax = host_axes_class(fig, *args, **kwargs) fig.add_axes(ax) @@ -524,9 +523,8 @@ def host_subplot(*args, **kwargs): import matplotlib.pyplot as plt axes_class = kwargs.pop("axes_class", None) host_subplot_class = host_subplot_class_factory(axes_class) - if kwargs.get("figure") is not None: - fig = kwargs["figure"] - else: + fig = kwargs.get("figure", None) + if fig is None: fig = plt.gcf() ax = host_subplot_class(fig, *args, **kwargs) fig.add_subplot(ax)