Skip to content

Improve symlog axis ticks #27310

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

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
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
6 changes: 4 additions & 2 deletions lib/matplotlib/scale.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ class SymmetricalLogScale(ScaleBase):
"""
name = 'symlog'

def __init__(self, axis, *, base=10, linthresh=2, subs=None, linscale=1):
def __init__(self, axis, *, base=10, linthresh=2, subs='auto', linscale=1):
self._transform = SymmetricalLogTransform(base, linthresh, linscale)
self.subs = subs

Expand All @@ -454,7 +454,9 @@ def set_default_locators_and_formatters(self, axis):
axis.set_major_formatter(LogFormatterSciNotation(self.base))
axis.set_minor_locator(SymmetricalLogLocator(self.get_transform(),
self.subs))
axis.set_minor_formatter(NullFormatter())
axis.set_minor_formatter(
LogFormatterSciNotation(self.base,
labelOnlyBase=(self.subs != 'auto')))

def get_transform(self):
"""Return the `.SymmetricalLogTransform` associated with this scale."""
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/scale.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class SymmetricalLogScale(ScaleBase):
*,
base: float = ...,
linthresh: float = ...,
subs: Iterable[int] | None = ...,
subs: Iterable[int] | Literal["auto", "all"] | None = ...,
linscale: float = ...
) -> None: ...
@property
Expand Down
Binary file modified lib/matplotlib/tests/baseline_images/test_axes/symlog.pdf
Binary file not shown.
Binary file modified lib/matplotlib/tests/baseline_images/test_axes/symlog2.pdf
Binary file not shown.
28 changes: 15 additions & 13 deletions lib/matplotlib/tests/test_ticker.py
Original file line number Diff line number Diff line change
Expand Up @@ -601,45 +601,47 @@ def test_set_params(self):
class TestSymmetricalLogLocator:
def test_set_params(self):
"""
Create symmetrical log locator with default subs =[1.0] numticks = 15,
Create symmetrical log locator with default subs=[1.0] numticks='auto',
and change it to something else.
See if change was successful.
Should not exception.
"""
sym = mticker.SymmetricalLogLocator(base=10, linthresh=1)
sym = mticker.SymmetricalLogLocator(base=10, linthresh=1, linscale=1)
sym.set_params(subs=[2.0], numticks=8)
assert sym._subs == [2.0]
assert sym.numticks == 8

@pytest.mark.parametrize(
'vmin, vmax, expected',
[
(0, 1, [0, 1]),
(-1, 1, [-1, 0, 1]),
(0, 1, [-1, 0, 1, 10]),
(-1, 1, [-10, -1, 0, 1, 10]),
],
)
def test_values(self, vmin, vmax, expected):
# https://github.com/matplotlib/matplotlib/issues/25945
sym = mticker.SymmetricalLogLocator(base=10, linthresh=1)
sym = mticker.SymmetricalLogLocator(base=10, linthresh=1, linscale=1)
ticks = sym.tick_values(vmin=vmin, vmax=vmax)
assert_array_equal(ticks, expected)

def test_subs(self):
sym = mticker.SymmetricalLogLocator(base=10, linthresh=1, subs=[2.0, 4.0])
sym = mticker.SymmetricalLogLocator(base=10, linthresh=1, linscale=1,
subs=[2.0, 4.0])
sym.create_dummy_axis()
sym.axis.set_view_interval(-10, 10)
assert (sym() == [-20., -40., -2., -4., 0., 2., 4., 20., 40.]).all()
assert (sym() == [-400., -200., -40., -20., -4., -2., -0.4, -0.2, -0.1, 0.,
0.1, 0.2, 0.4, 2., 4., 20., 40., 200., 400.]).all()

def test_extending(self):
sym = mticker.SymmetricalLogLocator(base=10, linthresh=1)
sym = mticker.SymmetricalLogLocator(base=10, linthresh=1, linscale=1)
sym.create_dummy_axis()
sym.axis.set_view_interval(8, 9)
assert (sym() == [1.0]).all()
sym.axis.set_view_interval(8, 12)
assert (sym() == [1.0, 10.0]).all()
assert sym.view_limits(10, 10) == (1, 100)
assert sym.view_limits(-10, -10) == (-100, -1)
assert sym.view_limits(0, 0) == (-0.001, 0.001)
sym.axis.set_view_interval(8, 12)
assert (sym() == [1.0, 10.0, 100.0]).all()
assert sym.view_limits(10, 10) == (1.0, 100.0)
assert sym.view_limits(-10, -10) == (-100.0, -1.0)
assert sym.view_limits(0, 0) == (-1.0, 1.0)


class TestAsinhLocator:
Expand Down
Loading