Skip to content

Commit

Permalink
tab20 as default for cats
Browse files Browse the repository at this point in the history
  • Loading branch information
knaaptime committed Jan 6, 2025
1 parent d3f1080 commit 23f52a8
Showing 1 changed file with 1 addition and 8 deletions.
9 changes: 1 addition & 8 deletions geosnap/visualize/interactive.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ def _dexplore(
f"`cmap` must be one of {list(colormaps.keys())} but {cmap} was passed"
)
if cmap is None:
cmap = "Set1" if categorical else "viridis"
cmap = "tab20" if categorical else "viridis"
if categorical:
color_array = _get_categorical_cmap(gdf[column], cmap, nan_color, alpha)
elif scheme is None:
Expand Down Expand Up @@ -313,29 +313,22 @@ def _get_categorical_cmap(categories, cmap, nan_color, alpha):
) from e

cat_codes = pd.Series(pd.Categorical(categories).codes, dtype="category")

# nans are encoded as -1 OR largest category depending on input type
# re-encode to always be last category
cat_codes = cat_codes.cat.rename_categories({-1: len(cat_codes.unique()) - 1})

unique_cats = categories.dropna().unique()
n_cats = len(unique_cats)

colors = colormaps[cmap].resampled(n_cats)(list(range(n_cats)), alpha)
colors = (np.array(colors) * 255).astype(int)
colors = np.vstack([colors, nan_color])


n_colors = colors.shape[0]

if n_cats > n_colors:
warn(
"the number of unique categories exceeds the number of available colors",
stacklevel=3,
)
floor = (n_cats // n_colors) + 1
colors = np.vstack([colors] * floor)

temp_cmap = dict(zip(range(n_cats + 1), colors))
fill_color = apply_categorical_cmap(cat_codes, temp_cmap)
return fill_color

0 comments on commit 23f52a8

Please sign in to comment.