分类数据可视化 - 分类散点图
stripplot() / swarmplot()
加载模块,设置风格、尺度
import numpy as np import pandas as pd import matplotlib.pyplot as plt import seaborn as sns #设置风格、尺度 sns.set_style('whitegrid') sns.set_context('paper') #不发出警告 import warnings warnings.filterwarnings('ignore')
1.stripplot()
#按照不同类别对样本数据进行分布散点图绘制
示例1:hue
tips = sns.load_dataset('tips') print(tips.head()) sns.stripplot(x = 'day', #x ---> 设置分组统计字段 y = 'total_bill',#y ---> 数据分布统计字段 #这里xy数据对调,会使得散点图横向分布 data = tips, #data ---> 对应数据 jitter = True, #jitter ---> 当数据重合较多时,用该参数做一些调整,也可以设置间距如,jitter = 0.1 size = 5, edgecolor = 'w', linewidth = 1, marker = 'o' ) #通过hue参数再分类 sns.stripplot(x = 'sex', y = 'total_bill', hue = 'day', data=tips, jitter = True)
通过stripplot() 按照x轴里的类别进行分类
并通过hue = ‘day’可以再对散点图中的数值进行分类
示例2:设置调色盘palette
#设置调色盘 sns.stripplot(x = 'sex' , y = 'total_bill', hue = 'day', data = tips, jitter = True, palette = 'Set2',#设置调色盘 dodge = True, )
示例3:用order参数进行筛选分类类别
#筛选分类类别 #查看day字段的唯一值 print(tips['day'].value_counts()) #order ---> 筛选类别 sns.stripplot(x = 'day', y = 'total_bill', data = tips, jitter = True, order = ['Sat','Sun'])
2、swarmplot()
分簇散点图
#用法和stripplot类似 sns.swarmplot(y = 'total_bill', x = 'day', data= tips, size = 5, edgecolor = 'w', linewidth = 1, marker = 'o', palette = 'Reds')