Plot a horizontal line using matplotlib

I have used spline interpolation to smooth a time series and would also like to add a horizontal line to the plot. But there seems to be an issue that is out of my grips. Any assistance would be really helpful. Here is what I have:

    annual = np.arange(1,21,1)
    l = np.array(value_list) # a list with 20 values
    spl = UnivariateSpline(annual,l)
    xs = np.linspace(1,21,200)
    plt.plot(xs,spl(xs),'b')

    plt.plot([0,len(xs)],[40,40],'r--',lw=2)
    pylab.ylim([0,200])
    plt.show()

problem seems to be with my use of [0,len(xs)] for horizontal line plotting.

You are correct, I think the [0,len(xs)] is throwing you off. You'll want to reuse the original x-axis variable xs and plot that with another numpy array of the same length that has your variable in it.

    annual = np.arange(1,21,1)
    l = np.array(value_list) # a list with 20 values
    spl = UnivariateSpline(annual,l)
    xs = np.linspace(1,21,200)
    plt.plot(xs,spl(xs),'b')

    #####horizontal line
    horiz_line_data = np.array([40 for i in xrange(len(xs))])
    plt.plot(xs, horiz_line_data, 'r--') 
    ###########plt.plot([0,len(xs)],[40,40],'r--',lw=2)
    pylab.ylim([0,200])
    plt.show()

Hopefully that fixes the problem!

From: stackoverflow.com/q/33382619