8000 Merge pull request #12131 from tacaswell/API_dont_break_cartopy · matplotlib/matplotlib@074cee4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 074cee4

Browse files
authored
Merge pull request #12131 from tacaswell/API_dont_break_cartopy
Fixes currently release version of cartopy
2 parents d0e408e + f5306b8 commit 074cee4

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

lib/matplotlib/axes/_subplots.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,18 @@ def _make_twin_axes(self, *kl, **kwargs):
187187
return ax2
188188

189189

190+
# this here to support cartopy which was using a private part of the
191+
# API to register their Axes subclasses.
192+
193+
# In 3.1 this should be changed to a dict subclass that warns on use
194+
# In 3.3 to a dict subclass that raises a useful exception on use
195+
# In 3.4 should be removed
196+
197+
# The slow timeline is to give cartopy enough time to get several
198+
# release out before we break them.
199+
_subplot_classes = {}
200+
201+
190202
@functools.lru_cache(None)
191203
def subplot_class_factory(axes_class=None):
192204
"""
@@ -198,9 +210,16 @@ def subplot_class_factory(axes_class=None):
198210
"""
199211
if axes_class is None:
200212
axes_class = Axes
201-
return type("%sSubplot" % axes_class.__name__,
202-
(SubplotBase, axes_class),
203-
{'_axes_class': axes_class})
213+
try:
214+
# Avoid creating two different instances of GeoAxesSubplot...
215+
# Only a temporary backcompat fix. This should be removed in
216+
# 3.4
217+
return next(cls for cls in SubplotBase.__subclasses__()
218+
if cls.__bases__ == (SubplotBase, axes_class))
219+
except StopIteration:
ECEB 220+
return type("%sSubplot" % axes_class.__name__,
221+
(SubplotBase, axes_class),
222+
{'_axes_class': axes_class})
204223

205224

206225
# This is provided for backward compatibility

lib/matplotlib/tests/test_axes.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5789,3 +5789,21 @@ def test_spines_properbbox_after_zoom():
57895789
None, False, False)
57905790
bb2 = ax.spines['bottom'].get_window_extent(fig.canvas.get_renderer())
57915791
np.testing.assert_allclose(bb.get_points(), bb2.get_points(), rtol=1e-6)
5792+
5793+
5794+
def test_cartopy_backcompat():
5795+
import matplotlib
5796+
import matplotlib.axes
5797+
import matplotlib.axes._subplots
5798+
5799+
class Dummy(matplotlib.axes.Axes):
5800+
...
5801+
5802+
class DummySubplot(matplotlib.axes.SubplotBase, Dummy):
5803+
_axes_class = Dummy
5804+
5805+
matplotlib.axes._subplots._subplot_classes[Dummy] = DummySubplot
5806+
5807+
FactoryDummySubplot = matplotlib.axes.subplot_class_factory(Dummy)
5808+
5809+
assert DummySubplot is FactoryDummySubplot

0 commit comments

Comments
 (0)
0