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

查看:65
本文介绍了使用 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天全站免登陆