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 6 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
33 changes: 25 additions & 8 deletions pyfar/plot/_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,17 +126,34 @@ 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 not deg:
if unwrap is False or unwrap == "360":
# major ticks at mulitples 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))
else:
# major ticks
if ymax-ymin < 20*np.pi:
# at multiples of 2 pi, if the range is smaller than 20 pi
n = 2
else:
# otherwise: at multiples of x*10*pi
# with x determined to maximum 10 intervals
n = np.ceil((ymax-ymin)/np.pi/9/10)*10
ax.yaxis.set_major_locator(MultipleFractionLocator(n, 1, np.pi))
ax.yaxis.set_major_formatter(
MultipleFractionFormatter(n, 1, np.pi, r'\pi'))
# minor ticks at half of major ticks,
# but rounded to multiples of 10*pi
n = np.round(n/2/10)*10
ax.yaxis.set_minor_locator(MultipleFractionLocator(n/2, 1, np.pi))

# prepare figure
ax.set_xlabel("Frequency in Hz")
ax.set_ylabel(ylabel_string)
Expand Down
4 changes: 3 additions & 1 deletion tests/test_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ def test_line_plots(function, handsome_signal, handsome_signal_v2):
@pytest.mark.parametrize('param', [
['phase_deg', True, False],
['phase_unwrap', False, True],
['phase_deg_unwrap', True, True]])
['phase_deg_unwrap', True, True],
['phase_360', False, '360'],
['phase_deg_360', True, '360']])
def test_line_phase_options(param, handsome_signal):
"""Test parameters that are unique to the phase plot."""
print(f"Testing: {param[0]}")
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.
Binary file added tests/test_plot_data/baseline/phase_360.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_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 added tests/test_plot_data/baseline/phase_deg_360.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_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 modified tests/test_plot_data/baseline/phase_unwrap.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.