微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

python – 极限和异常点的极坐标图

考虑以下数据框,

d = {'score': [0.25, 0.52, 0.26, 0.22, 0.31, 2.45, 3.68, 41.3, 87, 91], 
     'Thr1': 16.5,
     'Thr2': 45.5,  
     'Anomaly':[0, 0, 0, 0, 0, 0, 0, 1, 1, 1]}

df = pd.DataFrame(data = d)

我想要做的是绘制极坐标图,用虚线表示阈值或多个虚线表示多个阈值,不同颜色用于异常.到目前为止我得到的是,

r = df['score']
theta = df.index.values
fig = plt.figure()
ax = fig.add_subplot(111, projection = 'polar')
c = ax.scatter(theta, r)

enter image description here

我无法获得阈值并改变异常点的颜色.有任何想法吗?

解决方法:

您需要在阈值级别绘制一条虚线,以指示阈值的位置. (一条线在极坐标图上显示为圆圈).

然后,您需要根据散点图中的值进行分离,以确定它们是否在阈值之下,之间或之上,并相应地着色点.

enter image description here

import matplotlib
matplotlib.use('TkAgg')

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np


dataset = {'score': [0.25, 0.52, 0.26, 0.22, 0.31, 2.45, 3.68, 41.3, 87, 91], 
           'Thr1': 16.5,
           'Thr2': 45.5,  
           'Anomaly':[0, 0, 0, 0, 0, 0, 0, 1, 1, 1]}

df = pd.DataFrame(data=dataset)

scores = df['score']
theta, thr_1, thr_2 = df.index.values, dataset['Thr1'], dataset['Thr2']

fig = plt.figure()
ax = fig.add_subplot(111, projection='polar')

# assigns a color to each point based on their relative value to the thresholds
colors = ['b' if val < thr_1 else 'y' if val < thr_2 else 'r' for val in scores]
point_cloud = ax.scatter(theta, scores, color=colors, marker='o')

# Drawing the threshold dash lines (with alpha value 1/2)
theta_xs, thr_y1, thr_y2 = np.linspace(0, 2*np.pi, 20), [thr_1] * 20, [thr_2] * 20
thr_line_1 = ax.plot(theta_xs, thr_y1, color='blue', linestyle='--', alpha=0.5)
thr_line_2 = ax.plot(theta_xs, thr_y2, color='green', linestyle='--', alpha=0.5)

plt.show()

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐