1 minute read

The continuity of my practice on Pandas exercise from guisapmora. This one is interesting because it covers the basic exercise of visualization in Matplotlib.

Tips Dataset

Introduction:

This exercise was created based on the tutorial and documentation from Seaborn
The dataset being used is tips from Seaborn.

Step 1. Import the necessary libraries:

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
sns.set_theme()

Step 2. Import the dataset from this address.

Step 3. Assign it to a variable called tips

url = 'https://raw.githubusercontent.com/guipsamora/pandas_exercises/master/07_Visualization/Tips/tips.csv'
tips = pd.read_csv(url)

Step 4. Delete the Unnamed 0 column

tips.drop(['Unnamed: 0'], axis=1, inplace=True)

Step 5. Plot the total_bill column histogram

sns.histplot(tips['total_bill'])
<AxesSubplot:xlabel='total_bill', ylabel='Count'>

png

Step 6. Create a scatter plot presenting the relationship between total_bill and tip

sns.scatterplot(x=tips['total_bill'], y=tips['tip'])
<AxesSubplot:xlabel='total_bill', ylabel='tip'>

png

Step 7. Create one image with the relationship of total_bill, tip and size.

Hint: It is just one function.

sns.pairplot(tips)
<seaborn.axisgrid.PairGrid at 0x2b3d89aa070>

png

Step 8. Present the relationship between days and total_bill value

sns.barplot(x=tips['day'], y=tips['total_bill'])
<AxesSubplot:xlabel='day', ylabel='total_bill'>

png

Step 9. Create a scatter plot with the day as the y-axis and tip as the x-axis, differ the dots by sex

sns.stripplot(x=tips['tip'], y=tips['day'], hue=tips['sex'], alpha=0.8)
<AxesSubplot:xlabel='tip', ylabel='day'>

png

Step 10. Create a box plot presenting the total_bill per day differetiation the time (Dinner or Lunch)

sns.boxplot(x=tips['tip'], y=tips['day'], hue=tips['time'])
<AxesSubplot:xlabel='tip', ylabel='day'>

png

Step 11. Create two histograms of the tip value based for Dinner and Lunch. They must be side by side.

sns.FacetGrid(tips, col='time').map(sns.histplot, x=tips['tip'])
<seaborn.axisgrid.FacetGrid at 0x2b3dcce9910>

png

Step 12. Create two scatterplots graphs, one for Male and another for Female, presenting the total_bill value and tip relationship, differing by smoker or no smoker

They must be side by side.

sns.FacetGrid(tips, col='sex').map(sns.scatterplot, x=tips['total_bill'], y=tips['tip'], hue=tips['smoker'])
<seaborn.axisgrid.FacetGrid at 0x2b3dcd03d00>

png

BONUS: Create your own question and answer it using a graph.

sns.FacetGrid(tips, col='day', row='time').map(sns.scatterplot, x=tips['total_bill'], y=tips['tip'],\
                                               hue=tips['sex'], style=tips['smoker'], s=tips['size']*25)
<seaborn.axisgrid.FacetGrid at 0x2b3e099dac0>

png

Leave a comment