0Pricing
Pandas & NumPy Academy · 강의

산점도와 쌍별 그래프

sns.scatterplot으로 세 번째 변수에 따라 색을 지정한 산점도를 만들고 sns.pairplot으로 모든 쌍별 관계를 시각화합니다.

산점도와 쌍별 그래프은(는) CoddyKit의 무료 Pandas & NumPy Academy 강의입니다. 이것은 4개 중 3번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Pandas & NumPy Academy 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Pandas & NumPy Academy 강의에는 총 4개의 강의가 포함되어 있습니다.

이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.

Visualising Relationships Between Variables

Distribution plots show one variable at a time. When you want to understand the relationship between two numeric variables, you need scatter plots and related visualisations. These help you detect correlation (do both variables increase together?), clusters (are there subgroups in the data?), and outliers (are there unusual points far from the main cloud?). Seaborn makes all of these easy with scatterplot and pairplot.

import seaborn as sns
import matplotlib.pyplot as plt

# Load the classic iris dataset
iris = sns.load_dataset('iris')
print(iris.head())
print('\nShape:', iris.shape)

Basic Scatter Plot with sns.scatterplot

sns.scatterplot(data, x, y) plots each observation as a point at its (x, y) coordinates. Unlike Matplotlib's plt.scatter, Seaborn's version automatically links to a DataFrame and integrates with hue, size, and style semantics. The result is informative multi-variable plots with a single function call and an automatic legend.

import seaborn as sns
import matplotlib.pyplot as plt

tips = sns.load_dataset('tips')

sns.scatterplot(data=tips, x='total_bill', y='tip')
plt.title('Tip vs Total Bill')
plt.xlabel('Total Bill ($)')
plt.ylabel('Tip ($)')
plt.show()

Encoding a Third Variable with hue

The hue parameter assigns a colour to each point based on a third variable — either categorical or numeric. When hue is a categorical column (like 'smoker'), Seaborn uses distinct colours. When hue is a numeric column, it uses a sequential colour map. This lets you see three variables at once on a 2D plot without adding a third axis.

import seaborn as sns
import matplotlib.pyplot as plt

tips = sns.load_dataset('tips')

sns.scatterplot(
    data=tips,
    x='total_bill',
    y='tip',
    hue='time',       # Lunch vs Dinner
    style='smoker',   # marker shape
    palette='deep'
)
plt.title('Tip vs Bill by Time and Smoker Status')
plt.legend(bbox_to_anchor=(1, 1))
plt.tight_layout()
plt.show()

Encoding Size and Style

Beyond hue, Seaborn scatter plots support size (point area encodes a numeric variable) and style (marker shape encodes a categorical variable). Using all three semantics simultaneously can reveal complex multi-variable patterns but risks overloading the viewer. A practical guideline is to use hue first (most salient), style second, and size only when necessary.

import seaborn as sns
import matplotlib.pyplot as plt

tips = sns.load_dataset('tips')

sns.scatterplot(
    data=tips,
    x='total_bill',
    y='tip',
    hue='day',
    size='size',      # party size controls marker area
    sizes=(20, 200),  # min and max dot area
    alpha=0.7
)
plt.title('Tip vs Bill — Colour=Day, Size=Party Size')
plt.legend(bbox_to_anchor=(1, 1))
plt.tight_layout()
plt.show()

Adding a Regression Line with regplot

sns.regplot() draws a scatter plot and overlays a linear regression line with a shaded 95% confidence band. This is useful for visualising the strength and direction of the linear relationship between two variables. The confidence band narrows in regions with dense data and widens at the edges where fewer points inform the fit.

import seaborn as sns
import matplotlib.pyplot as plt

tips = sns.load_dataset('tips')

sns.regplot(
    data=tips,
    x='total_bill',
    y='tip',
    scatter_kws={'alpha': 0.4},
    line_kws={'color': 'red'}
)
plt.title('Tip vs Bill with Regression Line')
plt.show()

lmplot: Faceted Regression Plots

sns.lmplot() is the figure-level version of regplot. It supports hue (separate regression lines per group in the same panel) and col/row (separate panels). This is powerful for testing whether the relationship between two variables differs across subgroups — for example, whether the bill-to-tip relationship is steeper for dinner than for lunch.

import seaborn as sns
import matplotlib.pyplot as plt

tips = sns.load_dataset('tips')

# Separate regression line per time (Lunch / Dinner)
sns.lmplot(
    data=tips,
    x='total_bill',
    y='tip',
    hue='time',
    scatter_kws={'alpha': 0.4},
    height=5
)
plt.suptitle('Regression by Meal Time', y=1.02)
plt.show()

Introduction to Pair Plots

sns.pairplot(df) creates a grid of scatter plots for every pair of numeric columns in the DataFrame, with distribution plots on the diagonal. This is the fastest way to visualise all pairwise relationships in a dataset in one call. For a DataFrame with n numeric columns, pairplot creates an n×n grid. It is most practical when n is between 3 and 8 — larger grids become too small to read.

import seaborn as sns
import matplotlib.pyplot as plt

iris = sns.load_dataset('iris')

# Create pairplot coloured by species
sns.pairplot(iris, hue='species', diag_kind='kde', plot_kws={'alpha': 0.5})
plt.suptitle('Iris Dataset Pairplot', y=1.02)
plt.show()

Customising the Diagonal of pairplot

The diag_kind parameter controls what appears on the diagonal of the pairplot grid. Use 'hist' for histograms or 'kde' for kernel density estimates. The diagonal shows the univariate distribution of each variable, while the off-diagonal panels show pairwise scatter plots. You can also use corner=True to show only the lower triangle, halving the number of panels and reducing redundancy.

import seaborn as sns
import matplotlib.pyplot as plt

iris = sns.load_dataset('iris')

# Show only lower triangle
sns.pairplot(
    iris,
    hue='species',
    diag_kind='hist',
    corner=True
)
plt.show()

PairGrid for Full Customisation

sns.PairGrid gives you full control over which plot type appears in each section of the matrix. Use g.map_upper(), g.map_lower(), and g.map_diag() to assign different functions (like regplot on the upper triangle and kdeplot on the lower). This produces publication-quality plots where each panel conveys distinct information about the variable pair.

import seaborn as sns
import matplotlib.pyplot as plt

iris = sns.load_dataset('iris')
numeric_iris = iris.drop(columns=['species'])

g = sns.PairGrid(numeric_iris)
g.map_upper(sns.scatterplot, alpha=0.3)
g.map_lower(sns.kdeplot, fill=True)
g.map_diag(sns.histplot, kde=True)

plt.suptitle('Custom PairGrid', y=1.02)
plt.show()

Identifying Correlation in Scatter Plots

When reading a scatter plot, estimate the direction (does the cloud slope up or down?), strength (how tightly clustered are the points around the trend?), and form (linear or curved?) of the relationship. Correlation coefficient r ranges from -1 (perfect negative) to +1 (perfect positive), with 0 meaning no linear relationship. Remember: correlation is not causation, and a non-linear relationship can have r ≈ 0.

import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np

np.random.seed(0)
x = np.linspace(0, 10, 100)

fig, axes = plt.subplots(1, 3, figsize=(12, 4))

# Strong positive correlation
axes[0].scatter(x, x + np.random.normal(0, 0.5, 100))
axes[0].set_title('Strong Positive (r≈0.99)')

# Weak correlation
axes[1].scatter(x, np.random.normal(5, 3, 100))
axes[1].set_title('No Correlation (r≈0)')

# Non-linear (quadratic) — r can be low
axes[2].scatter(x, (x-5)**2 + np.random.normal(0, 1, 100))
axes[2].set_title('Non-linear (r low despite pattern)')

plt.tight_layout()
plt.show()

jointplot for Bivariate with Marginals

sns.jointplot() combines a central bivariate plot with marginal univariate plots on the top and right edges. Pass kind='scatter', 'hex', 'kde', or 'reg'. The hex kind is useful when you have thousands of overlapping points — it bins them into hexagons and uses colour to show density, solving the overplotting problem that makes scatter plots unreadable for large datasets.

import seaborn as sns
import matplotlib.pyplot as plt

tips = sns.load_dataset('tips')

# KDE joint plot with marginal distributions
sns.jointplot(
    data=tips,
    x='total_bill',
    y='tip',
    kind='reg',
    marginal_kws={'bins': 20}
)
plt.suptitle('Joint Distribution of Bill and Tip', y=1.02)
plt.show()

Quick Check

Test your understanding of Seaborn scatter and pair plots from this lesson.

Lesson Recap

In this lesson you learned: sns.scatterplot encodes up to four variables using x, y, hue, size, and style, sns.regplot/lmplot overlays a regression line to quantify linear relationships, and sns.pairplot produces an all-pairs grid ideal for exploratory analysis of multi-variable datasets. Next up we explore heatmaps for visualising correlation matrices.

자주 묻는 질문

“산점도와 쌍별 그래프” 강의는 무료인가요?

네 — “산점도와 쌍별 그래프” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Pandas & NumPy Academy 강의 전체를 잠금 해제할 수 있습니다. Pandas & NumPy Academy 강의에는 총 4개의 강의가 포함되어 있습니다.

“산점도와 쌍별 그래프”에서 뭘 배우나요?

sns.scatterplot으로 세 번째 변수에 따라 색을 지정한 산점도를 만들고 sns.pairplot으로 모든 쌍별 관계를 시각화합니다. 브라우저에서 직접 실행하는 실습 코드로 Pandas & NumPy Academy을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.

Pandas & NumPy Academy을(를) 시작하는 데 경험이 필요한가요?

사전 경험은 필요하지 않습니다. CoddyKit의 Pandas & NumPy Academy은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 3번째 강의입니다.

“산점도와 쌍별 그래프” 강의는 얼마나 걸리나요?

대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.

이 Pandas & NumPy Academy 강의에서 코드를 작성하고 실행할 수 있나요?

네. 모든 Pandas & NumPy Academy 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.

이 강의의 모든 강의

  1. 분포 그래프: histplot과 kdeplot
  2. 범주형 그래프: boxplot, barplot, violinplot
  3. 산점도와 쌍별 그래프
  4. 상관 행렬 히트맵
← Pandas & NumPy Academy(으)로 돌아가기