Entering the Multivariate Analysis Arena: Scatter Plots and Correlation of Variables

Welcome to the next guide on our remarkable voyage! Moving into our discussion on multivariate data visualization, we'll introduce you to scatter plots, one of the most powerful tools for visualizing the relationship between multiple variables. We'll guide you through plotting scatter plots for different variable pairs in our Titanic dataset and stepping further into correlating these variables.

Why is it important to understand the correlation among variables? Imagine, we want to know whether passengers in the higher classes were more likely to survive. Or maybe we are interested in the fare paid for a ticket correlates with the survival on Titanic. Finding correlations among variables will help us generate hypotheses, create insightful visualizations, and eventually enable efficient predictive modeling.

By the end of this lesson, you'll be conversant with how scatter plots and correlation techniques can be used to explore and visualize relationships between different features present in a multivariate dataset.

Scatter `Plots`: An Introduction

A scatter plot is a versatile visualization tool that can disclose the relationship, if any exists, between two variables. Each point on the plot represents an observation in the dataset, with its position along the X and Y axes representing the values of two variables.

Let's initiate with a scatter plot depicting the relationship between age and fare.

import seaborn as sns
import matplotlib.pyplot as plt

# Load Titanic dataset
titanic = sns.load_dataset('titanic')

# Display Scatter Plot of Age vs Fare
sns.scatterplot(x='age', y='fare', data=titanic)
plt.title("Age vs Fare")
plt.show()

image

In scatterplot() function:

  • x is for the data along the horizontal axis
  • y is for the data along the vertical axis
  • data: it's a required parameter, providing the data source.
Further into Scatter `Plots`

Looking at the scatter plot, there seems to be no apparent correlation between age and fare. But what if we consider another variable - class in our analysis? We might hypothesize that higher class passengers (1st or 2nd) could have paid more fare regardless of age.

Using the hue parameter, we can visualize this by adding color discrimination to our scatter plot. Setting hue='pclass' will provide different colors to data points belonging to different passenger classes:

sns.scatterplot(x='age', y='fare', hue='pclass', data=titanic)
plt.title("Age vs Fare (Separate colors for Passenger Class)")
plt.show()

image

hue: you can think of it as a fourth dimension of data, it can determine the color of data points using an additional variable.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal