用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()

用python的seaborn库画箱线图和小提琴图_第1张图片

1. 箱线图(boxplot)

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

用python的seaborn库画箱线图和小提琴图_第2张图片

1.1 箱线图的其他参数

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

用python的seaborn库画箱线图和小提琴图_第3张图片

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

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

用python的seaborn库画箱线图和小提琴图_第4张图片

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

用python的seaborn库画箱线图和小提琴图_第5张图片

2.1 小提琴图的其他参数

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

用python的seaborn库画箱线图和小提琴图_第6张图片

你可能感兴趣的:(数据可视化)