Python Pandas / Matplot - 注释行上方和下方的值

时间:2018-01-30 10:14:34

标签: python pandas matplotlib

我有一个值的数据框,我用它来绘制带置信区间的散点图/折线图:

数据框(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() 

图表如下所示:

enter image description here

我想要做的是使用'Org'的值注释掉落在99%置信区间之上和之下的值。有没有一种简单的方法可以计算出Python中高于和低于两行的那些值?

谢谢

1 个答案:

答案 0 :(得分:1)

在DataFrame中,您可以在一行中获得数据点的y值和行的y值。因此,您可以使用np.where来实现此目的。

    $build = $this->createQueryBuilder('building');
    $build
        ->addSelect('users', 'furniture')
        ->join('building.users', 'users')
        ->leftJoin('building.furniture', 'furniture')
        ->where('building.id = :id')
        ->setParameter('id', $id)
        ->orderBy('building.getStartDate', 'ASC')
        ->addOrderBy('building.getCreatedDate', 'DESC');

    $paginator = new Paginator($build->getQuery(), $fetchJoinCollection = true);
    $result = $paginator->getQuery()
        ->setFirstResult($offset)
        ->setMaxResults($limit)
        ->getResult();
如果条件为C = np.where(condition, A, B) ,则设置

A,如果条件为True,则设置为B。如果您想查看FalseUpper99行,可以按照以下步骤进行检查:

Lower99

如果数据点位于给定边界之外,则会生成包含sqlDF2['Outside'] = np.where((sqlDf2['Success'] > sqlDf2['Upper99']*100) | (sqlDf2['Success']<sqlDf['Lower99']*100), True, False) 的新列;如果数据点位于边界内,则会生成True