使用matplotlib绘制水平线 [英] Plot a horizontal line using matplotlib

查看:164
本文介绍了使用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()

问题似乎与我使用[0,len(xs)]进行水平线图绘制有关.

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

推荐答案

您是正确的,我认为[0,len(xs)]会让您失望.您将要重用原始的x轴变量xs,并使用另一个包含变量的相同长度的numpy数组对其进行绘制.

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

希望可以解决该问题!

这篇关于使用matplotlib绘制水平线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆