Skip to main content
 首页 » 编程设计

python之向具有非数字 x 轴的多个子图添加一条水平线

2024年12月31日8zhwl

我有这个数据框,我正在使用 df.plot 和子图绘制。

            AMLR   SAag    MDB   SMDB   SWWA 
All_months  0.000  0.000  0.000  0.041  0.000 
Jan         0.799  0.560  0.664  0.612  0.026 
Feb         0.591  0.230  0.139  0.211  0.017 
Mar         0.980  0.411  0.034  0.082  0.546 
Apr         0.243  0.420  0.692  0.890  0.795 
May         0.302  0.880  0.706  0.850  0.334 
June        0.952  0.480  0.848  0.261  0.235 
July        0.369  0.480  0.055  0.970  0.658 
Aug         0.953  0.530  0.184  0.241  0.523 
Sep         0.761  0.450  0.650  0.271  0.723 
Oct         0.151  0.047  0.000  0.004  0.690 
Nov         0.672  0.500  0.001  0.043  0.834 
Dec         0.557  0.170  0.012  0.039  0.007 

我用的是什么

correlationdataplot = correlationdf.plot(subplots = True, style ='.', figsize = (10,10)) 

我想在每个子图上绘制一条值为 0.05 的线。唯一的问题是我不确定如何创建它,因为 x 轴是非数字的(即一月、二月等)。

我尝试按照评论中的建议使用 axhline

correlationdataplot = correlationmonthsonlydf.plot(subplots = True, style ='.', figsize = (10,10)) 
correlationdataplot.axhline(y=0.05, xmin=0, xmax=1, colour = 'r', linestyle = '--') 

我得到“numpy.ndarray”对象没有属性“axhline”

预先感谢您的帮助!

请您参考如下方法:

只需使用axhline

figure = correlationdf.plot(subplots = True, style ='.', figsize = (10,10)) 
for ax in figure: 
    ax.axhline(y=0.05, linestyle = '--') 
    ax.set_xticks(range(len(correlationdf))) 
    ax.set_xticklabels(correlationdf.index)