python axhline 标签未显示在图中

标签 python matplotlib

我有以下小功能:

def plotresults(freqs,power,prob,title,sigP):
    pl.suptitle(title)
    ax1 = pl.subplot(2,1,1)
    ax1.axhline(y=sigP, color='r', ls='--',label='p=0.05')
    pl.plot(freqs,power)
    ax1.set_ylabel('Spectral Power')

    ax2 = pl.subplot(2,1,2)
    ax2.axhline(y=0.05, color='r', ls='--', label='p=0.05')
    pl.semilogy(freqs,prob)
    ax2.set_xlabel(r'Frequency (years$^{-1}$)')
    ax2.set_ylabel('p-value')
    pl.savefig('lsfast/figs/'+title+'.png')
    pl.close()

它绘制得很好,并将线条绘制在应有的位置,但线条标签没有出现在任何地方。我究竟做错了什么?附上输出示例:

Example plot created using the above function

最佳答案

绘图的标签 kwarg sets the label that's used by legend 。要显示它,您可以添加 legend到你的情节。或者,您可能想使用 annotate反而。

关于python axhline 标签未显示在图中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31950456/

相关文章:

python - request.get() 卡住了

python - 我可以通过 socket.fromshare 重用套接字文件句柄吗?

python - 如何增加/减少 x 和 y 刻度标签的字体大小

python - 如何调整行或列中的具体数据?

python - 如何在Python Matplotlib折线图中设置所需的时间间隔和X轴的所需格式?

python - numpy.subtract 但仅直到差异达到阈值 - 用阈值替换小于该值的数字

Python 文件上传小部件不工作,它没有在 jupyter notebook 中显示上传按钮

python - 记忆化python函数

python - 如何让 matplotlib 的 imshow 生成图像而不被绘制

python - 如何从 Pandas 绘图函数返回 matplotlib.figure.Figure 对象