How to visualize data in Python?

Practicing Coding Interview Questions in Python

Kirill Smirnov

Data Science Consultant, Altran

matplotlib

import matplotlib.pyplot as plt
  • scatter plot
  • histogram
  • boxplot
Practicing Coding Interview Questions in Python

Dataset

import pandas as pd

diabetes = pd.read_csv('diabetes.csv')
diabetes = diabetes[[
    'n pregnant', 'plasma glucose', 'blood pressure', 'skin thickness',
    'serum insulin', 'bmi', 'age', 'test result']]
print(diabetes.head())
   n pregnant  plasma glucose  blood pressure  skin thickness  serum insulin   bmi  age test result
0           6           148.0            72.0            35.0            NaN  33.6   50    positive
1           1            85.0            66.0            29.0            NaN  26.6   31    negative
2           8           183.0            64.0             NaN            NaN  23.3   32    positive
3           1            89.0            66.0            23.0           94.0  28.1   21    negative
4           0           137.0            40.0            35.0          168.0  43.1   33    positive
Practicing Coding Interview Questions in Python

Scatter plot

Scatter plot

Practicing Coding Interview Questions in Python

Create a scatter plot

import matplotlib.pyplot as plt
plt.scatter(
    diabetes['serum insulin'],
    diabetes['plasma glucose']
)
plt.show()

Scatter plot

Practicing Coding Interview Questions in Python

Create a scatter plot

import matplotlib.pyplot as plt
plt.scatter(
    diabetes['serum insulin'],
    diabetes['plasma glucose']
)

plt.title('Plasma Glucose vs Serum Insulin')

plt.show()

Scatter plot

Practicing Coding Interview Questions in Python

Create a scatter plot

import matplotlib.pyplot as plt
plt.scatter(
    diabetes['serum insulin'],
    diabetes['plasma glucose']
)

plt.title('Plasma Glucose vs Serum Insulin')

plt.xlabel('Serum Insulin')
plt.ylabel('Plasma Glucose')

plt.show()

Scatter plot

Practicing Coding Interview Questions in Python

Histogram

Histogram

Practicing Coding Interview Questions in Python

Create a histogram

import matplotlib.pyplot as plt
plt.hist(diabetes['bmi'])

plt.title('Histogram of BMI index')

plt.xlabel('BMI index')
plt.ylabel('counts')

plt.show()

Histogram

Practicing Coding Interview Questions in Python

Create a histogram

import matplotlib.pyplot as plt
plt.hist(diabetes['bmi'], bins=20)

plt.title('Histogram of BMI index')

plt.xlabel('BMI index')
plt.ylabel('counts')

plt.show()

Histogram

Practicing Coding Interview Questions in Python

Boxplot

Boxplot

Practicing Coding Interview Questions in Python

Create a boxplot

import seaborn as sns
sns.boxplot('test_result', 'bmi', data=diabetes)
plt.title('Boxplot of BMI index')

plt.show()

Boxplot

Practicing Coding Interview Questions in Python

Create a boxplot

import seaborn as sns
sns.boxplot(
    x='test_result',
    y='bmi',
    data=diabetes
)
plt.title('Boxplot of BMI index')

plt.show()

Boxplot

Practicing Coding Interview Questions in Python

Create a boxplot

import seaborn as sns
sns.boxplot(
    y='test_result',
    x='bmi',
    data=diabetes
)
plt.title('Boxplot of BMI index')

plt.show()

Boxplot

Practicing Coding Interview Questions in Python

Let's practice!

Practicing Coding Interview Questions in Python

Preparing Video For Download...