Giter Site home page Giter Site logo

Comments (4)

mwaskom avatar mwaskom commented on June 17, 2024

defaultdict is a nice pythonic solution here, but the type signature for palette is already quite complicated and i'm fairly averse to expanding it further. I'm not also not convinced that setting up the defaultdict is that much more convenient than defining a full dict palette based on the data, e.g. something like

palette = {
    *{x: "k" for x in data["hues"].unique()},
    "foo": "#ff0000",
    "bar": "#00ff00",
}

Is the same LoC and avoids an import.

from seaborn.

ehermes avatar ehermes commented on June 17, 2024

This is a good solution if you have the data that you will be plotting when you are first creating the palette. In our application, the palette is "statically" defined in a library, and the data we plot is generated at runtime. Sometimes the data contains entries that we did not expect to be present at the time we wrote the library, so we need to have a backup value present. My current workaround to this issue is to essentially do what you're suggesting, but I have to do it in every single function that creates a seaborn plot, which is a lot of redundant code. We could possibly simplify things through a code re-org, but my preference would be for seaborn to use the defaultdict that we have chosen for this exact reason in the expected manner.

from seaborn.

mwaskom avatar mwaskom commented on June 17, 2024

Why say “in this expected manner”? Defaultdict is not a subtype of dict and seaborn’s docs don’t suggest that it will be accepted.

from seaborn.

ehermes avatar ehermes commented on June 17, 2024

Strictly speaking, defaultdict is a subtype of dict:

In [1]: from collections import defaultdict

In [2]: palette = defaultdict(lambda: "#000000", {
   ...:     "foo": "#ff0000",
   ...:     "bar": "#00ff00",
   ...: })

In [3]: isinstance(palette, dict)
Out[3]: True

When I say "in the expected manner", I mean from the "duck typing" perspective: a defaultdict behaves like a dict, and thus should be suitable for any application in which a dict is accepted. The only reason we cannot use a defaultdict as the palette for seaborn is because of an extra check that every level has a corresponding key in it, which may not be true for non-primitive dict-likes. Actually, this brings to mind an alternative possible solution, which doesn't specifically require reference to defaultdict:

if isinstance(palette, dict):
    missing = set()
    for level in levels:
        try:
            palette[level]
        except KeyError:
            missing.add(level)
    if any(missing):
        err = "The palette dictionary is missing keys: {}"
        raise ValueError(err.format(missing))

Edit: Removed non-functional alternate suggestions (apparently defaultdict.get doesn't behave the way I thought it did)

In any case, my point is that the current check is preventing us from using something as the palette which we would otherwise be able to, and which we currently do use for our other non-matplotlib plots (namely plotly). The changes I have suggested here would add more flexibility to the code without impacting the functionality of the missing key check, when users are passing a standard dict.

from seaborn.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.