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

Skip to content

Commit 31a103a

Browse files
authored
Merge pull request #13983 from dstansby/fmt-reset
Fix locator/formatter setting when removing shared Axes
2 parents 095caa1 + 4a3c549 commit 31a103a

File tree

3 files changed

+39
-6
lines changed

3 files changed

+39
-6
lines changed

lib/matplotlib/axis.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1616,7 +1616,7 @@ def set_major_formatter(self, formatter):
16161616
"""
16171617
if not isinstance(formatter, mticker.Formatter):
16181618
raise TypeError("formatter argument should be instance of "
1619-
"matplotlib.ticker.Formatter")
1619+
"matplotlib.ticker.Formatter")
16201620
self.isDefault_majfmt = False
16211621
self.major.formatter = formatter
16221622
formatter.set_axis(self)

lib/matplotlib/figure.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1592,10 +1592,24 @@ def subplots(self, nrows=1, ncols=1, sharex=False, sharey=False,
15921592

15931593
def _remove_ax(self, ax):
15941594
def _reset_loc_form(axis):
1595-
axis.set_major_formatter(axis.get_major_formatter())
1596-
axis.set_major_locator(axis.get_major_locator())
1597-
axis.set_minor_formatter(axis.get_minor_formatter())
1598-
axis.set_minor_locator(axis.get_minor_locator())
1595+
# Set the formatters and locators to be associated with axis
1596+
# (where previously they may have been associated with another
1597+
# Axis isntance)
1598+
majfmt = axis.get_major_formatter()
1599+
if not majfmt.axis.isDefault_majfmt:
1600+
axis.set_major_formatter(majfmt)
1601+
1602+
majloc = axis.get_major_locator()
1603+
if not majloc.axis.isDefault_majloc:
1604+
axis.set_major_locator(majloc)
1605+
1606+
minfmt = axis.get_minor_formatter()
1607+
if not minfmt.axis.isDefault_minfmt:
1608+
axis.set_minor_formatter(minfmt)
1609+
1610+
minloc = axis.get_minor_locator()
1611+
if not minfmt.axis.isDefault_minloc:
1612+
axis.set_minor_locator(minloc)
15991613

16001614
def _break_share_link(ax, grouper):
16011615
siblings = grouper.get_siblings(ax)

lib/matplotlib/tests/test_figure.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
from datetime import datetime
12
from pathlib import Path
23
import platform
34

45
from matplotlib import rcParams
56
from matplotlib.testing.decorators import image_comparison, check_figures_equal
67
from matplotlib.axes import Axes
7-
from matplotlib.ticker import AutoMinorLocator, FixedFormatter
8+
from matplotlib.ticker import AutoMinorLocator, FixedFormatter, ScalarFormatter
89
import matplotlib.pyplot as plt
910
import matplotlib.dates as mdates
1011
import matplotlib.gridspec as gridspec
@@ -461,3 +462,21 @@ def test_tightbbox():
461462
# test bbox_extra_artists method...
462463
assert abs(ax.get_tightbbox(renderer, bbox_extra_artists=[]).x1
463464
- x1Nom * fig.dpi) < 2
465+
466+
467+
def test_axes_removal():
468+
# Check that units can set the formatter after an Axes removal
469+
fig, axs = plt.subplots(1, 2, sharex=True)
470+
axs[1].remove()
471+
axs[0].plot([datetime(2000, 1, 1), datetime(2000, 2, 1)], [0, 1])
472+
assert isinstance(axs[0].xaxis.get_major_formatter(),
473+
mdates.AutoDateFormatter)
474+
475+
# Check that manually setting the formatter, then removing Axes keeps
476+
# the set formatter.
477+
fig, axs = plt.subplots(1, 2, sharex=True)
478+
axs[1].xaxis.set_major_formatter(ScalarFormatter())
479+
axs[1].remove()
480+
axs[0].plot([datetime(2000, 1, 1), datetime(2000, 2, 1)], [0, 1])
481+
assert isinstance(axs[0].xaxis.get_major_formatter(),
482+
ScalarFormatter)

0 commit comments

Comments
 (0)