|
6 | 6 | LogTransform, InvertedLogTransform,
|
7 | 7 | SymmetricalLogTransform)
|
8 | 8 | import matplotlib.scale as mscale
|
9 |
| -from matplotlib.ticker import AsinhLocator, LogFormatterSciNotation |
| 9 | +from matplotlib.ticker import ( |
| 10 | + AsinhLocator, AutoLocator, LogFormatterSciNotation, |
| 11 | + NullFormatter, NullLocator, ScalarFormatter |
| 12 | +) |
10 | 13 | from matplotlib.testing.decorators import check_figures_equal, image_comparison
|
| 14 | +from matplotlib.transforms import IdentityTransform |
11 | 15 |
|
12 | 16 | import numpy as np
|
13 | 17 | from numpy.testing import assert_allclose
|
@@ -295,3 +299,75 @@ def test_bad_scale(self):
|
295 | 299 | AsinhScale(axis=None, linear_width=-1)
|
296 | 300 | s0 = AsinhScale(axis=None, )
|
297 | 301 | s1 = AsinhScale(axis=None, linear_width=3.0)
|
| 302 | + |
| 303 | + |
| 304 | +def test_custom_scale_without_axis(): |
| 305 | + """ |
| 306 | + Test that one can register and use custom scales that don't take an *axis* param. |
| 307 | + """ |
| 308 | + class CustomTransform(IdentityTransform): |
| 309 | + pass |
| 310 | + |
| 311 | + class CustomScale(mscale.ScaleBase): |
| 312 | + name = "custom" |
| 313 | + |
| 314 | + # Important: __init__ has no *axis* parameter |
| 315 | + def __init__(self): |
| 316 | + self._transform = CustomTransform() |
| 317 | + |
| 318 | + def get_transform(self): |
| 319 | + return self._transform |
| 320 | + |
| 321 | + def set_default_locators_and_formatters(self, axis): |
| 322 | + axis.set_major_locator(AutoLocator()) |
| 323 | + axis.set_major_formatter(ScalarFormatter()) |
| 324 | + axis.set_minor_locator(NullLocator()) |
| 325 | + axis.set_minor_formatter(NullFormatter()) |
| 326 | + |
| 327 | + try: |
| 328 | + mscale.register_scale(CustomScale) |
| 329 | + fig, ax = plt.subplots() |
| 330 | + ax.set_xscale('custom') |
| 331 | + assert isinstance(ax.xaxis.get_transform(), CustomTransform) |
| 332 | + finally: |
| 333 | + # cleanup - there's no public unregister_scale() |
| 334 | + del mscale._scale_mapping["custom"] |
| 335 | + del mscale._scale_has_axis_parameter["custom"] |
| 336 | + |
| 337 | + |
| 338 | +def test_custom_scale_with_axis(): |
| 339 | + """ |
| 340 | + Test that one can still register and use custom scales with an *axis* |
| 341 | + parameter, but that registering issues a pending-deprecation warning. |
| 342 | + """ |
| 343 | + class CustomTransform(IdentityTransform): |
| 344 | + pass |
| 345 | + |
| 346 | + class CustomScale(mscale.ScaleBase): |
| 347 | + name = "custom" |
| 348 | + |
| 349 | + # Important: __init__ still has the *axis* parameter |
| 350 | + def __init__(self, axis): |
| 351 | + self._transform = CustomTransform() |
| 352 | + |
| 353 | + def get_transform(self): |
| 354 | + return self._transform |
| 355 | + |
| 356 | + def set_default_locators_and_formatters(self, axis): |
| 357 | + axis.set_major_locator(AutoLocator()) |
| 358 | + axis.set_major_formatter(ScalarFormatter()) |
| 359 | + axis.set_minor_locator(NullLocator()) |
| 360 | + axis.set_minor_formatter(NullFormatter()) |
| 361 | + |
| 362 | + try: |
| 363 | + with pytest.warns( |
| 364 | + PendingDeprecationWarning, |
| 365 | + match=r"'axis' parameter .* is pending-deprecated"): |
| 366 | + mscale.register_scale(CustomScale) |
| 367 | + fig, ax = plt.subplots() |
| 368 | + ax.set_xscale('custom') |
| 369 | + assert isinstance(ax.xaxis.get_transform(), CustomTransform) |
| 370 | + finally: |
| 371 | + # cleanup - there's no public unregister_scale() |
| 372 | + del mscale._scale_mapping["custom"] |
| 373 | + del mscale._scale_has_axis_parameter["custom"] |
0 commit comments