Comments (4)
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.
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.
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.
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)
- Performance Issue: Seaborn Lineplot Execution Time Discrepancy with and without Timezones HOT 5
- sns.barplot(index ="index", x=" data_column1", y="data_column2") creates error due to whitespace before column name in x, y parameters HOT 1
- AttributeError: module 'numpy' has no attribute 'float'. HOT 6
- Add Color Universal Design palette HOT 2
- `so.Hist` ignores `common_norm=True` for the `"density"` aggregate statistic
- Differences when displaying standard deviation on line and barplot HOT 1
- Subtplot size difference from matplotlib plot HOT 5
- catplot with numeric hue and hue_order: empty legend handles HOT 2
- sns.lineplot `hue`param will make wrong number of classes when it pass a field whose dtype is not `category` HOT 1
- Calling scatterplot with size and hue raises TypeError HOT 3
- HI ,we need seaborn cpp HOT 2
- histogram with fixed binwidth - unexpected results for last column HOT 4
- Docu: Reference to root package missing in object.inv file HOT 6
- Value error in histplot with binwidth smaller than half the data range HOT 2
- UnboundLocalError: local variable 'boxprops' referenced before assignment HOT 16
- Doubts about using two types of graphics to draw simultaneously HOT 2
- Palette setting ignored unless passed directly for numeric hues HOT 1
- scatter plot with wide data: markers=False gives an error HOT 3
- countplot taking long time for Series and not Pandas HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from seaborn.