1
+ # 導入必要的程式庫
2
+ import pandas as pd
3
+ import seaborn as sns
4
+ from matplotlib import pyplot as plt
5
+
6
+ # 取得鳶尾花資料集
7
+ df = sns .load_dataset ('iris' )
8
+ # 箱形圖顯示了數據的總體分布,同時繪製了異常值的數據點。這個物理點讓它們的特定值在樣本之間容易被識別和比較。
9
+ # sns.boxplot(data = df, orient = "h")
10
+ # 當一個或兩個正在研究的變數是分類的時,我們使用像條帶線()、swarmplot()等的圖。
11
+ # 查看到每個物種petal_length的差異。但是,散點圖的主要問題是散點圖上的點重疊。
12
+ # sns.stripplot(x = "species", y = "petal_length", data = df)
13
+ # 上述散點圖的主要問題是散點圖上的點重疊。我們使用"抖動"參數來處理此類方案。
14
+ # 抖動會為數據添加一些隨機雜訊。此參數將沿分類軸調整位置。
15
+ # sns.stripplot(x = "species", y = "petal_length", data = df, jitter=True)
16
+ # 另一個可以用作「抖動」 的替代選項是函數群圖(), 此函數將散點圖的每個點都放在分類軸上,從而避免重疊點
17
+ # sns.swarmplot(x = "species", y = "petal_length", data = df)
18
+
19
+ # 核密度估計(Kernel Density Estimates, KDE)
20
+ # 可以觀察每個情節的變化。繪圖採用矩陣格式,其中行名表示 x 軸,列名稱表示 y 軸。
21
+ # 對角線圖是內核密度圖,其中其他圖是散點圖
22
+ # 內核密度估計是估計變數分佈的非參數化方法。
23
+ # sns.set_style("ticks")
24
+ # sns.pairplot(df,hue = 'species',diag_kind = "kde",kind = "scatter",palette = "husl")
25
+ # 可以在上三角形和下三角形使用不同的函數來查看關係的不同方面
26
+ # g = sns.pairplot(df,hue = 'species',diag_kind = "kde",kind = "scatter",palette = "husl")
27
+ # g.map_upper(plt.scatter)
28
+ # g.map_lower(sns.kdeplot, cmap = "Blues_d")
29
+ # g.map_diag(sns.kdeplot, lw = 3, legend = False)
30
+
31
+ # plt.show()
32
+
33
+ # FacetGrid 類有助於可視化一個變數的分佈,以及使用多個面板在數據集子集中分別顯示多個變數之間的關係
34
+ # 作業:取得另一個 dataset:titanic
35
+ df .info ()
36
+ # 做箱形圖
37
+ sns .boxplot (data = df , orient = "h" )
38
+ # 利用 FacetGrid 繪圖並分析
39
+ g = sns .FacetGrid (data = df , col = 'species' ,hue = 'species' )
40
+ g .map (plt .plot , 'X' , 'Y1' )
41
+ plt .show ()
42
+ # 繪製小提琴圖
43
+ sns .violinplot (x = "day" , y = "total_bill" , hue = "smoker" , data = df , palette = "muted" ,split = True )
44
+ plt .show ()
0 commit comments