gpt4 book ai didi

python - 箱线图 : Outliers Labels Python

转载 作者:行者123 更新时间:2023-12-03 20:22:26 24 4
gpt4 key购买 nike

我正在使用 seaborn 包制作时间序列箱线图,但我无法在异常值上贴标签。
我的数据是一个 3 列的数据框:[Month , Id , Value]我们可以这样伪造:

### Sample Data ###
Month = numpy.repeat(numpy.arange(1,11),10)
Id = numpy.arange(1,101)
Value = numpy.random.randn(100)

### As a pandas DataFrame ###
Ts = pandas.DataFrame({'Value' : Value,'Month':Month, 'Id': Id})

### Time series boxplot ###
ax = seaborn.boxplot(x="Month",y="Value",data=Ts)
我每个月都有一个箱线图,我正在尝试放置 Id作为图中三个异常值的标签:
1

最佳答案

首先需要检测哪个Id在您的数据框中是异常值,您可以使用:

outliers_df = pd.DataFrame(columns = ['Value', 'Month', 'Id'])
for month in Ts['Month'].unique():
outliers = [y for stat in boxplot_stats(Ts[Ts['Month'] == month]['Value']) for y in stat['fliers']]
if outliers != []:
for outlier in outliers:
outliers_df = outliers_df.append(Ts[(Ts['Month'] == month) & (Ts['Value'] == outlier)])
它创建了一个类似于原始数据框的数据框,仅包含异常值。
然后就可以批注 Id在你的情节上:
for row in outliers_df.iterrows():
ax.annotate(row[1]['Id'], xy=(row[1]['Month'] - 1, row[1]['Value']), xytext=(2,2), textcoords='offset points', fontsize=14)
完整代码:
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from matplotlib.cbook import boxplot_stats
sns.set_style('darkgrid')

Month = np.repeat(np.arange(1,11),10)
Id = np.arange(1,101)
Value = np.random.randn(100)

Ts = pd.DataFrame({'Value' : Value,'Month':Month, 'Id': Id})

fig, ax = plt.subplots()
sns.boxplot(ax=ax, x="Month",y="Value",data=Ts)

outliers_df = pd.DataFrame(columns = ['Value', 'Month', 'Id'])
for month in Ts['Month'].unique():
outliers = [y for stat in boxplot_stats(Ts[Ts['Month'] == month]['Value']) for y in stat['fliers']]
if outliers != []:
for outlier in outliers:
outliers_df = outliers_df.append(Ts[(Ts['Month'] == month) & (Ts['Value'] == outlier)])

for row in outliers_df.iterrows():
ax.annotate(row[1]['Id'], xy=(row[1]['Month'] - 1, row[1]['Value']), xytext=(2,2), textcoords='offset points', fontsize=14)

plt.show()
输出:
enter image description here

关于python - 箱线图 : Outliers Labels Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40470175/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com