Python3 - seaborn: countplot(), palette map, set_context, figure(), set_style(), despine()... 有料

博主是在Jupyter Notebooks上练习的,要想知道如何创建Jupyter Notebooks, 请点击这里

展示所使用dataset:

import seaborn as sns
import matplotlib.pyplot as plt
tips = sns.load_dataset('tips')
tips.head()

结果如下:
在这里插入图片描述
使用countplot():

# countplot(): Show the counts of observations in each categorical bin using bars.
# A count plot can be thought of as a histogram across a categorical, instead of quantitative, variable. 
sns.countplot(data=tips, x='sex')

结果如下:
在这里插入图片描述
使用set_style():

# set_style(): Set the aesthetic style of the plots.
#              This affects things like the color of the axes, whether a grid is enabled by default, and other aesthetic elements.
# 有五种主题可供选择:darkgrid, whitegrid, dark, white, and ticks
# The default theme is darkgrid.
sns.set_style('whitegrid')
# countplot(): Show the counts of observations in each categorical bin using bars.
# A count plot can be thought of as a histogram across a categorical, instead of quantitative, variable. 
sns.countplot(data=tips, x='sex')

sns.set_style('ticks')
sns.countplot(data=tips, x='sex')
# Remove the top and right spines from plot(s).
sns.despine()

结果如下:
在这里插入图片描述

sns.set_style('ticks')
sns.countplot(data=tips, x='sex')
# top, right, left, bottomboolean, optional
#      If True, remove that spine.
sns.despine(left=True,bottom=True)

sns.set_style('ticks')
sns.countplot(data=tips, x='sex')
sns.despine(right=True,top=True) # 因为最初始的上边和右边是没有边框刻度的

结果如下:
在这里插入图片描述
使用figure():

# figsize: width, height in inches.
plt.figure(figsize=(12,3))
sns.countplot(x='sex', data=tips)

plt.figure(figsize=(10,3))
sns.countplot(x='time', data=tips)

结果如下:
在这里插入图片描述
使用set_context():

plt.figure(figsize=(10,3))
sns.countplot(x='total_bill', data=tips)

# set_context(): set the plotting context parameters.
#                This affects things like the size of the labels, lines, and other elements of the plot, but not the overall style.
# contextdict, None, or one of {paper, notebook, talk, poster}
# font_scale: Separate scaling factor to independently scale the size of the font elements.
sns.set_context('poster', font_scale = 1)
sns.countplot(x='sex', data=tips)

结果如下:
在这里插入图片描述

sns.set_context('poster', font_scale = 2)
sns.countplot(x='sex', data=tips)

sns.set_context('notebook', font_scale = 2)
sns.countplot(x='sex', data=tips)

结果如下:
在这里插入图片描述
使用palette:
要想了解更多颜色,请点击这里

sns.lmplot(x='total_bill',y='tip',data=tips,hue='sex',palette='seismic')

结果如下:
在这里插入图片描述
如果觉得不错,就点赞或者关注或者留言~~
谢谢~ ~

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值