Hướng dẫn cdf plot python seaborn - cdf âm mưu trăn seaborn

Trong bài viết này mình giới thiệu với mọi người các biểu đồ mình hay dùng để visualize dữ liệu bằng seaborn.

Vì là intro nên để hiểu rõ hơn ý nghĩa của từng biểu đồ, mọi người có thể đọc thêm tại series data visualization của anh Ngọc tại đây và của anh Khánh tại đây

I. Seaborn vs Matplotlib

Seaborn là mở rộng của matplotlib, có nghĩa là seaborn kế thừa từ matplotlib và cũng chính vì vậy, seaborn khong thể thay thế hoàn toàn được matplotlib.

Mình sẽ so sánh nhanh giữa 2 tools visualize này nhé!

MatplotlibSeaborn
Chức năng Thường được sử dụng biểu diễn các biểu đồ đơn giản như: bars, pies, lines, scatter,.. Về cơ bản, seaborn cung cấp nhiều dạng biểu đồ hơn, với cú pháp đơn giản và hình vẽ "default" thể hiện ra cũng màu mè và thú vị hơn.
Xử lý đa hình Cần phải tắt bỏ hình hiện tại để hiện hình tiếp theo Tự động tạo nhiều hinh (Có thể gây tràn bộ nhớ)
Tính linh hoạt Có khả năng tùy biến cao, mạnh mẽ Cung cấp nhiều giao diện thường được sử dụng
Dataframes và Arrays Hoạt động với dataframes và arrays, được kế thừa từ matlab Hoạt động với toàn bộ dữ liệu và trực quan hơn matplotlib

Đại khái là thế . Nào bắt đầu thôi!!

Hướng dẫn cdf plot python seaborn - cdf âm mưu trăn seaborn
. Nào bắt đầu thôi!!

II. Các biểu đồ thường dùng trong seaborn

Trong bài viết này mình sẽ sử dụng data tips, iris vaf planets nhé!

# Import library
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
# Load data
tips = sns.load_dataset('tips')
iris = sns.load_dataset('iris')
planets = sns.load_dataset('planets')

2.1. Pair plots

Khi muốn nhìn tổng quan dữ liệu và mối tương quan giữa các chiều dữ liệu theo từng cặp với nhau, thì pair plots là lựa chọn vô cùng hoàn hảo (Các features dạng số, hiển nhiên :v):

sns.pairplot(iris, hue='species', height=1.5)

Hướng dẫn cdf plot python seaborn - cdf âm mưu trăn seaborn

2.2. Countplot

Countplot sẽ trả về số lượng của từng category dưới dạng cột

sns.countplot(x = tips['day'])

Hướng dẫn cdf plot python seaborn - cdf âm mưu trăn seaborn

Và với một chút điều chỉnh, chúng ta có thể sắp xếp các cột theo thứ tự tăng hoặc giảm (Có trường hợp xấp xỉ mà nhìn ko ra chả hạn ):

Hướng dẫn cdf plot python seaborn - cdf âm mưu trăn seaborn
):

sns.countplot(x = tips['day'],
             order = tips['day'].value_counts().index)

Hướng dẫn cdf plot python seaborn - cdf âm mưu trăn seaborn

Hoặc cũng có thể dùng countplot để xem tương quan giữa 2 features: Ví dụ : Xem các ngày trong tuần nam, nữ ai trả nhiều hơn

sns.countplot(x= 'day', hue= 'sex', data= tips)

Hướng dẫn cdf plot python seaborn - cdf âm mưu trăn seaborn

2.3. Histplot và Distplot (Trong version cũ của seaborn)

Displot giúp ta xem được sự phân phối (Distribution) của dữ liệu

sns.distplot(tips['total_bill'], bins= 7) # bins: chia ra thành bins cột

Hướng dẫn cdf plot python seaborn - cdf âm mưu trăn seaborn

Sử dụng

# Load data
tips = sns.load_dataset('tips')
iris = sns.load_dataset('iris')
planets = sns.load_dataset('planets')
8 thì ko còn thấy pdf nữa :v

sns.histplot(tips['total_bill'], bins= 7)

2.4. Joint distributions

Tương tự như pairplot, chúng ta có thể sử dụng

# Load data
tips = sns.load_dataset('tips')
iris = sns.load_dataset('iris')
planets = sns.load_dataset('planets')
9 để xem phân phối dữ liệu

sns.jointplot(x= 'tip', y= 'total_bill', kind= 'hex', data= tips)

Hướng dẫn cdf plot python seaborn - cdf âm mưu trăn seaborn

Hoặc:

sns.jointplot(x= 'tip', y= 'total_bill', kind= 'kde', data= tips)

Hướng dẫn cdf plot python seaborn - cdf âm mưu trăn seaborn

Hoặc:

# Load data
tips = sns.load_dataset('tips')
iris = sns.load_dataset('iris')
planets = sns.load_dataset('planets')
0

Hướng dẫn cdf plot python seaborn - cdf âm mưu trăn seaborn

Có rất nhiều lựa chọn có thể thử, tùy vào việc bạn nhìn vào biểu đồ nào thấy dễ hình dung về phân phối hơn!

2.5. Bar plots

Time series data có thể biểu diễn thông qua

sns.pairplot(iris, hue='species', height=1.5)
0 (version hiện tại khuyên dùng
sns.pairplot(iris, hue='species', height=1.5)
1) . Xem thử việc sử dụng chúng với data planets nào

# Load data
tips = sns.load_dataset('tips')
iris = sns.load_dataset('iris')
planets = sns.load_dataset('planets')
1

Hướng dẫn cdf plot python seaborn - cdf âm mưu trăn seaborn

Thử catplot với

sns.pairplot(iris, hue='species', height=1.5)
2 xem sao nhé!!

2.6. Heatmap

Cái này chắc quá quen thuộc với mọi người khi muốn plot Correlation Matrix rồi nhỉ?

# Load data
tips = sns.load_dataset('tips')
iris = sns.load_dataset('iris')
planets = sns.load_dataset('planets')
2

Hướng dẫn cdf plot python seaborn - cdf âm mưu trăn seaborn

2.7. Box plot, Swarm plot và Violin plot

Hướng dẫn cdf plot python seaborn - cdf âm mưu trăn seaborn

Ý tưởng chung là giúp ta dễ hình dung hơn phân phối dữ liệu và phát hiện outlier

Hướng dẫn cdf plot python seaborn - cdf âm mưu trăn seaborn

2.7.1. Box plot

# Load data
tips = sns.load_dataset('tips')
iris = sns.load_dataset('iris')
planets = sns.load_dataset('planets')
3

Hướng dẫn cdf plot python seaborn - cdf âm mưu trăn seaborn

Và với

sns.pairplot(iris, hue='species', height=1.5)
3 để xem ai hút thuốc
Hướng dẫn cdf plot python seaborn - cdf âm mưu trăn seaborn

# Load data
tips = sns.load_dataset('tips')
iris = sns.load_dataset('iris')
planets = sns.load_dataset('planets')
4

Hướng dẫn cdf plot python seaborn - cdf âm mưu trăn seaborn

2.7.2. Swarm plot

# Load data
tips = sns.load_dataset('tips')
iris = sns.load_dataset('iris')
planets = sns.load_dataset('planets')
5

Hướng dẫn cdf plot python seaborn - cdf âm mưu trăn seaborn

2.7.3. Violin plot ())

# Load data
tips = sns.load_dataset('tips')
iris = sns.load_dataset('iris')
planets = sns.load_dataset('planets')
6

Hướng dẫn cdf plot python seaborn - cdf âm mưu trăn seaborn

2.8. Pie chart

(Đổi gió dùng chút matplotlib.pyplot ))

Hướng dẫn cdf plot python seaborn - cdf âm mưu trăn seaborn
))

# Load data
tips = sns.load_dataset('tips')
iris = sns.load_dataset('iris')
planets = sns.load_dataset('planets')
7

Hướng dẫn cdf plot python seaborn - cdf âm mưu trăn seaborn

III. Kết luận

Vậy là mình đã giới thiệu đơn giản với mọi người các biểu đồ mình thường dùng khi sử dụng seaborn, để hiểu rõ hơn cách dùng mọi người có thể xem tại source code của seaborn. Cảm ơn đã dành thời gian đọc bài viết của mình. See ya!!! (KxSS)

Reference

https://jakevdp.github.io/PythonDataScienceHandbook/04.14-visualization-with-seaborn.html