gpt4 book ai didi

python - 具有极限和异常点的极坐标图

转载 作者:太空狗 更新时间:2023-10-30 02:15:55 25 4
gpt4 key购买 nike

考虑以下数据框,

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()

关于python - 具有极限和异常点的极坐标图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47476809/

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