用python的seaborn库画箱线图和小提琴图

先导入数据集

准备工作及导入数据集在seaborn库的介绍与使用中有讲解:

https://blog.csdn.net/weixin_43799652/article/details/102484031

#准备工作
import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns
import numpy as np
import pandas as pd

plt.rcParams['font.family']='SimHei'#'SimHei'为黑体
plt.rcParams['axes.unicode_minus'] = False #显示负号
sns.set_style('darkgrid',{'font.sans-serif':['SimHei','Arial']})

import warnings  # 去除部分警告信息
warnings.filterwarnings('ignore')
# 导入数据
tips = sns.load_dataset("tips")
tips.head()

在这里插入图片描述

1. 箱线图(boxplot)

  • sns.boxplot()
sns.boxplot(x = 'day',y = 'tip'   #x,y交换位置箱线图变为横向     
            ,data = tips   #以小费为数据
           )   

在这里插入图片描述

1.1 箱线图的其他参数

sns.boxplot(x = 'day',y = 'tip'   #x,y交换位置箱线图 横向     
            ,data = tips
            ,hue = 'sex'          # 按照性别分类
            ,palette = 'Purples'  # 设置颜色调色盘
           ) 

在这里插入图片描述

2. 小提琴图(箱线图和密度图的结合)

  • sns.violinplot()
L = [9,3,1,5,3]
sns.violinplot(y = L)  # x = L 可以将小提琴图横过来

在这里插入图片描述

sns.violinplot(x = 'day',y = 'tip'
               ,data = tips
              )

在这里插入图片描述

2.1 小提琴图的其他参数

sns.violinplot(x = 'day',y = 'tip'
               ,data = tips
               ,hue = 'sex'
               ,split = True  # 从中间切一半
              )

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值