|
38 | 38 |
|
39 | 39 | from matplotlib.axes import Axes, SubplotBase, subplot_class_factory
|
40 | 40 | from matplotlib.blocking_input import BlockingMouseInput, BlockingKeyMouseInput
|
41 |
| -from matplotlib.gridspec import GridSpec, SubplotSpec |
| 41 | +from matplotlib.gridspec import GridSpec |
42 | 42 | import matplotlib.legend as mlegend
|
43 | 43 | from matplotlib.patches import Rectangle
|
44 | 44 | from matplotlib.text import Text
|
@@ -814,52 +814,48 @@ def add_subplot(self, *args, **kwargs):
|
814 | 814 | fig.add_axes(ax)
|
815 | 815 | """
|
816 | 816 |
|
817 |
| - if not len(args) and 'rect' not in kwargs: |
818 |
| - cbook.warn_deprecated( |
819 |
| - "3.3", |
820 |
| - message="Calling add_axes() without argument is " |
821 |
| - "deprecated since %(since)s and will be removed %(removal)s. " |
822 |
| - "You may want to use add_subplot() instead.") |
823 |
| - return |
824 |
| - elif 'rect' in kwargs: |
825 |
| - if len(args): |
826 |
| - raise TypeError( |
827 |
| - "add_axes() got multiple values for argument 'rect'") |
828 |
| - args = (kwargs.pop('rect'), ) |
| 817 | + if 'figure' in kwargs: |
| 818 | + # Axes itself allows for a 'figure' kwarg, but since we want to |
| 819 | + # bind the created Axes to self, it is not allowed here. |
| 820 | + raise TypeError( |
| 821 | + "add_subplot() got an unexpected keyword argument 'figure'") |
829 | 822 |
|
830 |
| - # shortcut the projection "key" modifications later on, if an axes |
831 |
| - # with the exact args/kwargs exists, return it immediately. |
832 |
| - key = self._make_key(*args, **kwargs) |
833 |
| - ax = self._axstack.get(key) |
834 |
| - if ax is not None: |
835 |
| - self.sca(ax) |
836 |
| - return ax |
| 823 | + if len(args) == 1 and isinstance(args[0], SubplotBase): |
| 824 | + ax = args[0] |
| 825 | + if ax.get_figure() is not self: |
| 826 | + raise ValueError("The Subplot must have been created in " |
| 827 | + "the present figure") |
| 828 | + # make a key for the subplot (which includes the axes object id |
| 829 | + # in the hash) |
| 830 | + key = self._make_key(*args, **kwargs) |
837 | 831 |
|
838 |
| - if isinstance(args[0], Axes): |
839 |
| - a = args[0] |
840 |
| - if a.get_figure() is not self: |
841 |
| - raise ValueError( |
842 |
| - "The Axes must have been created in the present figure") |
843 | 832 | else:
|
844 |
| - rect = args[0] |
845 |
| - if not np.isfinite(rect).all(): |
846 |
| - raise ValueError('all entries in rect must be finite ' |
847 |
| - 'not {}'.format(rect)) |
| 833 | + if not args: |
| 834 | + args = (1, 1, 1) |
| 835 | + # Normalize correct ijk values to (i, j, k) here so that |
| 836 | + # add_subplot(111) == add_subplot(1, 1, 1). Invalid values will |
| 837 | + # trigger errors later (via SubplotSpec._from_subplot_args). |
| 838 | + if (len(args) == 1 and isinstance(args[0], Integral) |
| 839 | + and 100 <= args[0] <= 999): |
| 840 | + args = tuple(map(int, str(args[0]))) |
848 | 841 | projection_class, kwargs, key = \
|
849 | 842 | self._process_projection_requirements(*args, **kwargs)
|
850 |
| - |
851 |
| - # check that an axes of this type doesn't already exist, if it |
852 |
| - # does, set it as active and return it |
853 |
| - ax = self._axstack.get(key) |
854 |
| - if isinstance(ax, projection_class): |
855 |
| - self.sca(ax) |
856 |
| - return ax |
857 |
| - |
858 |
| - # create the new axes using the axes class given |
859 |
| - a = projection_class(self, rect, **kwargs) |
860 |
| - |
861 |
| - return self._add_axes_internal(key, a) |
862 |
| - |
| 843 | + ax = self._axstack.get(key) # search axes with this key in stack |
| 844 | + if ax is not None: |
| 845 | + if isinstance(ax, projection_class): |
| 846 | + # the axes already existed, so set it as active & return |
| 847 | + self.sca(ax) |
| 848 | + return ax |
| 849 | + else: |
| 850 | + # Undocumented convenience behavior: |
| 851 | + # subplot(111); subplot(111, projection='polar') |
| 852 | + # will replace the first with the second. |
| 853 | + # Without this, add_subplot would be simpler and |
| 854 | + # more similar to add_axes. |
| 855 | + self._axstack.remove(ax) |
| 856 | + ax = subplot_class_factory(projection_class)(self, *args, **kwargs) |
| 857 | + |
| 858 | + return self._add_axes_internal(key, ax) |
863 | 859 |
|
864 | 860 | def _add_axes_internal(self, key, ax):
|
865 | 861 | """Private helper for `add_axes` and `add_subplot`."""
|
|
0 commit comments