Better support for faceted plots with plt.hexbin and plt.hist2d
See original GitHub issueJust to add two more items to my seaborn wish-list: It would great if FacetGrid
and PairGrid
worked well with hexbin and/or hist2d plots.
Hexbin
plt.hexbin
works, but the defaults are so bad that you can barely tell:
import seaborn as sns
df = sns.load_dataset('iris')
g = sns.FacetGrid(df, col='species', hue='species')
g.map(plt.hexbin, 'sepal_length', 'petal_length')
Notice that by default edgecolor
is controlled by the hue
argument, which looks rather strange.
Tweaking some options makes a big difference (also solves the aesthetic issues in #271):
g.map(plt.hexbin, 'sepal_length', 'petal_length', gridsize=20, extent=[4, 8, 1, 7], edgecolor='none', mincnt=1)
Hist2D
g = sns.FacetGrid(df, col='species')
g.map(plt.hist2d, 'sepal_length', 'petal_length')
Nope!
AttributeError: Unknown property color
The culprit (also for the inability to control edgecolor with hexbin) is this line: https://github.com/mwaskom/seaborn/blob/v0.4.0/seaborn/axisgrid.py#L418
If we move that statement inside the conditional on the next line (if self._hue_var is not None
), then that solves the color issues with hexbin
and I can get hist2d
working. Again, the defaults are terrible:
When tweaked, it looks OK:
g = sns.FacetGrid(df, col='species')
g.map(plt.hist2d, 'sepal_length', 'petal_length', range=[[4, 8], [1, 7]], bins=[8, 6], cmin=1)
(Yes, matplotlib uses totally different keyword arguments in hexbin
and hist2d
for all the control nobs, even if they are basically equivalent.)
Action items
- Can we safely apply my suggested patch? I’m guessing it breaks something else but I haven’t run it through the test-suite yet.
- How do you feel about tweaking defaults parameters based on the type of plot passed to
FacetGrid.map
? Perhaps the clearer solution is to writesns.binplot
(mirroringsns.kdeplot
) to provide a unified API with better defaults, which we could also use insns.jointplot
? - Ideally FacetGrid should have some sort of logic to set the
range
/extent
argument for all these plots together based on the shared x and y limits. - Both these sorts of plots call for adding colorbar legends (see also #312, #313).
- In any case, I would love to add a few examples to show off these kinds of plots in the Seaborn gallery (it’s not obvious they can be done now).
Issue Analytics
- State:
- Created 9 years ago
- Reactions:5
- Comments:5 (4 by maintainers)
The one thing I would wonder about is that it would surrender control over colors to the axis color cycles. That might produce some surprising results, and currently when you map multiple kinds of plots they have the same color, which I think would no longer be the case. I’m also not sure if that will break
lmplot
assumptions, but it feels like it might.That feels messy, I think I would want to resist this as much as possible. The second solution seems a lot more workable and I agree more broadly useful (the default parameters for
plt.hexbin
are in general kind of bizarre).This feels similarly messy in terms of hardcoding assumptions about certain plot functions. I’d like to keep
FacetGrid
as agnostic to that kind of stuff as possible. But it’s less clear what a good way to have smart extents by default would be.Ah, yes…
👍
Also should note: that plot will soon be easier to make by doing something like
But that remains to be implemented.