Skip to content

Commit 7fd572d

Browse files
FIX: gracfully handle missing seaborn dependency (#9835)
xref: #9561 (comment) Fixes regression introduced in #9561 this specific seaborn import should be handled gracefully, as it was before ##9561
1 parent 0b97969 commit 7fd572d

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

xarray/plot/utils.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -143,14 +143,11 @@ def _color_palette(cmap, n_colors):
143143
pal = cmap(colors_i)
144144
except ValueError:
145145
# ValueError happens when mpl doesn't like a colormap, try seaborn
146-
if TYPE_CHECKING:
147-
import seaborn as sns
148-
else:
149-
sns = attempt_import("seaborn")
150-
151146
try:
152-
pal = sns.color_palette(cmap, n_colors=n_colors)
153-
except ValueError:
147+
from seaborn import color_palette
148+
149+
pal = color_palette(cmap, n_colors=n_colors)
150+
except (ValueError, ImportError):
154151
# or maybe we just got a single color as a string
155152
cmap = ListedColormap([cmap] * n_colors)
156153
pal = cmap(colors_i)
@@ -192,7 +189,10 @@ def _determine_cmap_params(
192189
cmap_params : dict
193190
Use depends on the type of the plotting function
194191
"""
195-
import matplotlib as mpl
192+
if TYPE_CHECKING:
193+
import matplotlib as mpl
194+
else:
195+
mpl = attempt_import("matplotlib")
196196

197197
if isinstance(levels, Iterable):
198198
levels = sorted(levels)

0 commit comments

Comments
 (0)