结合箱线图绘制水平线参考

Car*_*l92 3 matplotlib boxplot pandas

我想添加一条水平线,即 中的“参考” boxplot。我尝试了以下代码,但它没有按预期工作。

df=pd.DataFrame({"Prior": initial_FOPR,
                "DSI": pca_FOPR,
                "DSI.ESMDA": ksvd_FOPR,
                "DSI.ESMDA.LOC": dct_FOPR,"ES-MDA": Basic_FOPR,})
df.boxplot(grid=False, rot=45, fontsize=15)
plt.hlines(reference,xmin=min(reference),xmax=max(reference),'r')
Run Code Online (Sandbox Code Playgroud)

期望的结果: 预期结果

Diz*_*ahi 6

由于您似乎想要绘制一条跨越轴的整个宽度的线,因此使用 会更简单plt.axhline(),这就是为了这种精确使用而设计的。

那么你的代码就很简单:

df.boxplot(grid=False, rot=45, fontsize=15)
plt.axhline(reference, c='r')
Run Code Online (Sandbox Code Playgroud)