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

Enhancement: phase plot ticks #559

Merged
merged 8 commits into from Apr 10, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
50 changes: 42 additions & 8 deletions pyfar/plot/_line.py
Expand Up @@ -126,16 +126,50 @@ def _phase(signal, deg=False, unwrap=False, freq_scale='log', ax=None,

# y-axis formatting
y_margin = 5 if deg else np.radians(5)
if not deg and (not unwrap or unwrap == "360"):
# nice tick formatting is not done for unwrap=True. In this case
# it can create 1000 or more ticks.
ax.yaxis.set_major_locator(MultipleFractionLocator(np.pi, 2))
ax.yaxis.set_minor_locator(MultipleFractionLocator(np.pi, 6))
ax.yaxis.set_major_formatter(MultipleFractionFormatter(
nominator=1, denominator=2, base=np.pi, base_str=r'\pi'))

ymin = np.nanmin(phase_data) - y_margin # more elegant solution possible?
ymax = np.nanmax(phase_data) + y_margin
if unwrap == '360':
ymin = -y_margin
ymax = 360+y_margin if deg else 2*np.pi+y_margin

if not deg:
if ymax-ymin < np.pi:
# set yrange to +/- pi/2
ymin = np.pi/2+y_margin
ymax = -np.pi/2-y_margin
# major ticks at multiples of pi/4
ax.yaxis.set_major_locator(MultipleFractionLocator(np.pi, 4))
ax.yaxis.set_major_formatter(MultipleFractionFormatter(
nominator=1, denominator=4, base=np.pi, base_str=r'\pi'))
# minor ticks at multiples of pi/8
ax.yaxis.set_minor_locator(MultipleFractionLocator(np.pi, 8))
elif unwrap is True:
if ymax-ymin < 20*np.pi:
# ticks at at multiples of 2 pi / pi
# if yrange is smaller than 20 pi
nmaj = 2
nmin = 1
else:
# major ticks at multiples of x*20*pi
# with x determined to maximum 10 intervals
nmaj = np.ceil((ymax-ymin)/np.pi/9/20)*20
# minor ticks at half of major ticks,
# but rounded to multiples of 10*pi
nmin = np.round(nmaj/2/10)*10
ax.yaxis.set_major_locator(
MultipleFractionLocator(nmaj, 1, np.pi))
ax.yaxis.set_major_formatter(
MultipleFractionFormatter(nmaj, 1, np.pi, r'\pi'))
ax.yaxis.set_minor_locator(
MultipleFractionLocator(nmin/2, 1, np.pi))
else:
# unwrap is False or "360"
# major ticks at multiples of pi/2
ax.yaxis.set_major_locator(MultipleFractionLocator(np.pi, 2))
ax.yaxis.set_major_formatter(MultipleFractionFormatter(
nominator=1, denominator=2, base=np.pi, base_str=r'\pi'))
# minor ticks at multiples of pi/4
ax.yaxis.set_minor_locator(MultipleFractionLocator(np.pi, 4))

# prepare figure
ax.set_xlabel("Frequency in Hz")
Expand Down
15 changes: 9 additions & 6 deletions tests/test_plot.py
Expand Up @@ -74,17 +74,20 @@ def test_line_plots(function, handsome_signal, handsome_signal_v2):
file_type, compare_output)


@pytest.mark.parametrize('signal', ['handsome_signal', 'impulse'])
@pytest.mark.parametrize('param', [
['phase_deg', True, False],
['phase_unwrap', False, True],
['phase_deg_unwrap', True, True]])
def test_line_phase_options(param, handsome_signal):
['phase_deg_unwrap', True, True],
['phase_360', False, '360'],
['phase_deg_360', True, '360']])
def test_line_phase_options(param, signal, request):
"""Test parameters that are unique to the phase plot."""
print(f"Testing: {param[0]}")

filename = param[0]
filename = param[0] + '_' + signal
print(f"Testing: {param[0] + '_' + signal}")
signal = request.getfixturevalue(signal)
create_figure()
plot.phase(handsome_signal, deg=param[1], unwrap=param[2])
plot.phase(signal, deg=param[1], unwrap=param[2])
save_and_compare(create_baseline, baseline_path, output_path, filename,
file_type, compare_output)

Expand Down
Binary file modified tests/test_plot_data/baseline/custom_subplots_mix.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/test_plot_data/baseline/custom_subplots_mix_hold.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/test_plot_data/baseline/freq_phase_default.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/test_plot_data/baseline/freq_phase_frequency_data.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/test_plot_data/baseline/freq_phase_hold.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/test_plot_data/baseline/phase_default.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/test_plot_data/baseline/phase_freqscale_linear.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/test_plot_data/baseline/phase_freqscale_log.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/test_plot_data/baseline/phase_frequency_data.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/test_plot_data/baseline/phase_hold.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed tests/test_plot_data/baseline/phase_unwrap.png
Binary file not shown.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.