数据可视化(5)热力图及箱型图

1.热力图

python 复制代码
#基本热力图
#imshow(x)
#x,数据
x=[[1,2],[3,4],[5,6],[7,8],[9,10]]
plt.imshow(x)
plt.show()
python 复制代码
#使用热力图分析学生的成绩
df=pd.read_excel('学生成绩表.xlsx')
#:表示行号 截取数学到英语的列数
x=df.loc[:,"数学":'英语'].values
#设置x轴坐标轴刻度
plt.xticks(range(3),['数学','语文','英语'])
plt.yticks(range(10),df['姓名'])
#绘制热力图
plt.imshow(x)
plt.title('学生成绩热力图')
#显示颜色条
plt.colorbar()
plt.show()

2.箱型图

python 复制代码
#基本箱型图
#boxplot(x,whis=None,widths=None,patch_artist=None,showmeans=None,boxprops=None)
#x指定要绘制箱型图的数据
#whis指定上下限与上下四分位的距离,默认为1.5倍的四分位差
#widths指定箱型图的宽度,默认为0.5
#patch_artist是否填充箱体的颜色
x=[1,3,5,7,9]
plt.boxplot(x)
plt.show()
python 复制代码
#绘制多箱型图
x=[1,3,5,7,9]
x2=[10,78,45,34,20]
x3=[20,28,46,54,60]
plt.boxplot([x,x2,x3])
plt.show()
python 复制代码
#分析25%中位数等计算过程
x=[20,28,46,54,60]
#排序
x.sort()
s=pd.Series(x)
#排序之后最中间的位置的数就是中位数
print(s.quantile(q=0.5))
#下四分位计算公式1+(n-1)*0.25
print(s.quantile(q=0.25))
#上四分位数计算公式1+(n-1)*0.75
print(s.quantile(q=0.75))
python 复制代码
#通过箱型图判断总销售的异常值
df=pd.read_excel('tips.xlsx')
plt.boxplot(
    df['总消费'],
    patch_artist=True,#填充箱子颜色
    showmeans=True,#显示均值
    #异常值的填充色,边框颜色,大小
    flierprops={'markerfacecolor':'red','markeredgecolor':'r','markersize':'5'},
    #设置均值的符号,颜色,大小
    meanprops={'marker':'h','mfc':'black','markersize':8},
    medianprops={'linestyle':'--','color':'r'}
)
plt.show()
#查找异常值
#计算四分位数
Q1=df['总消费'].quantile(q=0.25)
Q2=df['总消费'].quantile(q=0.75)
#上限,下限
low=Q1-1.5*(Q2-Q1)
up=Q2+1.5*(Q2-Q1)
#查找异常
val=df['总消费'][(df['总消费']>up)|(df['总消费']<low)]
val
相关推荐
线条12 天前
Matplotlib 安装部署与版本兼容问题解决方案(pyCharm)
matplotlib
HuashuiMu花水木3 天前
Matplotlib笔记4----------图像处理
图像处理·笔记·matplotlib
程序员阿超的博客4 天前
Python 数据分析与机器学习入门 (五):Matplotlib 数据可视化基础
python·信息可视化·数据分析·matplotlib·数据可视化·python教程·pyplot
音程16 天前
Matplotlib绘制矩阵图,plt.matshow/imshow 与 ax.pcolor(pcolormesh)方法的使用
matplotlib
搏博16 天前
基于Python、tkinter、sqlite3 和matplotlib的校园书店管理系统
python·信息可视化·sqlite·matplotlib
勤奋的知更鸟19 天前
Matplotlib 绘图库使用技巧介绍
开发语言·python·matplotlib
@小红花19 天前
Matplotlib快速入门
matplotlib
仟濹23 天前
「Matplotlib 入门指南」 Python 数据可视化分析【数据分析全栈攻略:爬虫+处理+可视化+报告】
python·信息可视化·数据分析·matplotlib
Jay_271 个月前
cloudstudio腾讯云:matplotlib 设置中文字体
腾讯云·matplotlib
Gyoku Mint1 个月前
机器学习×第二卷:概念下篇——她不再只是模仿,而是开始决定怎么靠近你
人工智能·python·算法·机器学习·pandas·ai编程·matplotlib