From 6640e6063ff92893dc554df47f8f69218b44f81e Mon Sep 17 00:00:00 2001 From: saranti Date: Sun, 14 Jan 2024 20:16:00 +1100 Subject: [PATCH 1/2] fix minorticks_on --- lib/matplotlib/tests/test_ticker.py | 30 +++++++++++++++++++++++++++++ lib/matplotlib/ticker.py | 2 +- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/tests/test_ticker.py b/lib/matplotlib/tests/test_ticker.py index 12eafba9ea2b..267178662865 100644 --- a/lib/matplotlib/tests/test_ticker.py +++ b/lib/matplotlib/tests/test_ticker.py @@ -12,6 +12,8 @@ import matplotlib.pyplot as plt import matplotlib.ticker as mticker +import random + class TestMaxNLocator: basic_data = [ @@ -1796,3 +1798,31 @@ def test_set_offset_string(formatter): assert formatter.get_offset() == '' formatter.set_offset_string('mpl') assert formatter.get_offset() == 'mpl' + + +def test_minorticks_on_multi_fig(): + """ + Turning on minor gridlines in a multi-Axes Figure + that contains more than one boxplot and shares the x-axis + should not raise an exception. + """ + fig, ax = plt.subplots(sharex=True, ncols=2, nrows=2) + + def values(): + return [random.random() for _ in range(9)] + + for x in range(3): + ax[0, 0].boxplot(values(), positions=[x]) + ax[0, 1].boxplot(values(), positions=[x]) + ax[1, 0].boxplot(values(), positions=[x]) + ax[1, 1].boxplot(values(), positions=[x]) + + for a in ax.flatten(): + a.grid(which="major") + a.grid(which="minor", linestyle="--") + a.minorticks_on() + fig.canvas.draw() + + assert all(a.get_xgridlines() for a in ax.flatten()) + assert all((isinstance(a.xaxis.get_minor_locator(), mpl.ticker.AutoMinorLocator) + for a in ax.flatten())) diff --git a/lib/matplotlib/ticker.py b/lib/matplotlib/ticker.py index 10ac8cad6ebc..1e9f0892c322 100644 --- a/lib/matplotlib/ticker.py +++ b/lib/matplotlib/ticker.py @@ -2903,7 +2903,7 @@ def __call__(self): _api.warn_external('AutoMinorLocator does not work on logarithmic scales') return [] - majorlocs = self.axis.get_majorticklocs() + majorlocs = np.unique(self.axis.get_majorticklocs()) if len(majorlocs) < 2: # Need at least two major ticks to find minor tick locations. # TODO: Figure out a way to still be able to display minor ticks with less From 4a3df94d1829e581dad6c2f3214f1cc0437014e9 Mon Sep 17 00:00:00 2001 From: saranti Date: Wed, 17 Jan 2024 16:10:21 +1100 Subject: [PATCH 2/2] Simplify test --- lib/matplotlib/tests/test_ticker.py | 35 +++++++++++------------------ 1 file changed, 13 insertions(+), 22 deletions(-) diff --git a/lib/matplotlib/tests/test_ticker.py b/lib/matplotlib/tests/test_ticker.py index 267178662865..fcfdf8ee92f5 100644 --- a/lib/matplotlib/tests/test_ticker.py +++ b/lib/matplotlib/tests/test_ticker.py @@ -12,8 +12,6 @@ import matplotlib.pyplot as plt import matplotlib.ticker as mticker -import random - class TestMaxNLocator: basic_data = [ @@ -1806,23 +1804,16 @@ def test_minorticks_on_multi_fig(): that contains more than one boxplot and shares the x-axis should not raise an exception. """ - fig, ax = plt.subplots(sharex=True, ncols=2, nrows=2) - - def values(): - return [random.random() for _ in range(9)] - - for x in range(3): - ax[0, 0].boxplot(values(), positions=[x]) - ax[0, 1].boxplot(values(), positions=[x]) - ax[1, 0].boxplot(values(), positions=[x]) - ax[1, 1].boxplot(values(), positions=[x]) - - for a in ax.flatten(): - a.grid(which="major") - a.grid(which="minor", linestyle="--") - a.minorticks_on() - fig.canvas.draw() - - assert all(a.get_xgridlines() for a in ax.flatten()) - assert all((isinstance(a.xaxis.get_minor_locator(), mpl.ticker.AutoMinorLocator) - for a in ax.flatten())) + fig, ax = plt.subplots() + + ax.boxplot(np.arange(10), positions=[0]) + ax.boxplot(np.arange(10), positions=[0]) + ax.boxplot(np.arange(10), positions=[1]) + + ax.grid(which="major") + ax.grid(which="minor") + ax.minorticks_on() + fig.draw_without_rendering() + + assert ax.get_xgridlines() + assert isinstance(ax.xaxis.get_minor_locator(), mpl.ticker.AutoMinorLocator)