Skip to content

Store predefined group colors as dict? #596

Open

Description

@ivirshup

Could we change colors for groups from being defined by an array of colors to being a dict mapping from the relevant key/ category to the color? I would find this more intuitive, and much easier to modify. Plus tools like seaborn can accept dicts directly as a palette:

import seaborn as sns

iris = sns.load_dataset("iris")
g = sns.FacetGrid(
    iris, 
    row="species", 
    hue="species", 
    palette={"setosa": "red", "versicolor": "green", "virginica": "blue"}
)
g.map(sns.kdeplot, "sepal_width").show()

example

Activity

  1. flying-sheep commented on Apr 8, 2019

    @flying-sheep
    Member

    I think we should do that in a way that doesn’t tempt the user to use literal colors like you just did.

    The color names people think of have a lot of bad properties for colorblind people and contrast.

    Maybe if we redefine common color names? When users specify “red” there, they only want some kind of red, not #ff0000. In one of my analyses, there’s predefined clusters which I recolor like this:

    grafik

    It’s in R and what I do is that I give names to a lot of colorbrewer colors:

    prettier_colors <- c(
        setNames(brewer.pal(8, 'Dark2')[c(1,4,6,8)], c('turquoise', 'magenta', 'yellow', 'black')),
        setNames(brewer.pal(9, 'Set1')[-c(4,6)], c('red', 'blue', 'green', 'darkorange', 'brown', 'pink', 'grey'))
    )

    In python that would be:

    import numpy as np
    from matplotlib import cm
    
    prettier_colors = dict(zip(
        [
            'turquoise', 'magenta', 'yellow', 'black',
            'red', 'blue', 'green', 'darkorange', 'brown', 'pink', 'grey',
        ], np.concatenate([
            np.array(cm.Dark2.colors)[[0,3,5,7]],
            np.array(cm.Set1.colors)[np.setdiff1d(np.arange(9), [3, 5])],
        ])
    ))
  2. ivirshup commented on Apr 8, 2019

    @ivirshup
    MemberAuthor

    My main point is that having an implicit mapping between colors and the categories is not that user or developer friendly. It seems to me like it'd be simpler to just have the mapping be explicit. This wouldn't change much from right now, except for cutting down on some boiler plate in a bunch of plotting functions.

    That example was just to demonstrate that it could even be simpler to have an explicit mapping, since we don't have to do:

    dict(zip(adata.obs[key].cat.categories, adata.uns[key + "_colors"]))
    # and instead could just do:
    adata.uns[key + "_colors"]
  3. flying-sheep commented on Apr 8, 2019

    @flying-sheep
    Member

    Sure, I agree with you generally. I’m just saying that an easy, userfriendly way should make it easy to get beautiful colors, not ugly ones.

    So in the PR tuning that interface to be nicer, we should do something about that.

  4. falexwolf commented on Apr 26, 2019

    @falexwolf
    Member

    Sorry about the late response here...

    I can imagine that we'd have more than the color attributes for each category. Just think of a .groupby followed by a summary statistics. This gives rise to dataframes that are ordered as .cat.categories. The array-way of storing color attributes is inspired by the way that pd.Categorical manages the string attributes of categories.

    Also, seaborn accepts this array-way of passing palettes. You can just do palette=adata.uns['..._colors'] in any of the seaborn functions. At least I think I've done that many times already.

    I think that the current way of storing colors is ugly and bad.

    One improvement would be to have one dict for colors only
    adata.uns['colors'] = {'cat_var1': [...], 'cat_var2': ...}
    or one dict for all attributes of each categorical
    adata.uns['louvain'] = {'colors': [...], 'annotations': [...], 'mean_expr': [...], 'markers': [...]}

    Evidently, in the latter case, one would rather like to have a more powerful pd.Categorical class that can do more than just attach a string label to an integer.

  5. AlbertoFabbri93 commented on Jul 20, 2026

    @AlbertoFabbri93
    Contributor

    There is a duplicate issue at #4206.

    I also agree that making the mapping explicit using dictionaries would be much more intuitive, that is also how Seurat works.

    @falexwolf are you proposing declaring all the colors in a single dictionary stored in adata.uns["uns"]? Than those colors would act as defaults and could be overwritten by e.g. using the palette parameter in seaborn.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions