Python - 注释True的值(并使用其他值)

时间:2018-01-30 12:17:23

标签: python matplotlib seaborn

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

数据帧(sqlDF2)是这样的:

Statu   Total   Outside   Success   Pred   Upper95    Lower95      Upper99    Lower99
Org                             
A        391    True       38    0.35064  0.398903   0.302377    0.423034    0.278245
B        360    False       30    0.343464 0.393519   0.293408    0.418546    0.268381
C        271    False       29    0.319606 0.37626    0.262951    0.404587    0.234624
D        247    True       22    0.312089 0.371053   0.253125    0.400535    0.223643
...

我的代码是:

import seaborn as sns
import matplotlib.pyplot as plt

# Initialize Figure and Axes object
fig, ax = plt.subplots(figsize=(15,8))

# Load in the data
data = sqlDf2

y = data['Success'].values
x = data['Total'].values
up95 = (data['Upper95'].values)*100
low95 = (data['Lower95'].values)*100
up99 = (data['Upper99'].values)*100
low99 = (data['Lower99'].values)*100
middleLine = (data['Pred'].values)*100
holdOrg = sqlDf2.index.tolist()



# Create plot
sns.regplot(x="Total", y="Success", data=data,fit_reg=False, ax=ax)
plt.plot(x,up95, linewidth=2)
plt.plot(x,low95, linewidth=2)
plt.plot(x,up99, linewidth=2)
plt.plot(x,low99, linewidth=2)
plt.ylim(0, 100)
plt.margins(x=0)

# Show plot
plt.show()

仅当列'Outside'True时,我才会尝试在散点图中注释值。目前,每个要点都有注释:

enter image description here

我还希望注释的值不是xy,而是来自'holdOrg'的值。

这很容易吗?

1 个答案:

答案 0 :(得分:1)

如果您有三个列表x,y,z,则可以通过循环显示这三个列表,将xy给出的位置标记为z中的标记。< / p>

x = [1,2,3]
y = [4,5,6]
z = ["A","B","C"]
#annotation
for i,j, label in zip(x,y,z):
    ax.annotate(label,xy=(i,j))