Reputation: 97
The following code produces 2 side-by-side plots. However, I would like to push the right plot to the right so that its label shows detached from the left plot. How can I do it? I could not find any option in subplots
, nor in countplot
here is the code:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
data = {
'apples': [3, 2, 0, np.nan, 2],
'oranges': [0, 7, 7, 2, 7],
'figs':[1, np.nan, 10, np.nan, 10]
}
purchases = pd.DataFrame(data)
fig, ax =plt.subplots(1,2)
sns.countplot(purchases['apples'], ax=ax[0])
sns.countplot(purchases['oranges'], ax=ax[1])
show()
Upvotes: 0
Views: 557
Reputation: 7509
In order to make your data play nicely with seaborn, consider changing your dataframe to the "long" format and plotting all categories and their corresponding count with sns.catplot
:
data = purchases.stack().droplevel(0).reset_index()
data.columns = ['fruit', 'number']
print(data.head(5))
# output:
# fruit number
# 0 apples 3.0
# 1 oranges 0.0
# 2 figs 1.0
# 3 apples 2.0
# 4 oranges 7.0
sns.catplot(data=data, x='number', kind='count', col='fruit')
plt.show()
output:
Upvotes: 0
Reputation: 150735
An option is tight_layout
:
fig, ax =plt.subplots(1,2)
sns.countplot(purchases['apples'], ax=ax[0])
sns.countplot(purchases['oranges'], ax=ax[1])
plt.tight_layout()
output:
Upvotes: 2