Python基础——seaborn用法及实例

直方图

seaborn的displot()集合了matplotlib的hist()与核函数估计kdeplot的功能,增加了rugplot分布观测条显示与利用scipy库fit拟合参数分布的新颖用途。
具体用法如下:
seaborn.distplot(a, bins=None, hist=True, kde=True, rug=False, fit=None, hist_kws=None, kde_kws=None, rug_kws=None, fit_kws=None, color=None, vertical=False, norm_hist=False, axlabel=None, label=None, ax=None)
参数详解:
a : Series, 1d-array, or list.数据
Observed data. If this is a Series object with a name attribute, the name will be used to label the data axis.
bins : argument for matplotlib hist(), or None, optional #设置矩形图数量
Specification of hist bins, or None to use Freedman-Diaconis rule.
hist : bool, optional #控制是否显示条形图
kde : bool, optional #控制是否显示核密度估计图
rug : bool, optional #控制是否显示观测的小细条(边际毛毯)
fit : random variable object, optional #控制拟合的参数分布图形
{hist, kde, rug, fit}_kws : dictionaries, optional
Keyword arguments for underlying plotting functions.
vertical : bool, optional #显示正交控制If True, oberved values are on y-axis.

实例

import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

sns.set(style="white", palette="muted", color_codes=True)#设置主题、尺寸大小以及调色板。
rs = np.random.RandomState(10)

# Set up the matplotlib figure
f, axes = plt.subplots(2, 2, figsize=(7, 7), sharex=True)
sns.despine(left=True)#

# Generate a random univariate dataset
d = rs.normal(size=100)

# Plot a simple histogram with binsize determined automatically
sns.distplot(d, kde=True,rug=True, color="b", ax=axes[0, 0])#ax=axes代表子图的位置[0,0]代表第一行第一个

# Plot a kernel density estimate and rug plot
sns.distplot(d, hist=False, rug=True, color="r", ax=axes[0, 1])#关闭直方图,开启rug细条,shade控制阴影

# Plot a filled kernel density estimate
sns.distplot(d, hist=False, color="g", kde_kws={"shade": True}, ax=axes[1, 0])

# Plot a historgram and kernel density estimate
sns.distplot(d, color="m", ax=axes[1, 1])

plt.setp(axes, yticks=[])
plt.tight_layout()

在这里插入图片描述

relplot用散点图关联变量

relplot默认kind=“scatter”
相关参数:x,y,data
hue颜色
style点的形状
size点的大小,可以添加sizes=(c1, c2)添加范围

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
sns.set(style="darkgrid")

tips = sns.load_dataset("tips")
sns.relplot(x="total_bill", y="tip", data=tips)

'''第三个变量对点进行着色来将另一个维度添加到绘图中'''
sns.relplot(x="total_bill", y="tip", hue="smoker", data=tips)#添加颜色

sns.relplot(x="total_bill", y="tip", hue="smoker", style="smoker",
            data=tips)

sns.relplot(x="total_bill", y="tip", hue="size", palette="ch:r=-.5,l=.75", data=tips)

sns.relplot(x="total_bill", y="tip", size="size", sizes=(15, 200), data=tips)

仅展示第一个图和最后一个图
在这里插入图片描述
在这里插入图片描述

强调线图的连续性

散点图是非常有效的,但是没有通用的最优可视化类型。相反,可视表示应该适应数据集的细节以及您试图用图表回答的问题。

对于某些数据集,您可能希望了解一个变量中的变化关于时间的函数,或者类似的连续变量。在这种情况下,一个很好的选择是绘制线图。 在seaborn中,这可以通过lineplot()函数直接实现,也可以通过设置relplot()的参数kind="line"来实现

df = pd.DataFrame(dict(time=np.arange(500),
                       value=np.random.randn(500).cumsum()))
g = sns.relplot(x="time", y="value", kind="line", data=df)
g.fig.autofmt_xdate()

在这里插入图片描述

df = pd.DataFrame(np.random.randn(500, 2).cumsum(axis=0), columns=["x", "y"])
sns.relplot(x="x", y="y", sort=True, kind="line", data=df)#默认对x进行排序后再画图
sns.relplot(x="x", y="y", sort=False, kind="line", data=df)

在这里插入图片描述
在这里插入图片描述
https://www.cntofu.com/book/172/docs/3.md

猜你喜欢

转载自blog.csdn.net/qq_42871249/article/details/105089052
今日推荐