栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

【星海随笔】Seaborn研究

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【星海随笔】Seaborn研究

sns.factorplot(x="time", y="total_bill", hue="smoker",
               col="day", data=tips, kind="box", size=4, aspect=.5)

#生成盒图 kind=“box”
#每一列是day的数据
#使用数据集 tips.data
#x 轴
#没有的图片位预留 hue=“smoker”
#整个图的宽度 aspect=.5
#整个图的大小 size=4
#x 轴使用数据中的 time 名称
#y 轴使用数据中的 total_bill

%matplotlib inline
import numpy as np
import pandas as pd
import seaborn as sns
from scipy import stats
import matplotlib as mpl
import matplotlib.pyplot as plt

sns.set(style="ticks")
np.random.seed(sum(map(ord, "axis_grids")))
tips = sns.load_dataset("tips")
#载入数据

g = sns.FacetGrid(tips, col="time")
#实例化一个对象

g.map(plt.hist,"tip")
#hist 表示直方图,tip表示使用的数据,在载入的tips里有这一列数据。

FaceGrid

g = sns.FacetGrid(tips, col="sex", hue="smoker")
#根据tips绘制col的数据,还可以增加其他参数,来区别col值,例如引入一个smoker,False或者True的一列,会自动分成两种颜色进行区分。 
g.map(plt.scatter, "total_bill", "tip", alpha=.7)
g.add_legend()
g = sns.FacetGrid(tips, row="smoker", col="time", margin_titles=True)
g.map(sns.regplot, "size", "total_bill", color=".1", fit_reg=False, x_jitter=.1);

#先实力化一个对象,行设置为 smoker ,列设置为 time
#画散点图,regplot
#增加两个数据参考项,size和total_bill 。构成矩形图
#fit_reg如果为True则画出线性回归

指定顺序展示
Categorical

from pandas import Categorical
ordered_days = tips.day.value_counts().index
print (ordered_days)
ordered_days = Categorical(['Thur', 'Fri', 'Sat', 'Sun'])
g = sns.FacetGrid(tips, row="day", row_order=ordered_days,
                  size=1.7, aspect=4,)
g.map(sns.boxplot, "total_bill");

可以对调色板,palette 自定义

pal = dict(Lunch="seagreen", Dinner="gray")
g = sns.FacetGrid(tips, hue="time", palette=pal, size=5)
g.map(plt.scatter, "total_bill", "tip", s=50, alpha=.7, linewidth=.5, edgecolor="white")
g.add_legend();

还可以自定义marker

g = sns.FacetGrid(tips, hue="sex", palette="Set1", size=5, hue_kws={"marker": ["^", "v"]})
g.map(plt.scatter, "total_bill", "tip", s=100, linewidth=.5, edgecolor="white")
g.add_legend();

矩形图

with sns.axes_style("white"):
    g = sns.FacetGrid(tips, row="sex", col="smoker", margin_titles=True, size=2.5)
g.map(plt.scatter, "total_bill", "tip", color="#334488", edgecolor="white", lw=.5);
g.set_axis_labels("Total bill (US Dollars)", "Tip");
g.set(xticks=[10, 30, 50], yticks=[2, 6, 10]);
g.fig.subplots_adjust(wspace=.02, hspace=.02);
#g.fig.subplots_adjust(left  = 0.125,right = 0.5,bottom = 0.1,top = 0.9, wspace=.02, hspace=.02)

花瓣图

iris = sns.load_dataset("iris")
g = sns.PairGrid(iris)
g.map(plt.scatter)

不同样式的花瓣图

g = sns.PairGrid(iris, hue="species")
g.map_diag(plt.hist)
g.map_offdiag(plt.scatter)
g.add_legend()

设置用的值

g = sns.PairGrid(iris, vars=["sepal_length", "sepal_width"], hue="species")
g.map(plt.scatter);

调色板

g = sns.PairGrid(tips, hue="size", palette="GnBu_d")
g.map(plt.scatter, s=50, edgecolor="white")
g.add_legend();

热度图

%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np; 
np.random.seed(0)
import seaborn as sns;
sns.set()
uniform_data = np.random.rand(3, 3)
print (uniform_data)
heatmap = sns.heatmap(uniform_data)

还可以设置最小最大值

ax = sns.heatmap(uniform_data, vmin=0.2, vmax=0.5)

center 可以设置颜色

normal_data = np.random.randn(3, 3)
print (normal_data)
ax = sns.heatmap(normal_data, center=0)

热度图,X为year, Y为month, 显示 passengers 的值,呈现热度显示

flights = sns.load_dataset("flights")
flights.head()
flights = flights.pivot("month", "year", "passengers")
print (flights)
ax = sns.heatmap(flights)

显示数字

ax = sns.heatmap(flights, annot=True,fmt="d")

设置行与行之间的间隔

ax = sns.heatmap(flights, linewidths=.5)

设置颜色模板

ax = sns.heatmap(flights, cmap="YlGnBu")

test

ax = sns.heatmap(flights, cbar=False,cmap="YlGnBu",linewidths=.5,annot=True,fmt="d")

例子: 使用panda求出相关系数,相关程度,并形成热成像

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/918179.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号