Reputation: 6917
I am trying to create a boxplot with different y-axes and y-scales in seaborn but got stuck here.
In matplotlib I can use the following code to obtain my result:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# create random dataframe with different scales
df = pd.DataFrame(np.random.rand(30, 5), columns=['A', 'B', 'C', 'D', 'E'])
df['A'] *= 5
df['C'] *= 10
df['E'] *= 15
# create boxplot with a different y scale for different rows
selection = ['A', 'C', 'E']
fig, ax = plt.subplots(1, len(selection), figsize=(10, len(selection)))
i = 0
for col in selection:
axo = df[col].plot(kind='box', ax=ax[i], showfliers=False, grid=True)
axo.set_ylim(df[col].min(), df[col].max())
axo.set_ylabel(col + ' / Unit')
i += 1
plt.tight_layout()
plt.show()
which yields:
I have tried to replace the actual boxplot using a seaborn boxplot but it did not work.
Now my question is: How can I obtain the same plotting result using seaborn?
Thanks in advance for your help!
Cheers Cord
Upvotes: 2
Views: 3260
Reputation: 1
This is a code snippet I am using in my notebook, Can try using it this way.
columns = df_plt.columns # my data has 8 columns.
fig, ax = plt.subplots(ncols = 7, figsize=(19,10))
plt.subplots_adjust(wspace = 1)
sns.set_palette("PRGn")
for i in range(0,7):
s = sns.boxplot(ax = ax[i], data = df_plt[columns[i]], showfliers = False)
ax[i].set_xlabel(columns[i])
plt.show()
Upvotes: 0
Reputation: 6917
I found a solution on my own:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
# create random dataframe with different scales
df = pd.DataFrame(np.random.rand(30, 5), columns=['A', 'B', 'C', 'D', 'E'])
df['A'] *= 5
df['C'] *= 10
df['E'] *= 15
# create boxplot with a different y scale for different rows
selection = ['A', 'C', 'E']
fig, axes = plt.subplots(1, len(selection))
for i, col in enumerate(selection):
ax = sns.boxplot(y=df[col], ax=axes.flatten()[i])
ax.set_ylim(df[col].min(), df[col].max())
ax.set_ylabel(col + ' / Unit')
plt.show()
yields:
Thanks anyway!
Upvotes: 4