-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Palette does not support the use of defaultdict with missing values #3632
Comments
defaultdict is a nice pythonic solution here, but the type signature for palette = {
*{x: "k" for x in data["hues"].unique()},
"foo": "#ff0000",
"bar": "#00ff00",
} Is the same LoC and avoids an import. |
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 |
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. |
Strictly speaking, defaultdict is a subtype of dict:
When I say "in the expected manner", I mean from the "duck typing" perspective: a 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 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 |
Currently, Seaborn does not permit the use of defaultdict with missing values as a palette. A minimal example that reproduces this issue is:
My expectation is that this should use the default value of
#000000
forbaz
, which is missing from the palette. Instead, this raises an exception:For this test, I have used
seaborn-0.13.2
andmatplotlib-3.8.2
.I have a fix for this problem in a personal branch (https://github.com/ehermes/seaborn/tree/palette_defaultdict), but per your contribution guidelines, I have opened a bug report first. With permission, I can also create a PR for my fix.
The text was updated successfully, but these errors were encountered: