常见图形:(箱线图,柱状图,散点图,折线图...)searborn+ matplotlib

本文介绍了如何利用Python的seaborn和matplotlib库,结合plt函数,来创建并定制各种图形,包括箱线图、柱状图、散点图和折线图。内容涵盖了图形大小调整和分组排列的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

# -*- coding: utf-8 -*-

import seaborn as sns
import numpy as np

#------------------------显示中文---------------------------------#
import matplotlib as mpl
mpl.rcParams['font.sans-serif'] = ['SimHei']  # 指定默认字体
mpl.rcParams['axes.unicode_minus'] = False  # 解决保存图像是负号'-'显示为方块的问题


#----------------------------------绘制箱线图-----------------------------------#
'''
箱线图的作用:
检验连续数据是否存在离群点,以及数据分布的范围(4分位数)
必须要一个参数 
y: 需要判断的数据序列
x: 分类的标签的数据
hue: 分组因子
palette: 调色板...Set1, Set2, Set3
linwidth: 线宽(2.5相当于加粗的线)
order: 排序的序列.例如 order = ['Dinner', 'Lunch']
orient = 'h' 对df中的每个数值型的数据都做一个箱线图
whis 参数设定是否显示箱线图的离群点, whis = np.inf 表示不显示
'''
sns.set_style("whitegrid")
tips = sns.load_dataset("tips")
tips
ax = sns.boxplot(x = 'time', y = 'total_bill',hue = 'smoker', data = tips, order = ['Dinner', 'Lunch'],
                 linewidth= 1.5,palette = 'Set3')
ax = sns.boxplot(data = tips, orient = 'h', palette = 'Set3')

#箱线图+ 有分布趋势的散点图
#图形组合也就是两条绘图语句一起运行就可以了,相当于图形覆盖了
ax = sns.boxplot(x='day', y = 'total_bill', data = tips)
ax = sns.swarmplot(x = 'day', y = 'total_bill', data = tips, color = '.25')

#plt.scatter(x = iris['sepal_length'], y = iris['sepal_width'])


#-----------------------------------barplot 带分布的散点图--------------------------#
'''
estimator: 统计参数默认为 np.mean,可自定义: np.sum, np.count_nonzero, np.median...
palette: 主题的颜色 palette = 'Blues_d'
'''
#统计参数默认实mean
ax = sns.barplot(x = "day", y = "total_bill", hue = 'sex', data = tips, estimator= np.sum
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值