画一条垂直线,其中 Y 等于 Matplotlib 中的某个值



我有一个小片段,绘制了 10 个累积子图。我想引入一条垂直线来指示分布与 80 交叉的 x 值。

y 轴是

累计销售产品的百分比,x 轴是折扣百分比,从 0 到 100 不等

i=1
fig, axes = plt.subplots(ncols=2, nrows=5,figsize=(20,20))
for priceband in new_price_band_gc['price_band'].unique():
    plt.subplot(5,2,i)
    plt.title(priceband)
    plt.xlabel("discount%")
    plt.ylabel("Actual GC Value")
    x=new_price_band_gc.loc[new_price_band_gc['price_band']==priceband,'discount']
    y=np.cumsum(new_price_band_gc.loc[new_price_band_gc['price_band']==priceband,'act_gc'])
    plt.axvline(x=0.22058956)  ##Change this to a vertical line at 80% for each curve
    plt.plot(x,y)
    if i<10:
        i+=1
    else:
        i
plt.tight_layout()
plt.show()

我找到了问题的答案,这一行得到了想要的结果:

plt.axvline(x=np.interp(80, y, x1),ymin=0.04,ymax=0.78,color='grey') #Changed x to x1 from the question for clarity, also added ymin, ymax to bound the line and changed the color to Grey

最新更新