I currently have a plot like this (consider that data
is the dataframe I pasted at the very bottom):
JavaScript
x
10
10
1
import seaborn as sns
2
3
sns.relplot(
4
data = data,
5
x = "Threshold",
6
y = "Value",
7
kind = "line",
8
hue="Metric"
9
).set(xlabel="Threshold")
10
Which produces:
Now, I want to know how can I annotate a line in this plot, such that it is located between the curves, at the x-Axis value where the distance between curves are maximized. I would also need to annotate text to show the distance value.
It should be something like this:
Here is the pandas dataframe:
JavaScript
1
202
202
1
Threshold,Metric,Value
2
0.0,Recall,1.0
3
0.010101010101010102,Recall,0.9802536231884058
4
0.020202020202020204,Recall,0.9706521739130435
5
0.030303030303030304,Recall,0.9621376811594203
6
0.04040404040404041,Recall,0.9541666666666667
7
0.05050505050505051,Recall,0.9456521739130435
8
0.06060606060606061,Recall,0.9322463768115942
9
0.07070707070707072,Recall,0.9173913043478261
10
0.08080808080808081,Recall,0.908695652173913
11
0.09090909090909091,Recall,0.8976449275362319
12
0.10101010101010102,Recall,0.8813405797101449
13
0.11111111111111112,Recall,0.8644927536231884
14
0.12121212121212122,Recall,0.8498188405797101
15
0.13131313131313133,Recall,0.8358695652173913
16
0.14141414141414144,Recall,0.818659420289855
17
0.15151515151515152,Recall,0.7967391304347826
18
0.16161616161616163,Recall,0.7748188405797102
19
0.17171717171717174,Recall,0.7521739130434782
20
0.18181818181818182,Recall,0.7269927536231884
21
0.19191919191919193,Recall,0.6952898550724638
22
0.20202020202020204,Recall,0.6704710144927536
23
0.21212121212121213,Recall,0.648731884057971
24
0.22222222222222224,Recall,0.6097826086956522
25
0.23232323232323235,Recall,0.5847826086956521
26
0.24242424242424243,Recall,0.5521739130434783
27
0.25252525252525254,Recall,0.5023550724637681
28
0.26262626262626265,Recall,0.4766304347826087
29
0.27272727272727276,Recall,0.42047101449275365
30
0.2828282828282829,Recall,0.3958333333333333
31
0.29292929292929293,Recall,0.3539855072463768
32
0.30303030303030304,Recall,0.3327898550724638
33
0.31313131313131315,Recall,0.3036231884057971
34
0.32323232323232326,Recall,0.2798913043478261
35
0.33333333333333337,Recall,0.2371376811594203
36
0.3434343434343435,Recall,0.22119565217391304
37
0.3535353535353536,Recall,0.17300724637681159
38
0.36363636363636365,Recall,0.15996376811594204
39
0.37373737373737376,Recall,0.13568840579710145
40
0.38383838383838387,Recall,0.11938405797101449
41
0.393939393939394,Recall,0.10652173913043478
42
0.4040404040404041,Recall,0.09891304347826087
43
0.4141414141414142,Recall,0.08894927536231884
44
0.42424242424242425,Recall,0.07681159420289856
45
0.43434343434343436,Recall,0.06557971014492754
46
0.4444444444444445,Recall,0.05253623188405797
47
0.4545454545454546,Recall,0.04655797101449275
48
0.4646464646464647,Recall,0.024456521739130436
49
0.4747474747474748,Recall,0.019384057971014494
50
0.48484848484848486,Recall,0.009782608695652175
51
0.494949494949495,Recall,0.0034420289855072463
52
0.5050505050505051,Recall,0.002173913043478261
53
0.5151515151515152,Recall,0.0016304347826086956
54
0.5252525252525253,Recall,0.0007246376811594203
55
0.5353535353535354,Recall,0.00018115942028985507
56
0.5454545454545455,Recall,0.0
57
0.5555555555555556,Recall,0.0
58
0.5656565656565657,Recall,0.0
59
0.5757575757575758,Recall,0.0
60
0.5858585858585859,Recall,0.0
61
0.595959595959596,Recall,0.0
62
0.6060606060606061,Recall,0.0
63
0.6161616161616162,Recall,0.0
64
0.6262626262626263,Recall,0.0
65
0.6363636363636365,Recall,0.0
66
0.6464646464646465,Recall,0.0
67
0.6565656565656566,Recall,0.0
68
0.6666666666666667,Recall,0.0
69
0.6767676767676768,Recall,0.0
70
0.686868686868687,Recall,0.0
71
0.696969696969697,Recall,0.0
72
0.7070707070707072,Recall,0.0
73
0.7171717171717172,Recall,0.0
74
0.7272727272727273,Recall,0.0
75
0.7373737373737375,Recall,0.0
76
0.7474747474747475,Recall,0.0
77
0.7575757575757577,Recall,0.0
78
0.7676767676767677,Recall,0.0
79
0.7777777777777778,Recall,0.0
80
0.787878787878788,Recall,0.0
81
0.797979797979798,Recall,0.0
82
0.8080808080808082,Recall,0.0
83
0.8181818181818182,Recall,0.0
84
0.8282828282828284,Recall,0.0
85
0.8383838383838385,Recall,0.0
86
0.8484848484848485,Recall,0.0
87
0.8585858585858587,Recall,0.0
88
0.8686868686868687,Recall,0.0
89
0.8787878787878789,Recall,0.0
90
0.888888888888889,Recall,0.0
91
0.8989898989898991,Recall,0.0
92
0.9090909090909092,Recall,0.0
93
0.9191919191919192,Recall,0.0
94
0.9292929292929294,Recall,0.0
95
0.9393939393939394,Recall,0.0
96
0.9494949494949496,Recall,0.0
97
0.9595959595959597,Recall,0.0
98
0.9696969696969697,Recall,0.0
99
0.9797979797979799,Recall,0.0
100
0.98989898989899,Recall,0.0
101
1.0,Recall,0.0
102
0.0,Fall-out,1.0
103
0.010101010101010102,Fall-out,0.6990465720990212
104
0.020202020202020204,Fall-out,0.58461408367334
105
0.030303030303030304,Fall-out,0.516647992727734
106
0.04040404040404041,Fall-out,0.4643680104855929
107
0.05050505050505051,Fall-out,0.4172674037587468
108
0.06060606060606061,Fall-out,0.3796376551170116
109
0.07070707070707072,Fall-out,0.3507811343889394
110
0.08080808080808081,Fall-out,0.33186055852694335
111
0.09090909090909091,Fall-out,0.3152231359533222
112
0.10101010101010102,Fall-out,0.29964272879098575
113
0.11111111111111112,Fall-out,0.2855844238208993
114
0.12121212121212122,Fall-out,0.27161068008371564
115
0.13131313131313133,Fall-out,0.25719298987379235
116
0.14141414141414144,Fall-out,0.24338836860241422
117
0.15151515151515152,Fall-out,0.2312538316808659
118
0.16161616161616163,Fall-out,0.22026087140350506
119
0.17171717171717174,Fall-out,0.2083377375642137
120
0.18181818181818182,Fall-out,0.19694311143056467
121
0.19191919191919193,Fall-out,0.18402638310466565
122
0.20202020202020204,Fall-out,0.17440754286197493
123
0.21212121212121213,Fall-out,0.16548633279073208
124
0.22222222222222224,Fall-out,0.15278100754709004
125
0.23232323232323235,Fall-out,0.14292962391391667
126
0.24242424242424243,Fall-out,0.1317252605542989
127
0.25252525252525254,Fall-out,0.11555292476164303
128
0.26262626262626265,Fall-out,0.10612434729298353
129
0.27272727272727276,Fall-out,0.08902183793839714
130
0.2828282828282829,Fall-out,0.08331395471745978
131
0.29292929292929293,Fall-out,0.07232099444009894
132
0.30303030303030304,Fall-out,0.06735302200706086
133
0.31313131313131315,Fall-out,0.061454876012092256
134
0.32323232323232326,Fall-out,0.05665602604485973
135
0.33333333333333337,Fall-out,0.048982094158932836
136
0.3434343434343435,Fall-out,0.045641925459273196
137
0.3535353535353536,Fall-out,0.03748176648415534
138
0.36363636363636365,Fall-out,0.0341415977844957
139
0.37373737373737376,Fall-out,0.029321607509037482
140
0.38383838383838387,Fall-out,0.026996173604211148
141
0.393939393939394,Fall-out,0.024353635075999407
142
0.4040404040404041,Fall-out,0.022514428260364035
143
0.4141414141414142,Fall-out,0.01940680295118703
144
0.42424242424242425,Fall-out,0.017165930279263473
145
0.43434343434343436,Fall-out,0.014459970826374648
146
0.4444444444444445,Fall-out,0.011035240893812233
147
0.4545454545454546,Fall-out,0.009386296852208105
148
0.4646464646464647,Fall-out,0.004756569350781135
149
0.4747474747474748,Fall-out,0.003868676405301989
150
0.48484848484848486,Fall-out,0.002135171130795087
151
0.494949494949495,Fall-out,0.0008033317125763693
152
0.5050505050505051,Fall-out,0.0004228061645138786
153
0.5151515151515152,Fall-out,0.00031710462338540896
154
0.5252525252525253,Fall-out,4.228061645138786e-05
155
0.5353535353535354,Fall-out,0.0
156
0.5454545454545455,Fall-out,0.0
157
0.5555555555555556,Fall-out,0.0
158
0.5656565656565657,Fall-out,0.0
159
0.5757575757575758,Fall-out,0.0
160
0.5858585858585859,Fall-out,0.0
161
0.595959595959596,Fall-out,0.0
162
0.6060606060606061,Fall-out,0.0
163
0.6161616161616162,Fall-out,0.0
164
0.6262626262626263,Fall-out,0.0
165
0.6363636363636365,Fall-out,0.0
166
0.6464646464646465,Fall-out,0.0
167
0.6565656565656566,Fall-out,0.0
168
0.6666666666666667,Fall-out,0.0
169
0.6767676767676768,Fall-out,0.0
170
0.686868686868687,Fall-out,0.0
171
0.696969696969697,Fall-out,0.0
172
0.7070707070707072,Fall-out,0.0
173
0.7171717171717172,Fall-out,0.0
174
0.7272727272727273,Fall-out,0.0
175
0.7373737373737375,Fall-out,0.0
176
0.7474747474747475,Fall-out,0.0
177
0.7575757575757577,Fall-out,0.0
178
0.7676767676767677,Fall-out,0.0
179
0.7777777777777778,Fall-out,0.0
180
0.787878787878788,Fall-out,0.0
181
0.797979797979798,Fall-out,0.0
182
0.8080808080808082,Fall-out,0.0
183
0.8181818181818182,Fall-out,0.0
184
0.8282828282828284,Fall-out,0.0
185
0.8383838383838385,Fall-out,0.0
186
0.8484848484848485,Fall-out,0.0
187
0.8585858585858587,Fall-out,0.0
188
0.8686868686868687,Fall-out,0.0
189
0.8787878787878789,Fall-out,0.0
190
0.888888888888889,Fall-out,0.0
191
0.8989898989898991,Fall-out,0.0
192
0.9090909090909092,Fall-out,0.0
193
0.9191919191919192,Fall-out,0.0
194
0.9292929292929294,Fall-out,0.0
195
0.9393939393939394,Fall-out,0.0
196
0.9494949494949496,Fall-out,0.0
197
0.9595959595959597,Fall-out,0.0
198
0.9696969696969697,Fall-out,0.0
199
0.9797979797979799,Fall-out,0.0
200
0.98989898989899,Fall-out,0.0
201
1.0,Fall-out,0.0
202
Advertisement
Answer
- Use
pivot
to transform the data from long to wide - Use
idxmax
to find thex
(Threshold) of the max difference betweeny1
andy2
(Fall-out and Recall) - Use
vlines
to plot the vertical line atx
fromy1
toy2
- Use
annotate
to plot the label at the midpoint ofy1
andy2
JavaScript
1
17
17
1
g = sns.relplot(data=data, x='Threshold', y='Value', hue='Metric', kind='line')
2
3
# pivot to wide form
4
p = data.pivot(index='Threshold', columns='Metric', values='Value')
5
6
# find x, y1, and y2 corresponding to max difference
7
diff = p['Fall-out'].sub(p['Recall']).abs()
8
x = diff.idxmax()
9
y1, y2 = p.loc[x]
10
11
# plot line and label
12
ax = g.axes.flat[0]
13
ax.vlines(x, y1, y2, ls='--')
14
ax.annotate(f'Dist = {diff.loc[x]:.2f}', ha='left', va='center',
15
xy=(x, 0.5*(y1+y2)), xycoords='data',
16
xytext=(5, 0), textcoords='offset pixels')
17