Home > Software engineering >  How to avoid barplot display coverage
How to avoid barplot display coverage

Time:07-11

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')

enter image description here

There is one problem here.

Since the Feature (ex. Antihypertensive_medication_value) of ax = [0,1] and [1,1] will be covered, is there a way to hide this display?

If you know more about this, I would appreciate it if you could let me know.

CodePudding user response:

You can change spacing between the plots using enter image description here

If you want, you can also rename Antihypertensive_medication_value to something shorter.

enter image description here

CodePudding user response:

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:

enter image description here

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:

enter image description here

  • Related