data:绘图数据,可以是pandas.DataFrame,numpy.ndarray或者字典等
x,y:指定的x轴, y轴数据,可以是向量或者字符串,当是字符串时,一定是data中的一个key
hue:可以是向量(pandas中的一列,或者是list),也可以是字符串(data中的一个key),seaborn将根据这一列设置不同颜色
weights: 数据加权的权重
stat: 柱形的统计方式
1)count:统计每个区间的值的个数
2)frequency:区间内取值个数除以区间宽度
3)probability或proportion:进行标准化使条形高度总和为1
4)percent:标准化使条形总高度为100
5)使条形总面积为1
bins: 字符型、整型、向量都可以,可以是引用规则的名称、箱子的数量或箱子的分段或者分箱规则名称,规则名称见下方示例
binwidth: 条形宽度
binrange: 条形边缘的最大值或最小值
discrete: 如果为True,则默认为binwidth=1,并绘制条形图,使其位于相应数据点的中心。这避免了在使用离散(整数)数据时可能出现的“间隙”。
cumulative: 布尔型,是否逐个累加每个条形高度进行绘图
multiple: 直接看下文效果吧
element: 直接看下文效果吧
fill: 条形内部是否填充颜色
shrink: 缩小条形的宽度
kde: 是否生成核密度曲线
color: 设置条形颜色
legend: 是否显示图例
ax: 绘图的坐标轴实例
fig, ax =plt.subplots(1,2,constrained_layout=True, figsize=(8, 3))
pic = sns.histplot(penguins, x="flipper_length_mm", ax=ax[0])
pic = sns.histplot(penguins, x="body_mass_g", ax=ax[1])
fig, ax =plt.subplots(1,2,constrained_layout=True, figsize=(8, 3))
pic = sns.histplot(penguins, x="flipper_length_mm", ax=ax[0])
pic.set_title('x')
pic = sns.histplot(penguins, y="flipper_length_mm", ax=ax[1])
pic.set_title('y')
fig, ax =plt.subplots(1,5,constrained_layout=True, figsize=(15, 3))
_ = sns.histplot(penguins, x="flipper_length_mm", stat="count", ax=ax[0]) # count, 也是默认值
_ = sns.histplot(penguins, x="flipper_length_mm", stat="frequency", ax=ax[1]) # frequency
_ = sns.histplot(penguins, x="flipper_length_mm", stat="probability", ax=ax[2])# probability
_ = sns.histplot(penguins, x="flipper_length_mm", stat="percent", ax=ax[3]) # percent
_ = sns.histplot(penguins, x="flipper_length_mm", stat="density", ax=ax[4]) # density
fig, ax =plt.subplots(1,3,constrained_layout=True, figsize=(12, 3))
pic = sns.histplot(penguins, x="flipper_length_mm", ax=ax[0], bins=5)
pic.set_title('bins=5')
pic = sns.histplot(penguins, x="flipper_length_mm", ax=ax[1], bins=10)
pic.set_title('bins=10')
pic = sns.histplot(penguins, x="flipper_length_mm", ax=ax[2], bins=[150, 175, 200, 225, 250])
pic.set_title('bins=[150, 175, 200, 225, 250]')
fig, ax =plt.subplots(2,4,constrained_layout=True, figsize=(15, 6))
pic = sns.histplot(penguins, x="flipper_length_mm", bins="auto", ax=ax[0][0]) # count, 也是默认值
pic.set_title('auto')
pic = sns.histplot(penguins, x="flipper_length_mm", bins="fd", ax=ax[0][1]) # frequency
pic.set_title('fd')
pic = sns.histplot(penguins, x="flipper_length_mm", bins="doane", ax=ax[0][2])# probability
pic.set_title('doane')
pic = sns.histplot(penguins, x="flipper_length_mm", bins="scott", ax=ax[0][3]) # percent
pic.set_title('scott')
pic = sns.histplot(penguins, x="flipper_length_mm", bins="stone", ax=ax[1][0]) # count, 也是默认值
pic.set_title('stone')
pic = sns.histplot(penguins, x="flipper_length_mm", bins="rice", ax=ax[1][1]) # frequency
pic.set_title('rice')
pic = sns.histplot(penguins, x="flipper_length_mm", bins="sturges", ax=ax[1][2])# probability
pic.set_title('sturges')
pic = sns.histplot(penguins, x="flipper_length_mm", bins="sqrt", ax=ax[1][3]) # percent
pic.set_title('sqrt')
D:\ProgramData\Anaconda3\envs\machine_learning\lib\site-packages\numpy\lib\histograms.py:669: RuntimeWarning: The number of bins estimated may be suboptimal.
bin_edges, _ = _get_bin_edges(a, bins, range, weights)
fig, ax =plt.subplots(1,2,constrained_layout=True, figsize=(8, 3))
_ = sns.histplot(penguins, x="flipper_length_mm", ax=ax[0], binwidth=1)
_ = sns.histplot(penguins, x="flipper_length_mm", ax=ax[1], binwidth=3)
fig, ax =plt.subplots(1,3,constrained_layout=True, figsize=(12, 3))
pic = sns.histplot(penguins, x="flipper_length_mm", hue="species", ax=ax[0], element="bars")
pic.set_title('element="bars"')
pic = sns.histplot(penguins, x="flipper_length_mm", hue="species", ax=ax[1], element="step")
pic.set_title('element="step')
pic = sns.histplot(penguins, x="flipper_length_mm", hue="species", ax=ax[2], element="poly")
pic.set_title('element="poly"')
fig, ax =plt.subplots(1,4,constrained_layout=True, figsize=(16, 3))
pic = sns.histplot(penguins, x="flipper_length_mm", hue="species", ax=ax[0], multiple="layer")
pic.set_title('multiple="layer"')
pic = sns.histplot(penguins, x="flipper_length_mm", hue="species", ax=ax[1], multiple="dodge")
pic.set_title('multiple="dodge')
pic = sns.histplot(penguins, x="flipper_length_mm", hue="species", ax=ax[2], multiple="stack")
pic.set_title('multiple="stack"')
pic = sns.histplot(penguins, x="flipper_length_mm", hue="species", ax=ax[3], multiple="fill")
pic.set_title('multiple="fill"')
fig, ax =plt.subplots(1,2,constrained_layout=True, figsize=(8, 3))
pic = sns.histplot(penguins, x="flipper_length_mm", ax=ax[0], kde=False) # 默认值,不生成核密度曲线
pic.set_title('kde=False')
pic = sns.histplot(penguins, x="flipper_length_mm", ax=ax[1], kde=True) # 值为True,显示核密度曲线
pic.set_title('kde=True')
fig, ax =plt.subplots(1,2,constrained_layout=True, figsize=(8, 3))
pic = sns.histplot(penguins, x="flipper_length_mm", ax=ax[0], color="#FFC0CB") # 可以使16进制颜色
pic.set_title('color="#FFC0CB"')
pic = sns.histplot(penguins, x="flipper_length_mm", ax=ax[1], color="orange") # 也可以是 英文颜色字符串
pic.set_title('color="orange"')
fig, ax =plt.subplots(1,2,constrained_layout=True, figsize=(8, 3))
pic = sns.histplot(penguins, x="flipper_length_mm", ax=ax[0], color="#FFC0CB") # 可以使16进制颜色
pic.set_title('color="#FFC0CB"')
pic = sns.histplot(penguins, x="flipper_length_mm", ax=ax[1], color="orange") # 也可以是 英文颜色字符串
pic.set_title('color="orange"')
fig, ax =plt.subplots(1,2,constrained_layout=True, figsize=(8, 3))
pic = sns.histplot(penguins, x="flipper_length_mm", hue='sex', ax=ax[0], fill=False)
pic.set_title('fill=False')
pic = sns.histplot(penguins, x="flipper_length_mm", hue='sex', ax=ax[1], fill=True)
pic.set_title('fill=True')
fig, ax =plt.subplots(1,2,constrained_layout=True, figsize=(8, 3))
pic = sns.histplot(penguins, x="flipper_length_mm", hue="sex", ax=ax[0], legend=False)
pic.set_title('legend=False')
pic = sns.histplot(penguins, x="flipper_length_mm", hue="sex", ax=ax[1], legend=True)
pic.set_title('legend=True')
fig, ax =plt.subplots(1,2,constrained_layout=True, figsize=(8, 3))
pic = sns.histplot(penguins, x="flipper_length_mm", ax=ax[0], shrink=0.5)
pic.set_title('shrink=0.5')
# pic = sns.histplot(penguins, x="flipper_length_mm", ax=ax[1], bins=10)
pic = sns.histplot(penguins, x="flipper_length_mm", ax=ax[1], shrink=0.8)
pic.set_title('shrink=0.8')
x,y:指定的x轴, y轴数据,可以是向量或者字符串,当是字符串时,一定是data中的一个key
hue:可以是向量(pandas中的一列,或者是list),也可以是字符串(data中的一个key),seaborn将根据这一列设置不同颜色
data:绘图数据,可以是pandas.DataFrame,numpy.ndarray或者字典等
order:包含所有分组属性的列表,用于指定条形顺序,注意如果某个分组不在order中,该分组将不会显示
hue_order:字符串组成的list,设置hue后设置各颜色顺序
estimator:可调用对象,分组统计的方式,或者说条形图长度所表示的意义,可以使以下值:
len:调用内置的len方法统计数据总长
np.mean:表示统计各分组平均值,这也是默认的方式
np.sum:表示统计各分组总和
np.ptp:极差
np.median:统计中位数
np.std:标准差
np.var:方差
ci:float或者"sd"或None,在估计值附近绘制置信区间的大小,如果是"sd",则跳过bootstrapping并绘制观察的标准差,如果为None,则不执行bootstrapping,并且不绘制错误条。
orient:当x,y都是离散型或者数值型数据时,通过orient可设置图像方向
color:为所有条形设置统一颜色
palette:颜色面板,比color参数功能更加强大,更加个性化地设置条形的颜色
errcolor:错误带的颜色
errwidth:错误带的宽度
capsize:错误带“帽子”宽度
ax:自定义坐标系
fig, ax =plt.subplots(1,2,constrained_layout=True, figsize=(8, 3))
pic = sns.barplot(x="total_bill", y="day", data=tips, ax=ax[0])
pic.set_title('x="total_bill", y="day"')
pic = sns.barplot(x="day", y="total_bill", data=tips, ax=ax[1])
pic.set_title('x="day", y="total_bill"')
fig, ax =plt.subplots(1,3,constrained_layout=True, figsize=(12, 3))
pic = sns.barplot(x="day", y="total_bill", data=tips, ax=ax[0], estimator=np.mean)
pic.set_title('estimator=np.mean')
pic = sns.barplot(x="day", y="total_bill", data=tips, ax=ax[1], estimator=np.std)
pic.set_title('np.std')
pic = sns.barplot(x="day", y="total_bill", data=tips, ax=ax[2], estimator=len) # 内置方法len统计总次数
pic.set_title('len')
fig, ax =plt.subplots(1,2,constrained_layout=True, figsize=(8, 3))
pic = sns.barplot(x="day", y="total_bill", data=tips, ax=ax[0])
pic.set_title('default order')
pic = sns.barplot(x="day", y="total_bill", data=tips, order=['Sun', 'Sat', 'Thur', 'Fri'], ax=ax[1])
pic.set_title('"Sun", "Sat", "Thur", "Fri"')
fig, ax =plt.subplots(1,2,constrained_layout=True, figsize=(8, 3))
pic = sns.barplot(x="day", y="total_bill", data=tips, ax=ax[0])
pic.set_title('no hue')
pic = sns.barplot(x="day", y="total_bill", data=tips, ax=ax[1], hue="sex")
pic.set_title('hue="sex"')
条带图是是一种比较少见的图表,综合了散点图和直方图/条形图的图形特征和优势,其表达的含义又与箱型图十分类似。stripplot()参数如下:
x,y:指定的x轴, y轴数据,可以是向量或者字符串,当是字符串时,一定是data中的一个key
hue:可以是向量(pandas中的一列,或者是list),也可以是字符串(data中的一个key),seaborn将根据这一列设置不同颜色
data:绘图数据,可以是pandas.DataFrame,numpy.ndarray或者字典等
order:包含所有分组属性的列表,用于指定条形顺序,注意如果某个分组不在order中,该分组将不会显示
hue_order:字符串组成的list,设置hue后设置各颜色顺序
jitter:抖动量,当数据很多时,增加抖动量,使散点不至于聚成一团
dodge:在hue基础上,将不同颜色的散点分开
orient:当x,y都是离散型或者数值型数据时,通过orient可设置图像方向
color:统一设置所有散点的颜色
palette:颜色面板
size:散点的大小
edgecolor:散点的边框颜色
linewidth:散点边框宽度
ax:自定义坐标系
fig, ax =plt.subplots(1,2,constrained_layout=True, figsize=(8, 3))
pic = sns.stripplot(x="day", y="total_bill", data=tips, ax=ax[0])
pic.set_title('x="day", y="total_bill"')
pic = sns.stripplot(x="total_bill", y="day", data=tips, ax=ax[1])
pic.set_title('x="total_bill", y="day"')
fig, ax =plt.subplots(1,3,constrained_layout=True, figsize=(12, 3))
pic = sns.stripplot(x="day", y="total_bill", data=tips, ax=ax[0], )
pic.set_title('no jitter')
pic = sns.stripplot(x="day", y="total_bill", data=tips, ax=ax[1], jitter=0.05)
pic.set_title('jitter=0.05')
pic = sns.stripplot(x="day", y="total_bill", data=tips, ax=ax[2], jitter=0.3)
pic.set_title('jitter=0.3')
fig, ax =plt.subplots(1,3,constrained_layout=True, figsize=(12, 3))
pic = sns.stripplot(x="day", y="total_bill", data=tips, ax=ax[0], )
pic.set_title('no jitter')
pic = sns.stripplot(x="day", y="total_bill", data=tips, ax=ax[1], size=2)
pic.set_title('size=2')
pic = sns.stripplot(x="day", y="total_bill", data=tips, ax=ax[2], size=10)
pic.set_title('size=10')
fig, ax =plt.subplots(1,4,constrained_layout=True, figsize=(16, 3))
pic = sns.stripplot(x="day", y="total_bill", data=tips, ax=ax[0], )
pic.set_title('no linewidth')
pic = sns.stripplot(x="day", y="total_bill", data=tips, ax=ax[1], linewidth=1)
pic.set_title('linewidth=1')
pic = sns.stripplot(x="day", y="total_bill", data=tips, ax=ax[2], linewidth=2)
pic.set_title('linewidth=2')
pic = sns.stripplot(x="day", y="total_bill", data=tips, ax=ax[3], linewidth=1, edgecolor="yellow")
pic.set_title('linewidth=1, edgecolor="red"')
fig, ax =plt.subplots(1,2,constrained_layout=True, figsize=(8, 3))
pic = sns.stripplot(x="day", y="total_bill", data=tips, ax=ax[0], hue="smoker")
pic.set_title('hue="smoker"')
pic = sns.stripplot(x="day", y="total_bill", data=tips, ax=ax[1], hue="smoker", dodge=True)
pic.set_title('hue="smoker", dodge=True')