二、散點圖 import seaborn as sns import matplotlib.pyplot as plt sns.set_theme(style = 'whitegrid') # 載入 diamonds 數據集 diamonds = sns.load_dataset('diamonds ...
二、散點圖
import seaborn as sns
import matplotlib.pyplot as plt
sns.set_theme(style = 'whitegrid')
# 載入 diamonds 數據集
diamonds = sns.load_dataset('diamonds')
diamonds.head()
carat | cut | color | clarity | depth | table | price | x | y | z | |
---|---|---|---|---|---|---|---|---|---|---|
0 | 0.23 | Ideal | E | SI2 | 61.5 | 55.0 | 326 | 3.95 | 3.98 | 2.43 |
1 | 0.21 | Premium | E | SI1 | 59.8 | 61.0 | 326 | 3.89 | 3.84 | 2.31 |
2 | 0.23 | Good | E | VS1 | 56.9 | 65.0 | 327 | 4.05 | 4.07 | 2.31 |
3 | 0.29 | Premium | I | VS2 | 62.4 | 58.0 | 334 | 4.20 | 4.23 | 2.63 |
4 | 0.31 | Good | J | SI2 | 63.3 | 58.0 | 335 | 4.34 | 4.35 | 2.75 |
# 畫出 點的大小 和 顏色 不同的高維散點圖
f, ax = plt.subplots(figsize = (6.5, 6.5))
sns.despine(f, left = True, bottom = True)
clarity_ranking = ['I1', 'SI2', 'SI1', 'VS2', 'VS1', 'VVS2', 'VVS1', 'IF']
sns.scatterplot(
x = 'carat', y = 'price',
hue = 'clarity', size = 'depth',
palette = 'ch:r=-.2,d=.3_r',
hue_order = clarity_ranking,
sizes = (1, 8), linewidth = 0,
data = diamonds, ax = ax
) # hue: 色調,size: 大小
sns.scatterplot()
其他案例
來自:https://seaborn.pydata.org/generated/seaborn.scatterplot.html#seaborn.scatterplot
註:seaborn 數據集下載地址 https://github.com/mwaskom/seaborn-data
## 導入數據集
import pandas as pd
tips = pd.read_csv("../../seaborn-data-master/tips.csv")
tips.head()
total_bill | tip | sex | smoker | day | time | size | |
---|---|---|---|---|---|---|---|
0 | 16.99 | 1.01 | Female | No | Sun | Dinner | 2 |
1 | 10.34 | 1.66 | Male | No | Sun | Dinner | 3 |
2 | 21.01 | 3.50 | Male | No | Sun | Dinner | 3 |
3 | 23.68 | 3.31 | Male | No | Sun | Dinner | 2 |
4 | 24.59 | 3.61 | Female | No | Sun | Dinner | 4 |
example 1
## 以 total_bill 為 x 軸,tip 為 y 軸,繪製散點圖
sns.scatterplot(data = tips, x = 'total_bill', y = 'tip')
example 2
## 以 total_bill 為 x 軸,tip 為 y 軸,time 為分類因數,畫出不同 time 類的散點圖(用不同顏色區分)
sns.scatterplot(data = tips, x = 'total_bill', y = 'tip', hue = 'time')
example 3
## 在 example 的基礎上,增加散點形狀區分的條件
sns.scatterplot(data = tips, x = 'total_bill', y = 'tip', hue = 'time', style = 'time')
example 4
## 散點的形狀和顏色的分類依據可以不同
sns.scatterplot(data = tips, x = 'total_bill', y = 'tip', hue = 'day', style = 'time')
example 5
# 若分配給色版的變數是數字,會使用不同的預設調色板
sns.scatterplot(data = tips, x = 'total_bill', y = 'tip', hue = 'size')
example 6
## 可以使用 palette 更改色調
sns.scatterplot(data = tips, x = 'total_bill', y = 'tip', hue = 'size', palette = 'deep')
example 7
## 如果有大量的唯一數值,圖例將顯示一個具有代表性的等間距集合
tip_rate = tips.eval('tip / total_bill').rename('tip_rate')
sns.scatterplot(data = tips, x = 'total_bill', y = 'tip', hue = tip_rate)
example 8
## 在 example 5 的基礎上還能增加點的大小
sns.scatterplot(data = tips, x = 'total_bill', y = 'tip', hue = 'size', size = 'size')
example 9
## 可以更改散點的大小
sns.scatterplot(
data = tips, x = 'total_bill', y = 'tip', hue = 'size', size = 'size',
sizes = (20, 200), legend = 'full'
)
example 10
## 傳入一個元組或matplotlib.colors.Normalize以控制色調
sns.scatterplot(
data = tips, x = 'total_bill', y = 'tip', hue = 'size', size = 'size',
sizes = (20, 200), hue_norm = (0,7), legend = 'full'
)
example 11
## 傳入一個字典控制散點形狀
markers = {'Lunch' : 's', 'Dinner' : 'X'}
sns.scatterplot(data = tips, x = 'total_bill', y = 'tip', style = 'time', markers = markers)
example 12
sns.scatterplot(data = tips, x = 'total_bill', y = 'tip', s = 100, color = '.2', marker = "+")
example 13
## 可以繪製不同 類型 數據 的 時間序列圖像
import numpy as np
import pandas as pd
index = pd.date_range("1 1 2000", periods=100, freq='m', name='date')
data = np.random.randn(100, 4).cumsum(axis=0)
wide_df = pd.DataFrame(data, index, ['a','b','c','d'])
sns.scatterplot(data=wide_df)
wide_df.head()
a | b | c | d | |
---|---|---|---|---|
date | ||||
2000-01-31 | 3.381766 | -0.360579 | -0.080106 | 1.578611 |
2000-02-29 | 2.724598 | 0.351141 | -0.914548 | 1.825725 |
2000-03-31 | 2.276614 | 0.855341 | -0.227480 | 0.075641 |
2000-04-30 | 1.385905 | 0.793799 | -0.392478 | -0.053513 |
2000-05-31 | -0.011497 | 0.985883 | -0.829674 | 1.539929 |
example 14
## 按類別拆分數據,畫到不同的子圖上
sns.relplot(
data = tips, x = 'total_bill', y = 'tip',
col = 'time', hue = 'day', style = 'day',
kind = 'scatter'
)