Skip to content

Commit

Permalink
2 tick minimum also for logscale (#372)
Browse files Browse the repository at this point in the history
* add test for minimum of 2 ticks for logscale

* bump version to 2.8.2

* create `_set_logticks` method that ensures at least 2 ticks per log axis, unfortunately appears to have to be called after every call to `plot_2d`

* try to fix `test_BarPlot` which actually should be unrelated to recent changes but for some reason acts up

* try alternative fix for `test_BarPlot` making use of `iloc`

* Revert "try alternative fix for `test_BarPlot` making use of `iloc`"

This reverts commit 50afec0.

* Revert "try to fix `test_BarPlot` which actually should be unrelated to recent changes but for some reason acts up"

This reverts commit eba68c8.

* remove comparison of bar yticks with barh xticks, since this test is not robust across different figure sizes, where often the yaxis is shorter than the xaxis, which caused failures in some CI settings

* remove `rcParams` modification from `test_logscale_ticks`, as this messes with other tests, make the test work with default settings instead

* Revert "remove comparison of bar yticks with barh xticks, since this test is not robust across different figure sizes, where often the yaxis is shorter than the xaxis, which caused failures in some CI settings"

This reverts commit 3bcc92d.
  • Loading branch information
lukashergt committed Apr 8, 2024
1 parent 80769c5 commit ef8a27e
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
anesthetic: nested sampling post-processing
===========================================
:Authors: Will Handley and Lukas Hergt
:Version: 2.8.3
:Version: 2.8.4
:Homepage: https://github.com/handley-lab/anesthetic
:Documentation: http://anesthetic.readthedocs.io/

Expand Down
2 changes: 1 addition & 1 deletion anesthetic/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '2.8.3'
__version__ = '2.8.4'
11 changes: 10 additions & 1 deletion anesthetic/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from matplotlib.axes import Axes
import matplotlib.cbook as cbook
import matplotlib.lines as mlines
from matplotlib.ticker import MaxNLocator, AutoMinorLocator
from matplotlib.ticker import MaxNLocator, AutoMinorLocator, LogLocator
from matplotlib.colors import LinearSegmentedColormap
from matplotlib.transforms import Affine2D
from anesthetic.utils import nest_level
Expand Down Expand Up @@ -388,6 +388,15 @@ def _set_scale(self):
if y in self._logy:
ax.set_yscale('log')

def _set_logticks(self):
for y, rows in self.iterrows():
for x, ax in rows.items():
if ax is not None:
if x in self._logx:
ax.xaxis.set_major_locator(LogLocator(numticks=3))
if y in self._logy:
ax.yaxis.set_major_locator(LogLocator(numticks=3))

@staticmethod
def _set_labels(axes, labels, **kwargs):
all_params = list(axes.columns) + list(axes.index)
Expand Down
2 changes: 2 additions & 0 deletions anesthetic/samples.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,8 @@ def plot_2d(self, axes=None, *args, **kwargs):
else:
ax.plot([], [])

axes._set_logticks()

return axes

plot_2d_default_kinds = {
Expand Down
19 changes: 19 additions & 0 deletions tests/test_samples.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,25 @@ def test_plot_logscale_2d(kind):
assert ax.twin.get_yscale() == 'linear'


def test_logscale_ticks():
np.random.seed(42)
ndim = 5
data = np.exp(10 * np.random.randn(200, ndim))
params = [f'a{i}' for i in range(ndim)]
fig, axes = make_2d_axes(params, logx=params, logy=params, upper=False)
samples = Samples(data, columns=params)
samples.plot_2d(axes)
for _, col in axes.iterrows():
for _, ax in col.items():
if ax is not None:
xlims = ax.get_xlim()
xticks = ax.get_xticks()
assert np.sum((xticks > xlims[0]) & (xticks < xlims[1])) > 1
ylims = ax.get_ylim()
yticks = ax.get_yticks()
assert np.sum((yticks > ylims[0]) & (yticks < ylims[1])) > 1


@pytest.mark.parametrize('k', ['hist_1d', 'hist'])
@pytest.mark.parametrize('b', ['scott', 10, np.logspace(-3, 0, 20)])
@pytest.mark.parametrize('r', [None, (1e-5, 1)])
Expand Down

0 comments on commit ef8a27e

Please sign in to comment.