Skip to content
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

sharex not working with displot? #2557

Closed
RMeli opened this issue Apr 16, 2021 · 8 comments
Closed

sharex not working with displot? #2557

RMeli opened this issue Apr 16, 2021 · 8 comments

Comments

@RMeli
Copy link

RMeli commented Apr 16, 2021

I am trying to use displot to plot distributions of different variables in the same figure. I might be using displot incorrectly, but it seems to me that the sharex option passed to facet_kws is not working correctly (while sharey does work as expected).

Simple code snippet to reproduce:

import pandas as pd

import seaborn as sns
from matplotlib import pyplot as plt

titanic = sns.load_dataset("titanic")
df = pd.melt(titanic, id_vars=["sex", "class"], value_vars=["fare", "age"])
sns.displot(
    data=df,
    x="value",
    col="variable",
    hue="sex",
    facet_kws={"sharex": False, "sharey": False},
)
plt.savefig("test.png")

The result is the following:

test

As you can see, the y axes have different values, while the x axes seem to be shared despite the "sharex": False keyword.

@mwaskom
Copy link
Owner

mwaskom commented Apr 16, 2021

Duplicate of #2553

@mwaskom mwaskom marked this as a duplicate of #2553 Apr 16, 2021
@mwaskom mwaskom closed this as completed Apr 16, 2021
@RMeli
Copy link
Author

RMeli commented Apr 16, 2021

Thanks for pointing this out @mwaskom , sorry about that.

However, the use of common_bins=False (suggested in #2472) does not seem to do the job here; sharex=False now seems to work, however the bin size is now different also across the hue (within the same graph).

And my situation is actually more similar to the following one, where I also have different rows:

import pandas as pd

import seaborn as sns
from matplotlib import pyplot as plt

titanic = sns.load_dataset("titanic")
df = pd.melt(titanic, id_vars=["sex", "class"], value_vars=["fare", "age"])
sns.displot(
    data=df,
    x="value",
    col="variable",
    row="class",
    hue="sex",
    facet_kws={"sharex": False, "sharey": False},
    common_bins=False,
)
plt.savefig("test.png")

test

Is there a way to share bins across hue and row, but not across col?

@mwaskom
Copy link
Owner

mwaskom commented Apr 16, 2021

Well you can set the bin width you want to use, but for a deeper level of facet-specific parameterization you're going to need to use FacetGrid directly and loop over the facets while passing the specific kwargs you want to use for each one.

@RMeli
Copy link
Author

RMeli commented Apr 16, 2021

Ok, I'll try that. Thanks!

@RMeli
Copy link
Author

RMeli commented Dec 6, 2021

@mwaskom I've been using FacetGrid directly as you suggested above with some success, however I was not able to get a legend based on the hue. Essentially the same outcome described in #2472. Is there no way to get histograms that share axis across the different columns (i.e. same quantities plotted on a row, for different parameters) and have the same bin width, while also getting the legend according to the hue? I haven't been able to achieve this in any way so far. It's not possible at all because of the issues described in #2231?

@mwaskom
Copy link
Owner

mwaskom commented Dec 6, 2021

Hi @RMeli hard to advise without seeing code for your current solution. Could you make a reproducible example and ask on StackOverflow? It's an interesting task. Thanks.

@RMeli
Copy link
Author

RMeli commented Dec 7, 2021

I was about to post some code yesterday but realized it was quite similar to the code above, I think I'm running in circles... I'll post on StackOverflow, thanks.

@RMeli
Copy link
Author

RMeli commented Dec 7, 2021

For future reference: share histogram bins across columns - StackOverflow

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants