Reputation: 111
I would like to create a barplot from the following data frame.
import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
fig, axes = plt.subplots(2, 2, figsize=(10, 8), sharex=True)
df_A_1 = pd.DataFrame({'Features': ['age', 'weight', 'height', 'waist'],
'imp': [0.15, 0.1, 0.07, 0.05]
})
df_A_2 = pd.DataFrame({'Features': ['age', 'weight', 'height', 'waist'],
'imp': [0.17, 0.12, 0.08, 0.07]
})
df_B_1 = pd.DataFrame({'Features': ['Antihypertensive_medication_value', 'KDOSE', 'K', 'ARR_1', 'TG'],
'imp': [0.135, 0.107, 0.094, 0.059, 0.079]
})
df_B_2 = pd.DataFrame({'Features': ['Antihypertensive_medication_value', 'KDOSE', 'K', 'ARR_1', 'TG'],
'imp': [0.15, 0.13, 0.07, 0.06, 0.04]
})
sns.barplot('imp', 'Features', data=df_A_1, ax=axes[0, 0], color='lightgrey')
sns.barplot('imp', 'Features', data=df_A_2, ax=axes[0, 1], color='black')
sns.barplot(x='imp', y='Features', data=df_B_1, ax=axes[1, 0], color='lightgrey')
sns.barplot('imp', y='Features', data=df_B_2, ax=axes[1, 1], color='black')
There is one problem here.
Since the Feature (for example, Antihypertensive_medication_value) of ax = [0,1] and [1,1] will be covered, is there a way to hide this display?
Upvotes: 0
Views: 57
Reputation: 1935
You can change spacing between the plots using plt.subplots_adjust()
import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
fig, axes = plt.subplots(2, 2, figsize=(10, 8), sharex=True)
df_A_1 = pd.DataFrame({'Features': ['age', 'weight', 'height', 'waist'],
'imp': [0.15, 0.1, 0.07, 0.05]
})
df_A_2 = pd.DataFrame({'Features': ['age', 'weight', 'height', 'waist'],
'imp': [0.17, 0.12, 0.08, 0.07]
})
df_B_1 = pd.DataFrame({'Features': ['Antihypertensive_medication_value', 'KDOSE', 'K', 'ARR_1', 'TG'],
'imp': [0.135, 0.107, 0.094, 0.059, 0.079]
})
df_B_2 = pd.DataFrame({'Features': ['Antihypertensive_medication_value', 'KDOSE', 'K', 'ARR_1', 'TG'],
'imp': [0.15, 0.13, 0.07, 0.06, 0.04]
})
sns.barplot('imp', 'Features', data=df_A_1, ax=axes[0, 0], color='lightgrey')
sns.barplot('imp', 'Features', data=df_A_2, ax=axes[0, 1], color='black')
sns.barplot(x='imp', y='Features', data=df_B_1,
ax=axes[1, 0], color='lightgrey')
sns.barplot('imp', y='Features', data=df_B_2, ax=axes[1, 1], color='black')
plt.subplots_adjust(hspace=0.2, wspace=1.5)
If you want, you can also rename Antihypertensive_medication_value
to something shorter.
Upvotes: 1
Reputation: 4543
One option is to do not repeat your ylabels:
import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
fig, axes = plt.subplots(2, 2, figsize=(10, 8), sharex=True, sharey=True)
df_A_1 = pd.DataFrame({'Features' : ['age', 'weight', 'height', 'waist'],
'imp': [0.15, 0.1, 0.07, 0.05]
})
df_A_2 = pd.DataFrame({'Features' : ['age', 'weight', 'height', 'waist'],
'imp': [0.17, 0.12, 0.08, 0.07]
})
df_B_1 = pd.DataFrame({'Features' : ['Antihypertensive_medication_value', 'KDOSE', 'K', 'ARR_1', 'TG'],
'imp': [0.135, 0.107, 0.094, 0.059, 0.079]
})
df_B_2 = pd.DataFrame({'Features' : ['Antihypertensive_medication_value', 'KDOSE', 'K', 'ARR_1', 'TG'],
'imp': [0.15, 0.13, 0.07, 0.06, 0.04]
})
sns.barplot('imp','Features',data=df_A_1, ax=axes[0, 0], color='lightgrey')
sns.barplot('imp','Features', data=df_A_2, ax=axes[0, 1], color='black')
sns.barplot(x='imp',y='Features', data=df_B_1, ax=axes[1, 0], color='lightgrey')
sns.barplot('imp',y='Features', data=df_B_2, ax=axes[1, 1], color='black')
Output:
Another option is to set a layout, such as follows:
fig, axes = plt.subplots(2, 2, figsize=(16, 8), sharex=True, constrained_layout=True)
Output:
Upvotes: 0