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

✨ Add show_zero_line option to plot_overview and plot_fitted_traces #128

Merged
merged 1 commit into from
Jan 27, 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
6 changes: 5 additions & 1 deletion pyglotaran_extras/plotting/plot_overview.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def plot_overview(
show_data_svd_legend: bool = True,
show_residual_svd_legend: bool = True,
show_irf_dispersion_center: bool = True,
show_zero_line: bool = True,
) -> Figure | tuple[Figure, Axes]:
"""Plot overview of the optimization result.

Expand Down Expand Up @@ -93,6 +94,9 @@ def plot_overview(
show_irf_dispersion_center: bool
Whether to show the the IRF dispersion center as overlay on the residual/data plot.
Defaults to True.
show_zero_line: bool
Whether or not to add a horizontal line at zero to the plots of the spectra.
Defaults to True.

Returns
-------
Expand Down Expand Up @@ -126,7 +130,7 @@ def plot_overview(
main_irf_nr=main_irf_nr,
cycler=cycler,
)
plot_spectra(res, axes[0:2, 1:3], cycler=cycler)
plot_spectra(res, axes[0:2, 1:3], cycler=cycler, show_zero_line=show_zero_line)
plot_svd(
res,
axes[2:4, 0:3],
Expand Down
57 changes: 48 additions & 9 deletions pyglotaran_extras/plotting/plot_spectra.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@
from matplotlib.pyplot import Axes


def plot_spectra(res: xr.Dataset, axes: Axes, cycler: Cycler | None = PlotStyle().cycler) -> None:
def plot_spectra(
res: xr.Dataset,
axes: Axes,
cycler: Cycler | None = PlotStyle().cycler,
show_zero_line: bool = True,
) -> None:
"""Plot spectra such as SAS and DAS as well as their normalize version on ``axes``.

Parameters
Expand All @@ -26,15 +31,21 @@ def plot_spectra(res: xr.Dataset, axes: Axes, cycler: Cycler | None = PlotStyle(
Axes to plot the spectra on (needs to be at least 2x2).
cycler : Cycler | None
Plot style cycler to use. Defaults to PlotStyle().cycler.
show_zero_line: bool
Whether or not to add a horizontal line at zero. Defaults to True.
"""
plot_sas(res, axes[0, 0], cycler=cycler)
plot_das(res, axes[0, 1], cycler=cycler)
plot_norm_sas(res, axes[1, 0], cycler=cycler)
plot_norm_das(res, axes[1, 1], cycler=cycler)
plot_sas(res, axes[0, 0], cycler=cycler, show_zero_line=show_zero_line)
plot_das(res, axes[0, 1], cycler=cycler, show_zero_line=show_zero_line)
plot_norm_sas(res, axes[1, 0], cycler=cycler, show_zero_line=show_zero_line)
plot_norm_das(res, axes[1, 1], cycler=cycler, show_zero_line=show_zero_line)


def plot_sas(
res: xr.Dataset, ax: Axis, title: str = "SAS", cycler: Cycler | None = PlotStyle().cycler
res: xr.Dataset,
ax: Axis,
title: str = "SAS",
cycler: Cycler | None = PlotStyle().cycler,
show_zero_line: bool = True,
) -> None:
"""Plot SAS (Species Associated Spectra) on ``ax``.

Expand All @@ -48,6 +59,8 @@ def plot_sas(
Title of the plot. Defaults to "SAS".
cycler : Cycler | None
Plot style cycler to use. Defaults to PlotStyle().cycler.
show_zero_line: bool
Whether or not to add a horizontal line at zero. Defaults to True.
"""
add_cycler_if_not_none(ax, cycler)
keys = [
Expand All @@ -58,10 +71,16 @@ def plot_sas(
sas.plot.line(x="spectral", ax=ax)
ax.set_title(title)
ax.get_legend().remove()
if show_zero_line is True:
ax.axhline(0, color="k", linewidth=1)


def plot_norm_sas(
res: xr.Dataset, ax: Axis, title: str = "norm SAS", cycler: Cycler | None = PlotStyle().cycler
res: xr.Dataset,
ax: Axis,
title: str = "norm SAS",
cycler: Cycler | None = PlotStyle().cycler,
show_zero_line: bool = True,
) -> None:
"""Plot normalized SAS (Species Associated Spectra) on ``ax``.

Expand All @@ -75,6 +94,8 @@ def plot_norm_sas(
Title of the plot. Defaults to "norm SAS".
cycler : Cycler | None
Plot style cycler to use. Defaults to PlotStyle().cycler.
show_zero_line: bool
Whether or not to add a horizontal line at zero. Defaults to True.
"""
add_cycler_if_not_none(ax, cycler)
keys = [
Expand All @@ -86,10 +107,16 @@ def plot_norm_sas(
(sas / np.abs(sas).max(dim="spectral")).plot.line(x="spectral", ax=ax)
ax.set_title(title)
ax.get_legend().remove()
if show_zero_line is True:
ax.axhline(0, color="k", linewidth=1)


def plot_das(
res: xr.Dataset, ax: Axis, title: str = "DAS", cycler: Cycler | None = PlotStyle().cycler
res: xr.Dataset,
ax: Axis,
title: str = "DAS",
cycler: Cycler | None = PlotStyle().cycler,
show_zero_line: bool = True,
) -> None:
"""Plot DAS (Decay Associated Spectra) on ``ax``.

Expand All @@ -103,6 +130,8 @@ def plot_das(
Title of the plot. Defaults to "DAS".
cycler : Cycler | None
Plot style cycler to use. Defaults to PlotStyle().cycler.
show_zero_line: bool
Whether or not to add a horizontal line at zero. Defaults to True.
"""
add_cycler_if_not_none(ax, cycler)
keys = [
Expand All @@ -113,10 +142,16 @@ def plot_das(
das.plot.line(x="spectral", ax=ax)
ax.set_title(title)
ax.get_legend().remove()
if show_zero_line is True:
ax.axhline(0, color="k", linewidth=1)


def plot_norm_das(
res: xr.Dataset, ax: Axis, title: str = "norm DAS", cycler: Cycler | None = PlotStyle().cycler
res: xr.Dataset,
ax: Axis,
title: str = "norm DAS",
cycler: Cycler | None = PlotStyle().cycler,
show_zero_line: bool = True,
) -> None:
"""Plot normalized DAS (Decay Associated Spectra) on ``ax``.

Expand All @@ -130,6 +165,8 @@ def plot_norm_das(
Title of the plot. Defaults to "norm DAS".
cycler : Cycler | None
Plot style cycler to use. Defaults to PlotStyle().cycler.
show_zero_line: bool
Whether or not to add a horizontal line at zero. Defaults to True.
"""
add_cycler_if_not_none(ax, cycler)
keys = [
Expand All @@ -140,3 +177,5 @@ def plot_norm_das(
(das / np.abs(das).max(dim="spectral")).plot.line(x="spectral", ax=ax)
ax.set_title(title)
ax.get_legend().remove()
if show_zero_line is True:
ax.axhline(0, color="k", linewidth=1)
10 changes: 10 additions & 0 deletions pyglotaran_extras/plotting/plot_traces.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def plot_data_and_fits(
per_axis_legend: bool = False,
y_label: str = "a.u.",
cycler: Cycler | None = PlotStyle().data_cycler_solid,
show_zero_line: bool = True,
) -> None:
"""Plot data and fits for a given ``wavelength`` on a given ``axis``.

Expand Down Expand Up @@ -72,6 +73,8 @@ def plot_data_and_fits(
Label used for the y-axis of each subplot.
cycler : Cycler | None
Plot style cycler to use. Defaults to PlotStyle().data_cycler_solid.
show_zero_line: bool
Whether or not to add a horizontal line at zero. Defaults to True.

See Also
--------
Expand All @@ -94,6 +97,8 @@ def plot_data_and_fits(
[next(axis._get_lines.prop_cycler) for _ in range(2)]
if linlog:
axis.set_xscale("symlog", linthresh=linthresh)
if show_zero_line is True:
axis.axhline(0, color="k", linewidth=1)
axis.set_ylabel(y_label)
if per_axis_legend is True:
axis.legend()
Expand All @@ -113,6 +118,7 @@ def plot_fitted_traces(
title: str = "Fit overview",
y_label: str = "a.u.",
cycler: Cycler | None = PlotStyle().data_cycler_solid,
show_zero_line: bool = True,
) -> tuple[Figure, Axes]:
"""Plot data and their fit in per wavelength plot grid.

Expand Down Expand Up @@ -148,6 +154,9 @@ def plot_fitted_traces(
Label used for the y-axis of each subplot.
cycler : Cycler | None
Plot style cycler to use. Defaults to PlotStyle().data_cycler_solid.
show_zero_line: bool
Whether or not to add a horizontal line at zero. Defaults to True.


Returns
-------
Expand Down Expand Up @@ -189,6 +198,7 @@ def plot_fitted_traces(
per_axis_legend=per_axis_legend,
y_label=y_label,
cycler=cycler,
show_zero_line=show_zero_line,
)
if per_axis_legend is False:
add_unique_figure_legend(fig, axes)
Expand Down