Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python axhline label not showing up in plot

I have the following little function:

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()

It plots fine and draws the lines where they should be, but the line labels don't appear anywhere. What am I doing wrong? AN example of the output is attached:

Example plot created using the above function

like image 304
KBriggs Avatar asked Aug 11 '15 19:08

KBriggs


2 Answers

The label kwarg for plot sets the label that's used by legend. To display it you can add a legend to your plot. Alternately, you might want to use annotate instead.

like image 151
Molly Avatar answered Nov 07 '22 01:11

Molly


I don't think attaching a label to a line is meant to draw this label to the plot, it just associates this label with the line and can be used to create a legend.

like image 21
Siwel Avatar answered Nov 07 '22 01:11

Siwel