seaborn.FacetGrid and seaborn.pairplot

from sklearn.datasets import load_iris
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
import warnings
import numpy as np

warnings.filterwarnings('ignore')
sns.set(style="ticks")

FacetGrid

A FacetGrid can be drawn with up to three dimensions: row, col, and hue
有点像R的ggplot

tips = sns.load_dataset("tips")
tips
total_billtipsexsmokerdaytimesize
016.991.01FemaleNoSunDinner2
110.341.66MaleNoSunDinner3
221.013.50MaleNoSunDinner3
323.683.31MaleNoSunDinner2
424.593.61FemaleNoSunDinner4
23929.035.92MaleNoSatDinner3
24027.182.00FemaleYesSatDinner2
24122.672.00MaleYesSatDinner2
24217.821.75MaleNoSatDinner2
24318.783.00FemaleNoThurDinner2

244 rows × 7 columns

sns.palplot(sns.color_palette("Set2"))
png
g = sns.FacetGrid(data=tips, col="day",row='time',hue='sex',palette=sns.color_palette("Set2")[2:4])
g.map(sns.scatterplot,"total_bill","tip")
g.add_legend()
<seaborn.axisgrid.FacetGrid at 0x18ef1acba88>
png
g = sns.FacetGrid(tips, hue="smoker", palette=sns.color_palette("Set2"), height=5)
g.map(sns.kdeplot,"total_bill")
g.add_legend()
<seaborn.axisgrid.FacetGrid at 0x18ef2499e48>
png

可以添加自定义绘图函数

sns.palplot(sns.light_palette(color=sns.color_palette("Set2")[0]))
png
g = sns.FacetGrid(tips, hue="time", col="time", height=4)
g.map(sns.scatterplot,"total_bill","tip")
g.add_legend()
<seaborn.axisgrid.FacetGrid at 0x18ef374d7c8>
png
def hexbin(x, y, color, **kwargs):
    cmap = sns.light_palette(color, as_cmap=True)
    plt.hexbin(x, y, gridsize=15, cmap=cmap, **kwargs)#类似于scatter

with sns.axes_style("white"):
    g = sns.FacetGrid(tips, hue="time", col="time", height=4)
g.map(hexbin, "total_bill", "tip", extent=[0, 50, 0, 10]);
png

seaborn.PairGrid

g = sns.PairGrid(iris, hue="species",palette='deep')#sns.pairplot(iris, hue="species",palette='deep')
g.map_diag(sns.kdeplot,shade=True)
g.map_offdiag(sns.scatterplot)
g.add_legend()
<seaborn.axisgrid.PairGrid at 0x18ef8c8e948>
png
g = sns.PairGrid(iris, vars=["sepal_length", "sepal_width","petal_length"], hue="species",palette="Set2")
g.map_upper(hexbin,alpha=0.8)
g.map_lower(sns.regplot)
g.map_diag(sns.kdeplot, legend=True,shade=True)
<seaborn.axisgrid.PairGrid at 0x18efbe3ca48>
png




Categories: Python

0 Comments

Leave a Reply

Your email address will not be published.