How can I create a seaborn boxplot with 2 y-axes? I need this because of different scales. My current code will overwrite the first box in the boxplot, eg. it is populated by 2 first data item from first ax and first item from second ax.
import pandas as pd
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
matplotlib.style.use('ggplot')
import seaborn as sns
df = pd.DataFrame({'A': pd.Series(np.random.uniform(0,1,size=10)),
'B': pd.Series(np.random.uniform(10,20,size=10)),
'C': pd.Series(np.random.uniform(10,20,size=10))})
fig = plt.figure()
# 2/3 of A4
fig.set_size_inches(7.8, 5.51)
plt.ylim(0.0, 1.1)
ax1 = fig.add_subplot(111)
ax1 = sns.boxplot(ax=ax1, data=df[['A']])
ax2 = ax1.twinx()
boxplot = sns.boxplot(ax=ax2, data=df[['B','C']])
fig = boxplot.get_figure()
fig
How do I prevent the first item getting overwritten?
EDIT:
If I add positions argument
boxplot = sns.boxplot(ax=ax2, data=df[['B','C']], positions=[2,3])
I get an exception:
TypeError: boxplot() got multiple values for keyword argument 'positions'
Probably because seaborn already sets that argument internally.
It may not make too much sense to use seaborn here. Using usual matplotlib boxplots allows you to use the
positions
argument as expected.