博主是在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')
结果如下:
如果觉得不错,就点赞或者关注或者留言~~
谢谢~ ~