python如何安装seaborn模块_seaborn模块的基本使用

Seaborn是基于matplotlib的Python可视化库。 它提供了一个高级界面来绘制有吸引力的统计图形。Seaborn其实是在matplotlib的基础上进行了更高级的API封装,从而使得作图更加容易,不需要经过大量的调整就能使你的图变得精致。但应强调的是,应该把Seaborn视为matplotlib的补充,而不是替代物。

一、整体布局风格设置

importseaborn as snsimportnumpy as npimportmatplotlib.pyplot as pltimportmatplotlib as mpl%matplotlib inline #这句话的意思就是可以直接显示图

def sinplot(flip = 1):

x= np.linspace(0, 14, 100)for i in range(1, 7):

plt.plot(x, np.sin(x+ i*0.5) * (7 - i) *flip)

sinplot()#采用的是matplot默认的一些参数画图

1313448-20190910150111868-1401810428.png

#接下来采用的是seaborn的画图的风格,首先看看默认的变化

sns.set()

sinplot()

1313448-20190910150300706-213509238.png

#seaborn主要分为五种主题风格:darkgrid whitegrid dark white ticks

sns.set_style("whitegrid")

data= np.random.normal(size=(20, 6)) + np.arange(6) / 2sns.boxplot(data=data)

1313448-20190910150448280-805769135.png

高斯分布的概率密度函数

png.latex?f(x)=\frac{1}{\sqrt{2\pi&space;}\sigma&space;}exp(\frac{(x-\mu&space;)^{2}}{2\sigma&space;^{2}})

numpy中

numpy.random.normal(loc=0.0, scale=1.0, size=None)

参数的意义为:

loc:float

概率分布的均值,对应着整个分布的中心center

scale:float

概率分布的标准差,对应于分布的宽度,scale越大越矮胖,scale越小,越瘦高

size:int or tuple of ints

输出的shape,默认为None,只输出一个值

我们更经常会用到np.random.randn(size)所谓标准正太分布(μ=0, σ=1),对应于np.random.normal(loc=0, scale=1, size)

所以上面从正态分布随机产生20*6的二维数组,加上后面的6*1的一维数组。

boxplot 箱线图

46bbe6a3ea254bdc8ccdb28f64850e9f.jpg

"盒式图" 或叫 "盒须图" "箱形图",,其绘制须使用常用的统计量,能提供有关数据位置和分散情况的关键信息,尤其在比较不同的母体数据时更可表现其差异。

如上图所示,标示了图中每条线表示的含义,其中应用到了分位值(数)的概念。

主要包含五个数据节点,将一组数据从大到小排列,分别计算出他的上边缘,上四分位数,中位数,下四分位数,下边缘。

具体用法如下:seaborn.boxplot(x=None, y=None, hue=None, data=None, order=None, hue_order=None, orient=None, color=None, palette=None, saturation=0.75, width=0.8, fliersize=5, linewidth=None, whis=1.5, notch=False, ax=None, **kwargs)

Parameters:

x, y, hue: names of variables in data or vector data, optional #设置 x,y 以及颜色控制的变量

Inputs for plotting long-form data. See examples for interpretation.

data : DataFrame, array, or list of arrays, optional #设置输入的数据集

Dataset for plotting. If x and y are absent, this is interpreted as wide-form. Otherwise it is expected to be long-form.

order, hue_order : lists of strings, optional #控制变量绘图的顺序

Order to plot the categorical levels in, otherwise the levels are inferred from the data objects.

sns.set_style("dark")

sinplot()

1313448-20190910150852041-7709081.png

sns.set_style("white")

sinplot()

1313448-20190910150915698-179899117.png

sns.set_style("ticks")

sinplot()

1313448-20190910150941492-854338841.png

sinplot()

sns.despine()#在ticks基础上去掉上面的和右边的端线

1313448-20190910151040077-1648800219.png

二、风格细节的设置

importseaborn as snsimportnumpy as npimportmatplotlib.pyplot as pltimportmatplotlib as mpl%matplotlib inline #这句话的意思就是可以直接显示图

sns.violinplot(data)

sns.despine(offset=30) #显示的是图形的最低点距横轴的距离

1313448-20190910155305482-885232961.png

sns.set_style("whitegrid")

sns.boxplot(data=data, palette="deep")

sns.despine(left= True) #把左边的轴去掉了。

1313448-20190910155325545-1717048353.png

with sns.axes_style("darkgrid"): #通过with可以显示一种一个子图的风格。

plt.subplot(211)

sinplot()

plt.subplot(212)

sinplot(-1)

1313448-20190910155350865-1313413308.png

#另一种调节图中线的粗细程度的风格:paper talk poster notebook,前三种风格的线越来越粗,最后一个notebook是可以通过参数的修改的

sns.set()

sns.set_context("paper")

plt.figure(figsize=(8, 6))

sinplot()

1313448-20190910155455288-33884417.png

sns.set_context("talk")

plt.figure(figsize=(8, 6))

sinplot()

1313448-20190910155527395-1903728584.png

sns.set_context("poster")

plt.figure(figsize=(8, 6))

sinplot()

1313448-20190910155625181-437206188.png

sns.set_context("notebook", font_scale = 2, rc={"lines.linewidth" : 4.5}) #这个是可以的设置的参数的,font_scale是改变坐标轴上的大小,rc中的line.linewidth是改变线的粗细

plt.figure(figsize=(8, 6))

sinplot()

1313448-20190910155555214-164524694.png

三、调色板

color_palette()能传入任何Matplotlib所支持的颜色

set_palette()设置所有图的颜色

#显示默认的颜色 deep, muted, pastel, bright, dark, colorblind......

current_palette =sns.color_palette()

sns.palplot(current_palette)

1313448-20190910193714818-1498919329.png

圆形画板

当需要更多分类要区分时,最简单的方法就是在一个圆形的颜色空间中画出均匀间隔的颜色(这样的色调会保持亮度和饱和度不变)。这是大多数的需要使用的比默认颜色循环中的设置的颜色更多的默认方案。

最常用的方法是使用hls的颜色空间,这是RGB值的一个简单转换。

#比如我想画个12个不同的颜色

sns.palplot(sns.color_palette("hls", 12))

1313448-20190910194559824-1401641843.png

data = np.random.normal(size = (20, 8)) + np.arange(8) / 2sns.boxplot(data= data, palette = sns.color_palette("hls", 8))

1313448-20190910195014641-1443570671.png

hls_palette()函数来控制颜色的亮度和饱和度

l------亮度 lightness

s------饱和 saturation

sns.palplot(sns.hls_palette(8, l = 0.3, s = 0.8))

1313448-20190910195345711-1593284491.png

sns.palplot(sns.color_palette("Paired", 8)) #Paired意思是比如8就是4对颜色,每一对颜色都是一浅一深。

1313448-20190910195755814-965503415.png

使用xkcd颜色来命名颜色

xkcd包含了一套众包努力的针对随机RGB色的命名。产生了954个可以随机通过xkcd_rgb字典中调用的命名颜色。

plt.plot([0, 1], [0, 1], sns.xkcd_rgb["pale red"], lw=3)

plt.plot([0,1], [0, 2], sns.xkcd_rgb["medium green"], lw=3)

plt.plot([0,1], [0, 3], sns.xkcd_rgb["denim blue"], lw=3)

1313448-20190910202725007-664883208.png

#其他xkcd的一些颜色

colors = ["windows blue", "amber", "greyish", "faded green", "dusty purple"]

sns.palplot(sns.xkcd_palette(colors))

1313448-20190910203112974-2078230252.png

连续色板

色彩随数据变换,比如数据越来越重要则颜色越来越深

sns.palplot(sns.color_palette("Blues"))

1313448-20190910203333102-688881352.png

如果想要翻转渐变,可以在面板名称中添加一个_r后缀

sns.palplot(sns.color_palette("BuGn_r"))

1313448-20190910203553381-1512853463.png

cubehelix_palette()调色板

色调线性变换

sns.palplot(sns.color_palette("cubehelix", 8))

1313448-20190910204957587-1158575275.png

sns.palplot(sns.cubehelix_palette(8, start = 0.5, rot = -0.75))

1313448-20190910205015484-428238410.png

sns.palplot(sns.cubehelix_palette(8, start = 0.75, rot = -0.150))

1313448-20190910205028958-245094560.png

light_palette()和dark_palette()调用定制连续调色板

sns.palplot(sns.light_palette("green"))

1313448-20190910210043552-113729908.png

sns.palplot(sns.dark_palette("purple"))

1313448-20190910210100761-1391171618.png

sns.palplot(sns.light_palette("navy", reverse=True))

1313448-20190910210118511-1920102067.png

四、单变量分析绘图

importseaborn as snsimportnumpy as npimportmatplotlib.pyplot as pltimportmatplotlib as mplfrom scipy importstats, integrate%matplotlib inline

x= np.random.normal(size = 100) #正态分布

sns.distplot(x, kde=False) #绘制直方图

1313448-20190911161400087-550051071.png

sns.distplot(x, bins = 20, kde = False) #通过bins可以改变条的数目

1313448-20190911161442275-591992120.png

x = np.random.gamma(6, size = 200)

sns.distplot(x, kde= False, fit = stats.gamma) #通过fit进行拟合

1313448-20190911161521039-2047566032.png

根据均值和协方差生成数据

#生成两个变量是DateFrame格式的

mean, cov = [0, 1], [(1, 0.5), (0.5, 1)]

data= np.random.multivariate_normal(mean, cov, 200)

df= pd.DataFrame(data, columns=["x", "y"])

df

1313448-20190911223723349-822483686.png

1313448-20190911223816279-1471759980.png

观测两个变量之间的分布关系最好用散点图

sns.jointplot(x="x", y ="y", data=df);

1313448-20190911224151374-711572395.png

x, y = np.random.multivariate_normal(mean, cov, 1000).T

with sns.axes_style("white"):

sns.jointplot(x=x, y=y, kind="hex", color='k')

1313448-20190911224527723-361503917.png

1313448-20190911224539766-1467583897.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值