我有一个值数据框,我用它来绘制具有置信区间的散点图/折线图:
数据框(sqlDF2
)是这样的:
Statu Total count Success Pred Upper95 Lower95 Upper99 Lower99
Org
A 391 391 38 0.35064 0.398903 0.302377 0.423034 0.278245
B 360 360 30 0.343464 0.393519 0.293408 0.418546 0.268381
C 271 271 29 0.319606 0.37626 0.262951 0.404587 0.234624
D 247 247 22 0.312089 0.371053 0.253125 0.400535 0.223643
...
我绘制图表的代码是:
y = sqlDf2['Success'].values
x = sqlDf2['Total'].values
up95 = (sqlDf2['Upper95'].values)*100
low95 = (sqlDf2['Lower95'].values)*100
up99 = (sqlDf2['Upper99'].values)*100
low99 = (sqlDf2['Lower99'].values)*100
middleLine = (sqlDf2['Pred'].values)*100
plt.figure(figsize=(15,8))
plt.ylim(0, 100)
plt.margins(x=0)
plt.scatter(x,y,marker='o',c='white',edgecolors = 'black', alpha=.5)
plt.plot(x,up95, 'red', linestyle=':', dashes=(1, 5), linewidth=1)
plt.plot(x,low95, 'red', linestyle=':', dashes=(1, 5), linewidth=1)
plt.plot(x,up99, 'red', linestyle=':', dashes=(1, 5), linewidth=1)
plt.plot(x,low99, 'red', linestyle=':', dashes=(1, 5), linewidth=1)
plt.plot(x,middleLine, 'red', linestyle='-', dashes=(1, 2), linewidth=1)
plt.show()
图表看起来像这样:
我想要做的是用“Org”的值注释高于和低于 99% 置信区间的值。有没有一种简单的方法可以计算出 Python 中两行以上和以下的值?
谢谢
最佳答案
在您的 DataFrame 中,您有数据点的 y 值和单行中的行的 y 值。因此,您可以使用 np.where为此目的。
C = np.where(condition, A, B)
如果条件为 True
,则设置 A
;如果条件为 False
,则设置 B
。如果您想检查 Upper99
和 Lower99
行,您可以按如下方式实现:
sqlDF2['Outside'] = np.where((sqlDf2['Success'] > sqlDf2['Upper99']*100) | (sqlDf2['Success']<sqlDf['Lower99']*100), True, False)
如果数据点位于给定边界之外,这将导致包含 True
的新列,如果位于边界内,则包含 False
。
关于Python Pandas/Matplotlib - 在线上方和下方的注释值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48518881/