Here is my datasets:
df
A B C
0 13 Yes False
1 12 No True
2 2 Yes True
3 12 No False
4 4 No True
5 1 Yes True
6 1 No False
7 5 No True
8 15 Yes False
and
df2
A B C
0 13 Yes False
1 12 No False
2 11 No False
3 15 Yes False
4 12 No False
5 21 Yes False
Here is the piece of problematic code:
fig, ax = plt.subplots(2,1, sharey="all", sharex="all")
df2.boxplot("A", by=["B","C"], ax=ax[0])
df.boxplot("A", by=["B","C"], ax=ax[1])
which gives
The problem I have, is that, in the upper subplot, the boxplot on the right hand side should be shifted of 1 to right to align with the (Yes, False)
label.
I think it comes from the fact that sharex
doesn't care much about the xticklabels, but rather the xticks values (which are [1,2]
and [1,2,3,4]
). I can fix this with the positions=[1,3]
argument in df2.boxplot
.
The question is, how can I fix this without that prior knowledge of which groups won't be represented ?
Also, could this be a Pandas or Matplotlib bug, or this behavior is expected for certain reason ?