Skip to content

Commit

Permalink
Use None as fallback/default for plot titles
Browse files Browse the repository at this point in the history
This allows empty input '' to be distinguished from default None
  • Loading branch information
ajjackson committed Oct 14, 2024
1 parent 5af1228 commit 73a2598
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
8 changes: 3 additions & 5 deletions euphonic/cli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ def __call__(self, parser, args, values, option_string=None):
section.add_argument(
'-s', '--save-to', dest='save_to', default=None,
help='Save resulting plot to a file with this name')
section.add_argument('--title', type=str, default='',
section.add_argument('--title', type=str, default=None,
help='Plot title')
section.add_argument('--x-label', '--xlabel', type=str, default=None,
dest='xlabel', help='Plot x-axis label')
Expand Down Expand Up @@ -935,8 +935,6 @@ def _compose_style(
style.append(explicit_args)
return style

def _get_title(filename: str, title: str = '') -> str:
def _get_title(filename: str, title: str | None = None) -> str:
"""Get a plot title: either user-provided string, or from filename"""
if title:
return title
return pathlib.Path(filename).stem
return pathlib.Path(filename).stem if title is None else title
8 changes: 4 additions & 4 deletions euphonic/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def plot_1d(spectra: Union[Spectrum1D,
Spectrum1DCollection,
Sequence[Spectrum1D],
Sequence[Spectrum1DCollection]],
title: str = '',
title: str | None = None,
xlabel: str = '',
ylabel: str = '',
ymin: Optional[float] = None,
Expand Down Expand Up @@ -191,7 +191,7 @@ def plot_1d(spectra: Union[Spectrum1D,
ax.set_xlabel(xlabel)
ax.set_ylabel(ylabel)

fig.suptitle(title)
fig.suptitle('' if title is None else title)
return fig


Expand Down Expand Up @@ -249,7 +249,7 @@ def plot_2d(spectra: Union[Spectrum2D, Sequence[Spectrum2D]],
vmin: Optional[float] = None,
vmax: Optional[float] = None,
cmap: Optional[Union[str, Colormap]] = None,
title: str = '',
title: str | None = None,
xlabel: str = '',
ylabel: str = '') -> Figure:
"""
Expand Down Expand Up @@ -319,7 +319,7 @@ def _get_minmax_intensity(spectrum: Spectrum2D) -> Tuple[float, float]:
ax.set_xlabel(xlabel)
ax.set_ylabel(ylabel)

fig.suptitle(title)
fig.suptitle('' if title is None else title)

return fig

Expand Down
4 changes: 3 additions & 1 deletion euphonic/writers/phonon_website.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,15 @@ def _combine_neighbouring_labels(x_tick_labels: XTickLabels) -> XTickLabels:
for index in sorted(labels):
if index - 1 in labels:
if labels.get(index - 1) != labels.get(index):
# Neighbouring labels are different: merge second into first
labels[index - 1] = f"{labels[index - 1]}|{labels[index]}"
# Remove second label of pair
del labels[index]
return sorted(labels.items())


def _modes_to_phonon_website_dict(modes: QpointPhononModes,
name: str = 'Euphonic export',
name: str | None = None,
repetitions: tuple[int, int, int] = (2, 2, 2),
x_tick_labels: XTickLabels | None = None,
) -> PhononWebsiteData:
Expand Down

0 comments on commit 73a2598

Please sign in to comment.