Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4f5e73d

Browse files
committed
Merge pull request #126 from leejjoon/matplotlib
--- When pyplot.colorbar is called (w/ cax=None), it modifies the position of the parent axes and creates a new axes (cax) for a colorbar. However, issues can arise if the parent axes is an instance of Subplot because cax is created as an instance of Axes. This patch introduces a new keyword parameter "use_gridspec". If True (default is False) and if the parent axes is an instance of Subplot, cax is created also as an instance of Subplot. This patch is partly motivated to support tight_layout. Since cax is a subplot, tight_layout works.
2 parents de39c79 + 02fbdbc commit 4f5e73d

2 files changed

Lines changed: 107 additions & 1 deletion

File tree

lib/matplotlib/colorbar.py

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333
import matplotlib.contour as contour
3434
import matplotlib.artist as martist
3535

36+
import matplotlib.gridspec as gridspec
37+
38+
3639
make_axes_kw_doc = '''
3740
3841
============= ====================================================
@@ -122,6 +125,11 @@
122125
*ax*
123126
None | parent axes object from which space for a new
124127
colorbar axes will be stolen
128+
*use_gridspec*
129+
False | If *cax* is None, a new *cax* is created as an instance of
130+
Axes. If *ax* is an instance of Subplot and *use_gridspec* is True,
131+
*cax* is created as an instance of Subplot using the
132+
grid_spec module.
125133
126134
127135
Additional keyword arguments are of two kinds:
@@ -845,3 +853,97 @@ def make_axes(parent, **kw):
845853
cax = fig.add_axes(pbcb)
846854
cax.set_aspect(aspect, anchor=anchor, adjustable='box')
847855
return cax, kw
856+
857+
858+
@docstring.Substitution(make_axes_kw_doc)
859+
def make_axes_gridspec(parent, **kw):
860+
'''
861+
Resize and reposition a parent axes, and return a child axes
862+
suitable for a colorbar. This function is similar to
863+
make_axes. Prmary differences are
864+
865+
* *make_axes_gridspec* should only be used with a subplot parent.
866+
867+
* *make_axes* creates an instance of Axes. *make_axes_gridspec*
868+
creates an instance of Subplot.
869+
870+
* *make_axes* updates the position of the
871+
parent. *make_axes_gridspec* replaces the grid_spec attribute
872+
of the parent with a new one.
873+
874+
While this function is meant to be compatible with *make_axes*,
875+
there could be some minor differences.::
876+
877+
cax, kw = make_axes_gridspec(parent, **kw)
878+
879+
Keyword arguments may include the following (with defaults):
880+
881+
*orientation*
882+
'vertical' or 'horizontal'
883+
884+
%s
885+
886+
All but the first of these are stripped from the input kw set.
887+
888+
Returns (cax, kw), the child axes and the reduced kw dictionary.
889+
'''
890+
891+
orientation = kw.setdefault('orientation', 'vertical')
892+
fraction = kw.pop('fraction', 0.15)
893+
shrink = kw.pop('shrink', 1.0)
894+
aspect = kw.pop('aspect', 20)
895+
896+
x1 = 1.0-fraction
897+
898+
# for shrinking
899+
pad_s = (1.-shrink)*0.5
900+
wh_ratios = [pad_s, shrink, pad_s]
901+
902+
gs_from_subplotspec = gridspec.GridSpecFromSubplotSpec
903+
if orientation == 'vertical':
904+
pad = kw.pop('pad', 0.05)
905+
wh_space = 2*pad/(1-pad)
906+
907+
gs = gs_from_subplotspec(1, 2,
908+
subplot_spec=parent.get_subplotspec(),
909+
wspace=wh_space,
910+
width_ratios=[x1-pad, fraction]
911+
)
912+
913+
gs2 = gs_from_subplotspec(3, 1,
914+
subplot_spec=gs[1],
915+
hspace=0.,
916+
height_ratios=wh_ratios,
917+
)
918+
919+
anchor = (0.0, 0.5)
920+
panchor = (1.0, 0.5)
921+
else:
922+
pad = kw.pop('pad', 0.15)
923+
wh_space = 2*pad/(1-pad)
924+
925+
gs = gs_from_subplotspec(2, 1,
926+
subplot_spec=parent.get_subplotspec(),
927+
hspace=wh_space,
928+
height_ratios=[x1-pad, fraction]
929+
)
930+
931+
gs2 = gs_from_subplotspec(1, 3,
932+
subplot_spec=gs[1],
933+
wspace=0.,
934+
width_ratios=wh_ratios,
935+
)
936+
937+
aspect = 1.0/aspect
938+
anchor = (0.5, 1.0)
939+
panchor = (0.5, 0.0)
940+
941+
parent.set_subplotspec(gs[0])
942+
parent.update_params()
943+
parent.set_position(parent.figbox)
944+
parent.set_anchor(panchor)
945+
946+
fig = parent.get_figure()
947+
cax = fig.add_subplot(gs2[1])
948+
cax.set_aspect(aspect, anchor=anchor, adjustable='box')
949+
return cax, kw

lib/matplotlib/figure.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1176,8 +1176,12 @@ def colorbar(self, mappable, cax=None, ax=None, **kw):
11761176
"""
11771177
if ax is None:
11781178
ax = self.gca()
1179+
use_gridspec = kw.pop("use_gridspec", False)
11791180
if cax is None:
1180-
cax, kw = cbar.make_axes(ax, **kw)
1181+
if use_gridspec and isinstance(ax, SubplotBase):
1182+
cax, kw = cbar.make_axes_gridspec(ax, **kw)
1183+
else:
1184+
cax, kw = cbar.make_axes(ax, **kw)
11811185
cax.hold(True)
11821186
cb = cbar.Colorbar(cax, mappable, **kw)
11831187

0 commit comments

Comments
 (0)