Skip to content

Fix locator/formatter setting when removing shared Axes #13983

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/matplotlib/axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -1616,7 +1616,7 @@ def set_major_formatter(self, formatter):
"""
if not isinstance(formatter, mticker.Formatter):
raise TypeError("formatter argument should be instance of "
"matplotlib.ticker.Formatter")
"matplotlib.ticker.Formatter")
self.isDefault_majfmt = False
self.major.formatter = formatter
formatter.set_axis(self)
Expand Down
22 changes: 18 additions & 4 deletions lib/matplotlib/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -1592,10 +1592,24 @@ def subplots(self, nrows=1, ncols=1, sharex=False, sharey=False,

def _remove_ax(self, ax):
def _reset_loc_form(axis):
axis.set_major_formatter(axis.get_major_formatter())
axis.set_major_locator(axis.get_major_locator())
axis.set_minor_formatter(axis.get_minor_formatter())
axis.set_minor_locator(axis.get_minor_locator())
# Set the formatters and locators to be associated with axis
# (where previously they may have been associated with another
# Axis isntance)
majfmt = axis.get_major_formatter()
if not majfmt.axis.isDefault_majfmt:
axis.set_major_formatter(majfmt)

majloc = axis.get_major_locator()
if not majloc.axis.isDefault_majloc:
axis.set_major_locator(majloc)

minfmt = axis.get_minor_formatter()
if not minfmt.axis.isDefault_minfmt:
axis.set_minor_formatter(minfmt)

minloc = axis.get_minor_locator()
if not minfmt.axis.isDefault_minloc:
axis.set_minor_locator(minloc)

def _break_share_link(ax, grouper):
siblings = grouper.get_siblings(ax)
Expand Down
21 changes: 20 additions & 1 deletion lib/matplotlib/tests/test_figure.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from datetime import datetime
from pathlib import Path
import platform

from matplotlib import rcParams
from matplotlib.testing.decorators import image_comparison, check_figures_equal
from matplotlib.axes import Axes
from matplotlib.ticker import AutoMinorLocator, FixedFormatter
from matplotlib.ticker import AutoMinorLocator, FixedFormatter, ScalarFormatter
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import matplotlib.gridspec as gridspec
Expand Down Expand Up @@ -461,3 +462,21 @@ def test_tightbbox():
# test bbox_extra_artists method...
assert abs(ax.get_tightbbox(renderer, bbox_extra_artists=[]).x1
- x1Nom * fig.dpi) < 2


def test_axes_removal():
# Check that units can set the formatter after an Axes removal
fig, axs = plt.subplots(1, 2, sharex=True)
axs[1].remove()
axs[0].plot([datetime(2000, 1, 1), datetime(2000, 2, 1)], [0, 1])
assert isinstance(axs[0].xaxis.get_major_formatter(),
mdates.AutoDateFormatter)

# Check that manually setting the formatter, then removing Axes keeps
# the set formatter.
fig, axs = plt.subplots(1, 2, sharex=True)
axs[1].xaxis.set_major_formatter(ScalarFormatter())
axs[1].remove()
axs[0].plot([datetime(2000, 1, 1), datetime(2000, 2, 1)], [0, 1])
assert isinstance(axs[0].xaxis.get_major_formatter(),
ScalarFormatter)