視覺化庫-seaborn-Facetgrid(第五天)
阿新 • • 發佈:2019-01-09
1. sns.Facetgrid 畫一個基本的直方圖
import numpy as np import pandas as pd from scipy import stats, integrate import matplotlib.pyplot as plt import seaborn as sns sns.set(color_codes=True) np.random.seed(sum(map(ord, 'distributions'))) tips = sns.load_dataset('tips') # 使用sns.Facetgrid 畫一個基本的直方圖 g = sns.FacetGrid(tips, col='time') g.map(plt.hist, 'tip') plt.show()
2 . 新增sns.Facetgrid屬性hue,畫散點圖
g = sns.FacetGrid(tips, col='sex', hue='smoker') g.map(plt.scatter, 'total_bill', 'tip', alpha=0.7) g.add_legend() plt.show()
3. 使用color='0.1'來定義顏色, margin_titles=True把標題分開, fit_reg是否畫擬合曲線,sns.regplot畫迴歸圖
g = sns.FacetGrid(tips, col='time', row='smoker', margin_titles=False) g.map(sns.regplot, 'size', 'total_bill', color='0.1', fit_reg=False, x_jitter=0.1) plt.show()
4. 繪製條形圖,同時使用Categorical 生成col對應順序的條形圖, row_order 寫入新的順序的排列
g = sns.FacetGrid(tips, col='day', size=4, aspect=0.5) g.map(sns.barplot, 'sex', 'total_bill') plt.show()# 指定col順序進行畫圖 from pandas import Categorical # 列印當前的day的順序 ordered_days = tips.day.value_counts().index # 指定順序 ordered_sys = Categorical(['Thur', 'Fri', 'Sat', 'Sun']) g = sns.FacetGrid(tips, col='day', size=4, aspect=0.5, row_order=ordered_days) g.map(sns.barplot, 'sex', 'total_bill') plt.show()
5. 繪製多變數指定顏色,通過palette新增顏色
pal = {'Lunch':'seagreen', 'Dinner':'gray'} # size 指定外面的大小 g = sns.FacetGrid(tips, hue='time', palette=pal, size=5) # s指定圓的大小, linewidth=0.5邊緣線的寬度,egecolor邊緣的顏色 g.map(plt.scatter, 'total_bill', 'tip', s=50, alpha=0.7, linewidth=0.5, edgecolor='white') plt.show()
6. hue_kws={'marker':['^', 'o']}
pal = {'Lunch':'seagreen', 'Dinner':'gray'} # size 指定外面的大小 g = sns.FacetGrid(tips, hue='time', palette=pal, size=5, hue_kws={'marker':['^', 'o']}) # s指定圓的大小, linewidth=0.5邊緣線的寬度,egecolor邊緣的顏色 g.map(plt.scatter, 'total_bill', 'tip', s=50, alpha=0.7, linewidth=0.5, edgecolor='white') plt.show()
7. 設定set_axis_labels 設定座標, g.fig.subplots_adjust(wspace=0.2, hspace) 表示子圖與子圖之間的間隔
with sns.axes_style('white'): g = sns.FacetGrid(tips, row='sex', col='smoker', margin_titles=True, size=2.5) # lw表示球的半徑 g.map(plt.scatter, 'total_bill', 'tip', color='#334488', edgecolor='white', lw=0.1) g.set_axis_labels('Total bill (US Dollars)', 'Tip') # 設定x軸的範圍 g.set(xticks=[10, 30, 50], yticks=[2, 6, 10]) # wspace 和 hspace 設定子圖與子圖之間的距離 g.fig.subplots_adjust(wspace=0.2, hspace=0.2) # 調子圖的偏移 # g.fig.subplots_adjust(left=) plt.show()
8. sns.PairGrid(iris) # 進行兩兩變數繪圖
g = sns.PairGrid(iris)
g.map(plt.scatter)
plt.show()
9. 將主對角線和非對角線的畫圖方式分開
g = sns.PairGrid(iris)
g.map_diag(plt.hist)
g.map_offdiag(plt.scatter)
plt.show()
10 多加上一個屬性進行畫圖操作
g = sns.PairGrid(iris, hue='species') g.map_diag(plt.hist) g.map_offdiag(plt.scatter) g.add_legend() plt.show()
11. 只取其中的兩個屬性進行畫圖vars()
g = sns.PairGrid(iris, hue='species', vars=['sepal_length', 'sepal_width']) g.map_diag(plt.hist) g.map_offdiag(plt.scatter) g.add_legend() plt.show()
12. palette='green_d' 使用漸變色進行畫圖,取的顏色是整數的
g = sns.PairGrid(iris, hue='species', vars=['sepal_length', 'sepal_width'], palette='GnBu_r') g.map_diag(plt.hist) g.map_offdiag(plt.scatter) g.add_legend() plt.show()