Skip to content
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

Remove unwanted minor tick label for log plots #450

Merged
merged 8 commits into from
Apr 21, 2023
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
4 changes: 4 additions & 0 deletions pyfar/plot/_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
LogLocatorITAToolbox,
MultipleFractionLocator,
MultipleFractionFormatter)
from matplotlib.ticker import NullFormatter


def _time(signal, dB=False, log_prefix=20, log_reference=1, unit="s",
Expand Down Expand Up @@ -97,6 +98,7 @@ def _freq(signal, dB=True, log_prefix=None, log_reference=1, freq_scale='log',
# set and format ticks
if freq_scale == 'log':
ax.xaxis.set_major_locator(LogLocatorITAToolbox())
ax.xaxis.set_minor_formatter(NullFormatter())
ax.xaxis.set_major_formatter(LogFormatterITAToolbox())

return ax
Expand Down Expand Up @@ -152,6 +154,7 @@ def _phase(signal, deg=False, unwrap=False, freq_scale='log', ax=None,
# set and format ticks
if freq_scale == 'log':
ax.xaxis.set_major_locator(LogLocatorITAToolbox())
ax.xaxis.set_minor_formatter(NullFormatter())
ax.xaxis.set_major_formatter(LogFormatterITAToolbox())

return ax
Expand Down Expand Up @@ -197,6 +200,7 @@ def _group_delay(signal, unit="s", freq_scale='log', ax=None, **kwargs):
# set and format ticks
if freq_scale == 'log':
ax.xaxis.set_major_locator(LogLocatorITAToolbox())
ax.xaxis.set_minor_formatter(NullFormatter())
ax.xaxis.set_major_formatter(LogFormatterITAToolbox())

return ax
Expand Down
5 changes: 5 additions & 0 deletions pyfar/plot/_two_d.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
LogLocatorITAToolbox,
MultipleFractionLocator,
MultipleFractionFormatter)
from matplotlib.ticker import NullFormatter


def _time_2d(signal, dB, log_prefix, log_reference, unit, indices,
Expand Down Expand Up @@ -101,6 +102,7 @@ def _freq_2d(signal, dB, log_prefix, log_reference, freq_scale, indices,
ax_scale[0](freq_scale)
if freq_scale == "log":
axis[0].set_major_locator(LogLocatorITAToolbox())
axis[0].set_minor_formatter(NullFormatter())
axis[0].set_major_formatter(LogFormatterITAToolbox())

# color limits
Expand Down Expand Up @@ -162,6 +164,7 @@ def _phase_2d(signal, deg, unwrap, freq_scale, indices, orientation, method,
ax_scale[0](freq_scale)
if freq_scale == "log":
axis[0].set_major_locator(LogLocatorITAToolbox())
axis[0].set_minor_formatter(NullFormatter())
axis[0].set_major_formatter(LogFormatterITAToolbox())

# colorbar
Expand Down Expand Up @@ -217,6 +220,7 @@ def _group_delay_2d(signal, unit, freq_scale, indices, orientation, method,
ax_scale[0](freq_scale)
if freq_scale == "log":
axis[0].set_major_locator(LogLocatorITAToolbox())
axis[0].set_minor_formatter(NullFormatter())
axis[0].set_major_formatter(LogFormatterITAToolbox())

# color limits
Expand Down Expand Up @@ -371,6 +375,7 @@ def _spectrogram(signal, dB=True, log_prefix=None, log_reference=1,
if freq_scale == 'log':
ax[0].set_yscale('symlog')
ax[0].yaxis.set_major_locator(LogLocatorITAToolbox())
ax[0].yaxis.set_minor_formatter(NullFormatter())
ax[0].yaxis.set_major_formatter(LogFormatterITAToolbox())
ax[0].grid(ls='dotted', color='white')

Expand Down
67 changes: 67 additions & 0 deletions tests/test_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,73 @@ def test_title_style(style, handsome_signal):
plt.close('all')


@pytest.mark.parametrize("function", [
(plot.freq), (plot.phase), (plot.group_delay)])
@pytest.mark.parametrize("limits", [[20, 20e3], [50, 425]])
def test_log_tick_labels(function, limits, noise):
"""
Test that only major tick labels are shown for logarithmic frequency
axis in line plots.
"""
create_figure()
ax = function(noise)

ax.set_xlim(limits)

major_label_test = [
bool(r.get_text()) for r in ax.get_xticklabels(which="major")]
minor_label_test = [
bool(r.get_text()) for r in ax.get_xticklabels(which="minor")]

assert all(major_label_test)
assert not any(minor_label_test)


@pytest.mark.parametrize(
"function", [(plot.freq_2d), (plot.phase_2d), (plot.group_delay_2d)]
)
@pytest.mark.parametrize("limits", [[20, 20e3], [50, 425]])
def test_2d_log_tick_labels(function, limits, handsome_signal_2d):
"""
Test that only major tick labels are shown for logarithmic frequency
axis in 2d plots.
"""
create_figure()
axs = function(handsome_signal_2d, freq_scale="log")
ax = axs[0][0]

ax.set_ylim(limits)

major_label_test = [
bool(r.get_text()) for r in ax.get_yticklabels(which="major")]
minor_label_test = [
bool(r.get_text()) for r in ax.get_yticklabels(which="minor")]

assert all(major_label_test)
assert not any(minor_label_test)


@pytest.mark.parametrize("limits", [[20, 20e3], [50, 425]])
def test_spectrogram_log_tick_labels(limits, noise):
"""
Test that only major tick labels are shown for logarithmic frequency
axis in spectrograms.
"""
create_figure()
axs = plot.spectrogram(noise, freq_scale="log")
ax = axs[0][0]

ax.set_ylim(limits)

major_label_test = [
bool(r.get_text()) for r in ax.get_yticklabels(which="major")]
minor_label_test = [
bool(r.get_text()) for r in ax.get_yticklabels(which="minor")]

assert all(major_label_test)
assert not any(minor_label_test)


@pytest.mark.parametrize('rcParams, value', [
['lines.linestyle', ':'],
['axes.facecolor', 'black'],
Expand Down