Plot separately different parts of a dataframe

Viewed 119

I would like to plot from the seaborn dataset 'tips'.

import seaborn as sns
import pandas as pd

tips = sns.load_dataset("tips")
x1 = tips.loc[(df['time']=='lunch'), 'tip']
x2 = tips.loc[(df['time']=='dinner'),'tip']

x1.plot.kde(color='orange')
x2.plot.kde(color='blue')
plt.show()

I don't know exactly where it's wrong...

Thanks for the help.

2 Answers

Seaborn's sns.kdeplot() supports the hue argument to split the plot between different categories:

import seaborn as sns
import pandas as pd

tips = sns.load_dataset("tips")
sns.kdeplot(data=tips, x='tip', hue='time')

KDE-plot

Of course your approach could work too, but there are several problems with your code:

  • What is df? Shouldn't that be tips?
  • The category names Lunch and Dinner must be capitalized, as in the data.
  • You're mixing different indexing techniques. It should be e.g. x1 = tips.tip[tips['time'] == 'Lunch'].
  • If you want to plot two KDE in the same diagram, they should be scaled according to sample size. With my approach above, seaborn has done that automatically.

As you are loading data from the seaborn built-in datasets check that your column names are case sensitive replace them with correct name.
You can plot the cumulative distribution between the time and density as follows:

sns.kdeplot(
data=tips, x="total_bill", hue="time",
cumulative=True, common_norm=False, common_grid=True,
)
Related