-
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
Problems when setting positions in boxplot() (mainly on log-scale axis) #3566
Comments
The problem is that I don't think there's any simple way to make this work. It's possible that seaborn should just reject the |
Hmm. At least the "center lines" were positioned correctly, it seems. And seaborn makes nice boxwidths in logscale when I use native_scale without dodging: fig, ax = plt.subplots()
ax.set_xscale("log")
sns.boxplot(data, x="x", y="y", width=0.3, native_scale=True, ax=ax) That seems to happen in seaborn, doesn't it? Because matplotlib scales boxwidth logarithmically as it seems. At least, fig, ax = plt.subplots()
ax.set_xscale("log")
ax.boxplot([[1,2,3,4], [1,2,3,4]], positions=[0.1, 1.0]) gives That means, the math must be somewhere in there already :) |
Not really sure I understand your question here. Yes, seaborn is handling setting the widths properly (for both the boxes and caps) on a log scale because matplotlib does not know how to. To accomplish that, it needs to modify the artists after matplotlib creates them. It does not need to modify the vertical line because that is width-independent. Feel free to look at the code if you would like to know more. My point is that seaborn already has a way to manage the positions of the boxes the categorical axis (i.e., the value of the x/y variable) and juggling the separate |
The problem in my case is that I want to do a boxplot for two categories with bars dodged side-by-side, but the values don't live on the same y-scale. So I am using two different axes (via ax2=ax.twinx()) to plot them. Otherwise I could use the hue parameter, of course. But I understand that it is perhaps not seaborn's job to support these kind of hacks. I will see if I can find the code which dodges the bars on native scale in case of several categories when using the hue keyword. This would just do, what I need here, I think. |
I’m still not following why you can’t apply your positional adjustment to the x data. Alternatively, you could use hue and hue order to dodge across the twinned axes. |
e.g something like tips = sns.load_dataset("tips")
x = 10 ** tips["size"]
f, ax1 = plt.subplots()
ax1.set_xscale("log")
ax2 = ax1.twinx()
sns.boxplot(tips, x=np.exp(np.log(x) - .45), y="total_bill", native_scale=True, width=.4, ax=ax1)
sns.boxplot(tips, x=np.exp(np.log(x) + .45), y="tip", native_scale=True, width=.4, color="C1", ax=ax2) is what you're looking for, no? |
Alternatively, tips = sns.load_dataset("tips")
x = 10 ** tips["size"]
f, ax1 = plt.subplots()
ax1.set_xscale("log")
ax2 = ax1.twinx()
sns.boxplot(tips, x=x, y="total_bill", hue=0, hue_order=[0, 1], dodge=True, legend=False, native_scale=True, ax=ax1)
sns.boxplot(tips, x=x, y="tip", hue=1, hue_order=[0, 1], dodge=True, legend=False, native_scale=True, ax=ax2) |
Yes, you're right, of course! I didn't get that the first time - sorry! Thanks a lot for looking into this in such detail! |
Great. If you're interested, I'd probably accept a PR to actively ignore the |
Hey everybody,
Thanks for adding
native_scale
to boxplot in 0.13!! I've been waiting for this! :)Now, I tried to do some tweaking dodging boxpositions manually, and encountered the following (I'm on 0.13.0, and matplotlib 3.7.2):
My code:
produces the following image:
This doesn't get better when trying to use log_scale instead of native_scale:
→
It works more or less fine on linear scale:
Only the xlim is not updated:
Cheers,
Leo
The text was updated successfully, but these errors were encountered: