使用matplotlib绘制水平线

时间:2015-10-28 03:53:33

标签: python matplotlib

我使用样条插值来平滑时间序列,并且还想在图中添加水平线。但似乎有一个问题不在我的掌握之中。任何帮助都会非常有帮助。这就是我所拥有的:

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)]进行水平线绘图有关。

8 个答案:

答案 0 :(得分:268)

您正在寻找axhline(水平轴线)。例如,以下内容将为您提供y = 0.5的水平线。

import matplotlib.pyplot as plt
plt.axhline(y=0.5, color='r', linestyle='-')
plt.show()

sample figure

答案 1 :(得分:15)

如果要在轴上绘制水平线,也可以尝试使用ax.hlines()方法。您需要在数据坐标中指定y位置和xminxmax(即,您在x轴上的实际数据范围)。示例代码段为:

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(1, 21, 200)
y = np.exp(-x)

fig, ax = plt.subplots()
ax.plot(x, y)
ax.hlines(y=0.2, xmin=4, xmax=20, linewidth=2, color='r')

plt.show()

上面的代码段会在y=0.2的轴上绘制一条水平线。水平线从x=4开始,到x=20结束。生成的图像是:

enter image description here

答案 2 :(得分:7)

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

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

希望能解决问题!

答案 3 :(得分:4)

使用matplotlib.pyplot.hlines

import numpy as np
import matplotlib.pyplot as plt

xs = np.linspace(1, 21, 200)
plt.hlines(y=40, xmin=0, xmax=len(xs), colors='r', linestyles='--', lw=2)
plt.show()

enter image description here

答案 4 :(得分:3)

除了此处最受好评的答案外,还可以在axhline的{​​{1}}上调用plot之后链接pandas

DataFrame

enter image description here

答案 5 :(得分:2)

对于那些总是忘记命令axhline的人来说,这是一个简单明了的方法

plt.plot(x, [y]*len(x))

在您的情况下xs = xy = 40。 如果len(x)很大,则效率低下,你应该使用axhline

答案 6 :(得分:1)

您可以使用plt.grid画一条水平线。

import numpy as np
from matplotlib import pyplot as plt
from scipy.interpolate import UnivariateSpline
from matplotlib.ticker import LinearLocator

# your data here
annual = np.arange(1,21,1)
l = np.random.random(20)
spl = UnivariateSpline(annual,l)
xs = np.linspace(1,21,200)

# plot your data
plt.plot(xs,spl(xs),'b')

# horizental line?
ax = plt.axes()
# three ticks:
ax.yaxis.set_major_locator(LinearLocator(3))
# plot grids only on y axis on major locations
plt.grid(True, which='major', axis='y')

# show
plt.show()

random data plot example

答案 7 :(得分:-2)

pylab.plot(...) 可以覆盖给定坐标的水平或垂直线

import pylab as pl  
import numpy as np

observations = [0.797, 1.116, 1.071, 0.998, -0.333, 1.129, 0.381, 0.815, 1.28715,
    0.727, 1.309147, 2.492, 0.946, 0.486536, 0.382539, -0.482, -0.208923,
    0.981166, 0.499, 0.022, 0.747333, -0.045, 0.27304, -1.386, 0.654258, 
    -0.43931, -2.012764, -0.387, -0.730, 0.812032, -0.229, -0.286, -0.293,
    -0.483649, 0.232185, -0.027, 0.142, 0.173, -0.618, 0.393, 0.534, 0.804,
    -0.867, 0.776, 0.342, 0.797, 0.550, -0.215, 0.706, -0.973] 

targets = [-0.007, -0.029, -0.025, -0.0119, -0.0719, -0.1283, -0.1077, -0.0844, 
    -0.0474, -0.0419, -0.016, 0.0613, 0.0949, 0.0553, 0.0353, 0.0173, 0.0467,
    0.0562, 0.0523, -0.0032, 0.0548, 0.0245, 0.0372, 0.0404, 0.0388, 0.0703,
    0.0203, -0.0078, -0.0102, 0.0151, -0.0048, -0.0027, 0.0215, -0.0063, -0.0216,
    -0.0618, -0.0172, 0.0212, -0.0203, -0.006, 0.0438, 0.0642, 0.0365, 0.0124,
    -0.0332, -0.064, 0.0061, -0.0007, -0.0242, -0.036] 
 
#scatter plot using x and y points.  c stands for color.  s stands for size 
pl.scatter(observations, targets, c='red', s=5.5) 
 
max_feature_float = max(observations) 
horizontal_line_start_position = 0 
num_dots_on_horizontal_line = 20  
xs = np.linspace(horizontal_line_start_position,max_feature_float, 
    num_dots_on_horizontal_line) 
horiz_line_data = np.array([0 for i in range(len(xs))]) 
pl.plot(xs, horiz_line_data, 'b--') 
 
max_tars_float = max(targets) 
#make a vertical green line starting at (x=0,y=0) and going to (x=0, y=2) ) 
pl.plot((0, 0), (0, max_tars_float), 'g-') 
 
#define the feature and targets axis legend names and title. 
pl.xlabel("observation") 
pl.ylabel("target") 
pl.title('Scatterplot of target against observation.') 
pl.show()

enter image description here