Skip to content

Commit

Permalink
Merge pull request #208 from neutrinoceros/bug/create_cmap_overview_s…
Browse files Browse the repository at this point in the history
…ingle_map

BUG: fix a bug where `create_cmap_overview` would raise an exception if passed a single element list of colormaps
  • Loading branch information
neutrinoceros authored Jan 23, 2025
2 parents ea88088 + f7e7e77 commit c321332
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
17 changes: 10 additions & 7 deletions cmyt/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# type aliases

if TYPE_CHECKING:
from typing import Final, TypeAlias, cast
from typing import Final, TypeAlias

from matplotlib.axes import Axes
from numpy.typing import NDArray
Expand Down Expand Up @@ -156,17 +156,19 @@ def create_cmap_overview(
raise ValueError(f"Received invalid or empty subset: {subset}")

# scale the image size by the number of cmaps
fig = Figure(figsize=(6, 2.6 * len(cmaps) / 10.0))
axes = fig.subplots(nrows=len(cmaps))
if TYPE_CHECKING:
axes = cast(NDArray[np.floating], axes)
fig = Figure(figsize=(6, 0.26 * len(cmaps)))
axes: list[Axes]
if len(cmaps) == 1:
axes = [fig.subplots()]
else:
axes = list(fig.subplots(nrows=len(cmaps)))

for name, ax in zip(cmaps, axes, strict=True):
RGBs = [get_rgb(mpl.colormaps[name])]
_axes = [ax]
if with_grayscale:
RGBs.append(to_grayscale(RGBs[0]))
_axes.append(ax.inset_axes([0, 1, 0.999999, 0.3]))
_axes.append(ax.inset_axes((0, 1, 0.999999, 0.3)))

for rgb, _ax in zip(RGBs, _axes, strict=True):
_ax.axis("off")
Expand All @@ -175,7 +177,8 @@ def create_cmap_overview(
ax.get_xlim()[1] * 1.02, 0, name.removeprefix(_CMYT_PREFIX), fontsize=10
)

fig.tight_layout(h_pad=0.2)
if len(cmaps) > 1:
fig.tight_layout(h_pad=0.2)
fig.subplots_adjust(top=0.9, bottom=0.05, right=0.85, left=0.05)
if filename is not None:
fig.savefig(os.fspath(filename), dpi=200)
Expand Down
Binary file added tests/baseline/test_overview_to_fig_subset2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@


@mpl_compare
@pytest.mark.parametrize("subset", [None, ["pastel", "arbre", "algae"]])
@pytest.mark.parametrize("subset", [None, ["pastel", "arbre", "algae"], ["pastel"]])
def test_overview_to_fig(subset):
return create_cmap_overview(subset=subset, with_grayscale=True)

Expand Down

0 comments on commit c321332

Please sign in to comment.