Improve symlog axis ticks by schtandard · Pull Request #27310 · matplotlib/matplotlib · GitHub
Skip to content
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: 6 additions & 0 deletions doc/api/next_api_changes/behavior/27310-BW.rst
7 changes: 7 additions & 0 deletions doc/release/next_whats_new/improved_symlog_ticks.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Improved tick placement for ``symlog`` axes
-------------------------------------------

The placement of ticks for ``symlog`` axes has been improved. Ticks are now
placed identically to ``log`` axes in the logarithmic part with a reasonable
extension of this behavior to the linear part of the axis. Axes with too few
ticks or spurious ticks should now be avoided.
9 changes: 7 additions & 2 deletions lib/matplotlib/scale.py
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,9 @@ class SymmetricalLogScale(ScaleBase):
For example, in a log10 scale: ``[2, 3, 4, 5, 6, 7, 8, 9]`` will place
8 logarithmically spaced minor ticks between each major tick.

.. versionchanged:: 3.12
The default value is now ``'auto'``.

linscale : float, optional
This allows the linear range ``(-linthresh, linthresh)`` to be
stretched relative to the logarithmic range. Its value is the number of
Expand All @@ -610,7 +613,7 @@ class SymmetricalLogScale(ScaleBase):
name = 'symlog'

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

Expand All @@ -624,7 +627,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 @@ -114,7 +114,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.
21 changes: 12 additions & 9 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1377,19 +1377,22 @@ def test_fill_between_interpolate_nan():
interpolate=True, alpha=0.5)


# test_symlog and test_symlog2 used to have baseline images in all three
# formats, but the png and svg baselines got invalidated by the removal of
# minor tick overstriking.
@image_comparison(['symlog.pdf'], style='mpl20')
def test_symlog():
fig, axs = plt.subplots(2, figsize=[6.4, 9.6])

x = np.array([0, 1, 2, 4, 6, 9, 12, 24])
y = np.array([1000000, 500000, 100000, 100, 5, 0, 0, 0])

fig, ax = plt.subplots()
ax.plot(x, y)
ax.set_yscale('symlog')
ax.set_xscale('linear')
ax.set_ylim(-1, 10000000)
axs[0].plot(x, y)
axs[0].set_yscale('symlog')
axs[0].set_xscale('linear')
axs[0].set_ylim(-1, 10000000)

x = np.linspace(-25, 25, 101)
y = np.linspace(0, 2500, 101)
axs[1].plot(x, y)
axs[1].set_xscale('symlog')
axs[1].set_yscale('symlog', linthresh=1, linscale=2)


@image_comparison(['symlog2.pdf'], remove_text=True, style='_classic_test')
Expand Down
24 changes: 13 additions & 11 deletions lib/matplotlib/tests/test_ticker.py
Loading
Loading