overlapping distribution plot using sea born

Viewed 400

here is the dataset: https://www.kaggle.com/emilianito/saheart

here is the question: compare the distribution of tobbaco consumption for persons having CHD and not having CHD. can you interpret the effect of tobbaco consumption on having coronary heart disease?

here is what i tried:

sn.distplot(sa_heart_df[sa_heart_df['chd'] == 'No'],
        ['tobbaco'],
        color = 'y',
        label = 'No')

sn.distplot(sa_heart_df[sa_heart_df['chd'] == 'Si'],
        ['tobbaco'],
        color = 'r',
        label = 'Si')

plt.legend();

the error is:

usr/local/lib/python3.7/dist-packages/seaborn/distributions.py:2619: FutureWarning: 

distplot is a deprecated function and will be removed in a future version. Please
adapt your code to use either displot (a figure-level function with similar flexibility) or histplot (an axes-level function for histograms). warnings.warn(msg, FutureWarning)


ValueError                                Traceback (most recent call last)
<ipython-input-28-44d824301cf1> in <module>()
  2             ['tobbaco'],
  3             color = 'y',

----> 4 label = 'No') 5 6 sn.distplot(sa_heart_df[sa_heart_df['chd'] == 'Si'],

1 Answers

First, note that FutureWarning: distplot is a deprecated is just a warning and totally unrelated to your error.

There are several issues:

  • distplot's second parameter is called bins; setting it to ['tobbaco'] isn't meaningful; anyway, for functions with many possible parameters, it is recommended to explicitly set the parameter names
  • 'tobbaco' doesn't seem to be a column in the given dataset; it's probably meant to be 'tobacco'
  • to select the 'tobacco' column of the dataframe, you could use the following syntax sns.distplot(sa_heart_df[sa_heart_df['chd'] == 'No']['tobacco'], ...)

Some minor issues:

  • distplot has been deprecated; it doesn't support parameters such as e.g. hue which are available in newer seaborn functions; it is strongly recommended to use histplot (for a histogram with optional kde curve), kdeplot (for a kde curve without histogram) or displot (for figure-level function with multiple subplots)
  • seaborn is standard imported as sns; following that convention makes your code easier to read for other people, and makes it easier to find your way in the documentation and in related Stackoverflow posts

Here is how the code could look like using kdeplot with hue:

import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd

sa_heart_df = pd.read_csv('SAheart.csv')
sns.set_style('whitegrid')
sns.kdeplot(data=sa_heart_df, x='tobacco', hue='chd', fill=True)
sns.despine()
plt.show()

sns.kdeplot with hue

import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd

sa_heart_df = pd.read_csv('SAheart.csv')
sns.displot(data=sa_heart_df, x='tobacco', col='chd', kde=True)
plt.show()

sns.displot

Related