Reputation: 45
First, to say. I'm aware of similar questions namely pandas-boxplot-groupby-different-ylim-in-each-subplot and different-ylim-for-shared-axes-in-pandas-boxplot but I am still not able to get the desired result with my dataset. I've got a dataset of wastewater samples where columns are different pollutants and rows with "codes" for three different groups:
Dataset looks like this (but bigger)
data = pd.DataFrame({'Group': pd.Series(['IN', 'IN', 'IN', 'OUT', 'OUT', 'OUT']),
'COD' : pd.Series([800, 900, 100, 400, 450, 500]),
'BOD' : pd.Series([40, 50, 60, 20, 25, 30])})
and my code for boxplot (in jupyter with matplotlib inline)
fig, ax = plt.subplots(figsize=(10, 5))
data.boxplot(['COD','BOD'], 'Group', ax)
and the output is like this
It's obvious that the y-axis can't be the same for all the data, but I was not able to recreate the output in such way to change the axis for individual subplots even with help of the links above. I would really a appreciate any help on this.
Upvotes: 4
Views: 7600
Reputation: 40697
You need to create your axes using sharey=False
before calling DataFrame.boxplot()
to force the non-sharing of y-axes limits. But you have to be careful to provide the correct number of axes, otherwise pandas is forced to clear the figure and create it's own axes (which will then share their y-axes).
fig, axs = plt.subplots(1,len(data.Group.unique()),figsize=(10, 5), sharey=False)
data.boxplot(['COD','BOD'], 'Group', axs)
Upvotes: 8
Reputation: 153460
One way you could do this create different axes for the subplots and plotting the dataframe columns separately:
fig, ax = plt.subplots(1, 2, figsize=(10, 5))
data.boxplot('COD','Group', ax=ax[0])
data.boxplot('BOD','Group', ax=ax[1])
Output:
Upvotes: 7