网络编程
位置:首页>> 网络编程>> Python编程>> Pandas+Matplotlib 箱式图异常值分析示例

Pandas+Matplotlib 箱式图异常值分析示例

作者:灰兔子-刘  发布时间:2022-09-19 08:49:39 

标签:Pandas,Matplotlib,箱式图,异常值

我就废话不多说了,直接上代码吧!


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

import pandas as pd
import matplotlib.pyplot as plt

catering_sale = '../data/catering_sale.xls'
data = pd.read_excel(catering_sale, index_col=u'日期') #指定日期列为索引,data类型为DataFrame

plt.rcParams['font.sans-serif'] = ['SimHei']  #指定字体为黑体
plt.rcParams['axes.unicode_minus'] = False  #显示负号

plt.figure()
p = data.boxplot(return_type='dict')  #画箱式图

x = p['fliers'][0].get_xdata()  #fliers为异常值标签,get_xdata()与get_ydata()用来获取横纵坐标数组
y = p['fliers'][0].get_ydata()
y.sort()

#使用annotate添加注释,xy表示标注点坐标, xytext表示注释坐标
for i in range(len(x)):
 if i > 0:
   plt.annotate(y[i], xy=(x[i], y[i]), xytext=(x[i]+0.05 - 0.8/(y[i]-y[i-1]), y[i]))
 else:
   plt.annotate(y[i], xy=(x[i], y[i]), xytext=(x[i]+0.08, y[i]))

plt.show()


来源:https://blog.csdn.net/Ericsson_Liu/article/details/81145874

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com