Plot a horizontal line using matplotlib

后端 未结 7 1336
旧时难觅i
旧时难觅i 2020-11-27 11:39

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. An

相关标签:
7条回答
  • 2020-11-27 12:22

    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!

    0 讨论(0)
提交回复
热议问题