Skip to content

Commit

Permalink
FIX: gracfully handle missing seaborn dependency (pydata#9835)
Browse files Browse the repository at this point in the history
xref: pydata#9561 (comment)

Fixes regression introduced in pydata#9561

this specific seaborn import should be handled gracefully, as it was before #pydata#9561
  • Loading branch information
scott-huberty authored Nov 28, 2024
1 parent 0b97969 commit 7fd572d
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions xarray/plot/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,11 @@ def _color_palette(cmap, n_colors):
pal = cmap(colors_i)
except ValueError:
# ValueError happens when mpl doesn't like a colormap, try seaborn
if TYPE_CHECKING:
import seaborn as sns
else:
sns = attempt_import("seaborn")

try:
pal = sns.color_palette(cmap, n_colors=n_colors)
except ValueError:
from seaborn import color_palette

pal = color_palette(cmap, n_colors=n_colors)
except (ValueError, ImportError):
# or maybe we just got a single color as a string
cmap = ListedColormap([cmap] * n_colors)
pal = cmap(colors_i)
Expand Down Expand Up @@ -192,7 +189,10 @@ def _determine_cmap_params(
cmap_params : dict
Use depends on the type of the plotting function
"""
import matplotlib as mpl
if TYPE_CHECKING:
import matplotlib as mpl
else:
mpl = attempt_import("matplotlib")

if isinstance(levels, Iterable):
levels = sorted(levels)
Expand Down

0 comments on commit 7fd572d

Please sign in to comment.