question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Better support for faceted plots with plt.hexbin and plt.hist2d

See original GitHub issue

Just 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')

image

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)

image

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: image

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)

image

(Yes, matplotlib uses totally different keyword arguments in hexbin and hist2d for all the control nobs, even if they are basically equivalent.)

Action items

  1. 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.
  2. How do you feel about tweaking defaults parameters based on the type of plot passed to FacetGrid.map? Perhaps the clearer solution is to write sns.binplot (mirroring sns.kdeplot) to provide a unified API with better defaults, which we could also use in sns.jointplot?
  3. 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.
  4. Both these sorts of plots call for adding colorbar legends (see also #312, #313).
  5. 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:closed
  • Created 9 years ago
  • Reactions:5
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
mwaskomcommented, Oct 6, 2014

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.

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.

How do you feel about tweaking defaults parameters based on the type of plot passed to FacetGrid.map? Perhaps the clearer solution is to write sns.binplot (mirroring sns.kdeplot) to provide a unified API with better defaults, which we could also use in sns.jointplot?

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).

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.

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.

Both these sorts of plots call for adding colorbar legends (see also #312, #313).

Ah, yes…

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).

👍

0reactions
mwaskomcommented, Jun 15, 2020

Also should note: that plot will soon be easier to make by doing something like

sns.distplot(
    data=penguins, x="culmen_length_mm", y="culmen_depth_mm",
    hue="species", col="species",
)

But that remains to be implemented.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Bountysource
Better support for faceted plots with plt.hexbin and plt.hist2d. ... Coming soon: A brand new website interface for an even better experience!
Read more >
Different behaviour of hexbin and histogram2d - Stack Overflow
My recommendation is just to use plt.hist2d() instead, which will adjust the extent and orientation properly, as with plt.hexbin .
Read more >
Chapter 4. Visualization with Matplotlib - O'Reilly
A visualization you can't see won't be of much use, but just how you view your Matplotlib plots depends on the context. The...
Read more >
Hexbin plot with matplotlib - Python Graph Gallery
This post aims to display hexbin plots built with matplotlib and shows how to change bin size and colors. A hexbin plot is...
Read more >
Make your Data Talk!. From 0 to Hero in visualization using…
It is low level library and you have total control over your plot. ... can help us understand our data more, and probably...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found