|
33 | 33 | import matplotlib.contour as contour |
34 | 34 | import matplotlib.artist as martist |
35 | 35 |
|
| 36 | +import matplotlib.gridspec as gridspec |
| 37 | + |
| 38 | + |
36 | 39 | make_axes_kw_doc = ''' |
37 | 40 |
|
38 | 41 | ============= ==================================================== |
|
122 | 125 | *ax* |
123 | 126 | None | parent axes object from which space for a new |
124 | 127 | 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. |
125 | 133 |
|
126 | 134 |
|
127 | 135 | Additional keyword arguments are of two kinds: |
@@ -845,3 +853,97 @@ def make_axes(parent, **kw): |
845 | 853 | cax = fig.add_axes(pbcb) |
846 | 854 | cax.set_aspect(aspect, anchor=anchor, adjustable='box') |
847 | 855 | 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 |
0 commit comments