Python - Seaborn 绘图

Seaborn

安装

conda install seaborn

加载数据集

import seaborn as sns
anscombe = sns.load_dataset('anscombe')
dataset_1 = anscombe[anscombe.dataset == 'I']

多变量图表

def recode_sex(sex):
	if sex == 'Femail':
		return 0
	else:
		return 1

tips['sex_color'] = tips['sex'].apply(recode_sex)

scatter_plot = plt.figure()

axes1 = scatter_plot.add_subplot(1, 1, 1)
axes1.scatter(x=tips['total_bill'], y=tips['tip'], s=tips['size']*10, c=tips['sex_color'], alpha=0.5)
axes1.set_title('Total Bill vs Tip color by Sex and sized by Size')
axes1.set_xlabel('Total Bill')
axes1.set_ylabel('Tip')

scatter_plot.show()

单变量图标

直方图

hist = sns.distplot(tips['total_bill'])
hist.set_title('Total Bill Histogram with Density Plot')

去掉 kde:

hist = sns.distplot(tips['total_bill'], kde=False)
hist.set_title('Total Bill Histogram')
hist.set_xlabel('Total Bill')
hist.set_ylabel('Frequency')
plt.show()

只要 kde:

hist = sns.distplot(tips['total_bill'], hist=False)
hist.set_title('Total Bill Density')
hist.set_xlabel('Total Bill')
hist.set_ylabel('Unit Probability')
plt.show()

添加地毯图:

hist = sns.distplot(tips['total_bill'], rug=True)
hist.set_title('Total Bill Histogram with Desity and Rug Plot')
hist.set_xlabel('Total Bill')
plt.show()

条形图

count = sns.countplot('day', data=tips)
count.set_title('Count of days')
count.set_xlabel('Day of the Week')
count.set_ylabel('Frequency')
plt.show()

散点图:

scatter = sns.regplot(x='ttotal_bill', y='tip', data=tips)
scatter.set_title('Scatterplot of Total Bill and Tip')
scatter.set_xlabel('Total Bill')
scatter.set_ylabel('Tip')
plt.show()
fig = sns.lmplot(x='total_bill', y='tip', data=tips)
plt.show()
hbin = sns.jointplot(x='total_bill', y='tip', data=tips, kind='hex')
hbin = set_axis_labels(xlabel='Total Bill', ylabel='Tip')
hbin.fig.suptitle('Hexbin Joint plot of Total Bill and Tip')
plt.show()
kde = sns.kdeplot(data=tips['total_bill'], data2=tips['tip'], shade=True)
kde.set_title('Kernel Density Plot of Total Bill and Tip')
kde.set_xlabel('Total Bill')
kde.set_ylabel('Tip')
plt.show()
kde_joint = sns.jointplot(x='total_bill', y='tip', data=tips, kind='kde')
plt.show()
bar = sns.barplot(x='time', y='total_bill', data=tips)
bar.set_title('Barplot of average total bill for time of day')
bar.set_xlabel('Time of day')
bar.set_ylabel('Average total bill')
plt.show()
box = sns.boxplot(x='time', y='total_bill', data=tips)
box.set_title('Box plot of total bill by time of day')
box.set_xlabel('Time of day')
box.set_ylabel('Total Bill')
plt.show()

小提琴图:

violin = sns.violinplot(x='time', y='total_bill', data=tips)
violin.set_title('Violin plot of total bill by time of day')
violin.set_xlabel('Time of day')
violin.set_ylabel('Total Bill')
plt.show()

九宫格:

tips = tips[['total_bill', 'tip', 'size']]
pair_grid = sns.PairGrid(tips)
pair_grid = pair_grid.map_upper(sns.regplot)
pair_grid = pair_grid.map_lower(sns.kdeplot)
pair_grid = pair_grid.map_diag(sns.distplot, rug=True)
plt.show()
violin = sns.violinplot(x='time', y='total_bill', hue='sex', data=tips, split=True)
plt.show()
scatter = sns.lmplot(x='total_bill', y='tip', data=tips, hue='sex', fit_reg=False)
plt.show()
pair_plot = sns.pairplot(tips, hue='sex')
plt.show()
scatter = sns.lmplot(x='total_bill', y='tip', data=tips, fit_reg=False, hue='sex', markers=['o', 'x'], scatter_kws={'s': tips['size']*10})
plt.show()

网格图:

anscombe = sns.lmplot(x='x', y='y', data=anscombe, fit_reg=False, col='dataset', col_wrap=2)
plt.show()

多面图:

facet = sns.FaceGrid(tips, col='time')
facet.map(sns.distplot, 'total_bill', rug=True)
plt.show()
facet = sns.FaceGrid(tips, col='day', hue='sex')
facet.map(plt.scatter, 'total_bill', 'tip')
facet = facet.add_legend()
plt.show()
facet = sns.lmplot(x='total_bill', y='tip', data=tips, fit_reg=False, hue='sex', col='day')
plt.show()
facet = sns.FaceGrid(tips, col='time', row='smoker', hue='sex')
facet.map(plt.scatter, 'total_bill', 'tip')
factor_plot = sns.factorplot(x='day', y='total_bill', hue='sex', data=tips, row='smoker', col='time', kind='violin')
plt.show()
  • 11
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值