系統(tǒng):Windows 7
語言版本:Anaconda3-4.3.0.1-Windows-x86_64
編輯器:pycharm-community-2016.3.2
- 這個系列講講Python的科學(xué)計算版塊
- 今天講講seaborn模塊:做幾個點的矩陣圖
Part 1:示例
- 已知
df_1
,有4列["p1", "p2", "p3", "from"]
- 做出
P1、P2、P3
三列的相關(guān)性圖亡问,其實就是兩兩的散點圖,效果如下圖 - 映射實例:有4種樣本肛宋,每種樣本采集5個州藕,合計20個樣本束世。每個樣本檢測其中3個控制點的數(shù)據(jù),對這些數(shù)據(jù)進行可視化顯示床玻,合計數(shù)據(jù)量20*3=60個
矩陣圖
Part 2:代碼
import pandas as pd
import seaborn as sns
from matplotlib import pyplot as plt
dict_1 = {
"p1": [0.5, 0.8, 1.0, 1.2, 1.5, 2.5, 0.9, 0.6, 1.3, 1.0,
1.3, 1.6, 1.9, 2.5, 4.2, 3.5, 2.2, 1.2, 1.5, 0.5],
"p2": [1.3, 2.8, 1.3, 1.4, 6.5, 2.5, 0.9, 0.6, 1.3, 1.0,
1.3, 1.6, 1.9, 2.5, 4.2, 3.5, 1.2, 1.2, 3.5, 2.5],
"p3": [2.5, 0.8, 1.3, 1.2, 1.5, 2.8, 1.9, 0.6, 1.3, 1.1,
1.3, 1.6, 1.1, 2.5, 4.2, 3.9, 2.2, 1.2, 1.5, 0.5],
"from": ["sample1", "sample1", "sample1", "sample1", "sample1",
"sample2", "sample2", "sample2", "sample2", "sample2",
"sample3", "sample3", "sample3", "sample3", "sample3",
"sample4", "sample4", "sample4", "sample4", "sample4"]}
df_1 = pd.DataFrame(dict_1, columns=["p1", "p2", "p3", "from"])
print(df_1)
sns.set(style="ticks", color_codes=True)
g = sns.pairplot(df_1,
hue="from", # 設(shè)置顏色列
palette="Set1", # 調(diào)色板:husl / Set1
markers=["o", "s", "D", "^"], # 設(shè)置標記marker形狀
vars=["p1", "p2", "p3"])
leg = g._legend
leg.set_bbox_to_anchor([0.5, 0, 0.5, 0.5])
plt.show()
代碼截圖
df_1
Part 3:部分代碼解讀
g = sns.pairplot(df_1,
hue="from", # 設(shè)置顏色列
palette="Set1", # 調(diào)色板:husl / Set1
markers=["o", "s", "D", "^"], # 設(shè)置標記marker形狀
vars=["p1", "p2", "p3"])
-
df_1
數(shù)據(jù)源 -
hue
設(shè)置已哪一列作為顏色的分類 -
palette
設(shè)置顏色板毁涉,可以有多種不同的風(fēng)格,如設(shè)置為husl
,效果如下圖 -
markers
設(shè)置每個數(shù)據(jù)的標記形狀 -
vars
設(shè)置參與顯示的列笨枯,如果更改為vars=["p1", "p2"]
薪丁,效果如下圖
husl效果圖
vars=["p1", "p2"]
本文為原創(chuàng)作品,歡迎分享朋友圈
長按圖片識別二維碼馅精,關(guān)注本公眾號
Python 優(yōu)雅 帥氣