Python-查找绘图与Python绘图上的axhline交叉的位置

Python-查找绘图与Python绘图上的axhline交叉的位置,python,matplotlib,statsmodels,Python,Matplotlib,Statsmodels,我正在对一些简单的数据进行分析,并试图绘制自相关和部分自相关。使用这些曲线图,我试图找到要在我的ARIMA模型中绘制的P和Q值 我可以在图表上看到,但我想知道,对于每个图表,是否可以明确地找到绘图与axhline相交的位置 plt.subplot(122) plt.plot(lag_pacf) plt.axhline(y=0, linestyle = '--', color = 'grey') plt.axhline(y=-1.96/np.sqrt(len(log_moving_average_

我正在对一些简单的数据进行分析,并试图绘制自相关和部分自相关。使用这些曲线图,我试图找到要在我的ARIMA模型中绘制的P和Q值

我可以在图表上看到,但我想知道,对于每个图表,是否可以明确地找到绘图与axhline相交的位置

plt.subplot(122)
plt.plot(lag_pacf)
plt.axhline(y=0, linestyle = '--', color = 'grey')
plt.axhline(y=-1.96/np.sqrt(len(log_moving_average_difference)),linestyle  = '--',color = 'red')
plt.axhline(y=1.96/np.sqrt(len(log_moving_average_difference)),linestyle = '--', color = 'green')
plt.title('Partial Autocorelation Function')
在上面的代码中,我能找到并显示lag_pacf图与我预先确定的axhline的交叉点吗


谢谢

您需要计算lag_pacf和y的线段之间的交点:

from matplotlib import pyplot as plt
import numpy as np
lag_pacf = np.random.randint(-10,10,30)
log_moving_average_difference = [i for i in range(30)]
#plt.subplot(122)
plt.plot(lag_pacf)
plt.axhline(y=0, linestyle = '--', color = 'grey')
plt.axhline(y=-1.96/np.sqrt(len(log_moving_average_difference)),linestyle  = '--',color = 'red')
plt.axhline(y=1.96/np.sqrt(len(log_moving_average_difference)),linestyle = '--', color = 'green')
plt.title('Partial Autocorelation Function')
plt.xlim(0,30)
plt.ylim(-10,10)
plt.show()

def line_intersection(line1, line2):
    xdiff = (line1[0][0] - line1[1][0], line2[0][0] - line2[1][0])
    ydiff = (line1[0][1] - line1[1][1], line2[0][1] - line2[1][1]) #Typo was here

    def det(a, b):
        return a[0] * b[1] - a[1] * b[0]

    div = det(xdiff, ydiff)
    if div == 0:
        return None

    d = (det(*line1), det(*line2))
    x = det(d, xdiff) / div
    y = det(d, ydiff) / div
    return x, y

def near(a, b, rtol=1e-5, atol=1e-8):
    return abs(a - b) < (atol + rtol * abs(b))
def crosses(line1, line2):
    """
    Return True if line segment line1 intersects line segment line2 and 
    line1 and line2 are not parallel.
    """
    (x1,y1), (x2,y2) = line1
    (u1,v1), (u2,v2) = line2
    (a,b), (c,d) = (x2-x1, u1-u2), (y2-y1, v1-v2)
    e, f = u1-x1, v1-y1
    denom = float(a*d - b*c)
    if near(denom, 0):
        # parallel
        return False
    else:
        t = (e*d - b*f)/denom
        s = (a*f - e*c)/denom
        # When 0<=t<=1 and 0<=s<=1 the point of intersection occurs within the
        # line segments
        return 0<=t<=1 and 0<=s<=1

plt.plot(lag_pacf)
plt.axhline(y=0, linestyle = '--', color = 'grey')
plt.axhline(y=-1.96/np.sqrt(len(log_moving_average_difference)),linestyle  = '--',color = 'red')
plt.axhline(y=1.96/np.sqrt(len(log_moving_average_difference)),linestyle = '--', color = 'green')
plt.title('Partial Autocorelation Function')

yys = [0,-1.96/np.sqrt(len(log_moving_average_difference)),1.96/np.sqrt(len(log_moving_average_difference))]
xx, yy = [],[]
xo,yo = [k for k in range(30)],lag_pacf
d = 20
for i in range(1,len(lag_pacf)):
    for k in yys:
        p1 = np.array([xo[i-1],yo[i-1]],dtype='float')
        p2 = np.array([xo[i],yo[i]],dtype='float')
        k1 = np.array([xo[i-1],k],dtype='float')
        k2 = np.array([xo[i],k],dtype='float')
        if crosses((p2,p1),(k1,k2)):
            seg = line_intersection((p2,p1),(k1,k2))
            if seg is not None:
                xx.append(seg[0])
                yy.append(seg[1]-d)
                plt.scatter(seg[0],seg[1],c='red')
plt.xlim(0,30)
plt.ylim(-10,10)
plt.show()
从matplotlib导入pyplot作为plt
将numpy作为np导入
lag_pacf=np.random.randint(-10,10,30)
对数\移动\平均\差值=[i为范围内的i(30)]
#小地块(122)
plt.plt.plot(lag_pacf)
plt.axhline(y=0,线型='--',颜色='灰色')
plt.axhline(y=-1.96/np.sqrt(len(对数移动平均差)),线型='--',颜色='红色')
plt.axhline(y=1.96/np.sqrt(len(对数移动平均差)),线型='--',颜色='绿色')
plt.title(‘偏自相关函数’)
plt.xlim(0,30)
plt.ylim(-10,10)
plt.show()
def测线交叉点(测线1、测线2):
xdiff=(第1行[0][0]-第1行[1][0],第2行[0][0]-第2行[1][0])
ydiff=(第1行[0][1]-第1行[1][1],第2行[0][1]-第2行[1][1])#这里有打字错误
def数据(a、b):
返回a[0]*b[1]-a[1]*b[0]
div=det(xdiff,ydiff)
如果div==0:
一无所获
d=(数据表(*第1行),数据表(*第2行))
x=det(d,xdiff)/div
y=det(d,ydiff)/div
返回x,y
def接近(a、b、rtol=1e-5、atol=1e-8):
返回abs(a-b)<(atol+rtol*abs(b))
def交叉(第1行、第2行):
"""
如果线段line1与线段line2相交,则返回True
第1行和第2行不平行。
"""
(x1,y1),(x2,y2)=第1行
(u1,v1),(u2,v2)=第2行
(a,b),(c,d)=(x2-x1,u1-u2),(y2-y1,v1-v2)
e、 f=u1-x1,v1-y1
denom=浮点数(a*d-b*c)
如果接近(denom,0):
#平行的
返回错误
其他:
t=(e*d-b*f)/denom
s=(a*f-e*c)/denom

#当0时,需要计算lag_pacf和y的线段之间的交点:

from matplotlib import pyplot as plt
import numpy as np
lag_pacf = np.random.randint(-10,10,30)
log_moving_average_difference = [i for i in range(30)]
#plt.subplot(122)
plt.plot(lag_pacf)
plt.axhline(y=0, linestyle = '--', color = 'grey')
plt.axhline(y=-1.96/np.sqrt(len(log_moving_average_difference)),linestyle  = '--',color = 'red')
plt.axhline(y=1.96/np.sqrt(len(log_moving_average_difference)),linestyle = '--', color = 'green')
plt.title('Partial Autocorelation Function')
plt.xlim(0,30)
plt.ylim(-10,10)
plt.show()

def line_intersection(line1, line2):
    xdiff = (line1[0][0] - line1[1][0], line2[0][0] - line2[1][0])
    ydiff = (line1[0][1] - line1[1][1], line2[0][1] - line2[1][1]) #Typo was here

    def det(a, b):
        return a[0] * b[1] - a[1] * b[0]

    div = det(xdiff, ydiff)
    if div == 0:
        return None

    d = (det(*line1), det(*line2))
    x = det(d, xdiff) / div
    y = det(d, ydiff) / div
    return x, y

def near(a, b, rtol=1e-5, atol=1e-8):
    return abs(a - b) < (atol + rtol * abs(b))
def crosses(line1, line2):
    """
    Return True if line segment line1 intersects line segment line2 and 
    line1 and line2 are not parallel.
    """
    (x1,y1), (x2,y2) = line1
    (u1,v1), (u2,v2) = line2
    (a,b), (c,d) = (x2-x1, u1-u2), (y2-y1, v1-v2)
    e, f = u1-x1, v1-y1
    denom = float(a*d - b*c)
    if near(denom, 0):
        # parallel
        return False
    else:
        t = (e*d - b*f)/denom
        s = (a*f - e*c)/denom
        # When 0<=t<=1 and 0<=s<=1 the point of intersection occurs within the
        # line segments
        return 0<=t<=1 and 0<=s<=1

plt.plot(lag_pacf)
plt.axhline(y=0, linestyle = '--', color = 'grey')
plt.axhline(y=-1.96/np.sqrt(len(log_moving_average_difference)),linestyle  = '--',color = 'red')
plt.axhline(y=1.96/np.sqrt(len(log_moving_average_difference)),linestyle = '--', color = 'green')
plt.title('Partial Autocorelation Function')

yys = [0,-1.96/np.sqrt(len(log_moving_average_difference)),1.96/np.sqrt(len(log_moving_average_difference))]
xx, yy = [],[]
xo,yo = [k for k in range(30)],lag_pacf
d = 20
for i in range(1,len(lag_pacf)):
    for k in yys:
        p1 = np.array([xo[i-1],yo[i-1]],dtype='float')
        p2 = np.array([xo[i],yo[i]],dtype='float')
        k1 = np.array([xo[i-1],k],dtype='float')
        k2 = np.array([xo[i],k],dtype='float')
        if crosses((p2,p1),(k1,k2)):
            seg = line_intersection((p2,p1),(k1,k2))
            if seg is not None:
                xx.append(seg[0])
                yy.append(seg[1]-d)
                plt.scatter(seg[0],seg[1],c='red')
plt.xlim(0,30)
plt.ylim(-10,10)
plt.show()
从matplotlib导入pyplot作为plt
将numpy作为np导入
lag_pacf=np.random.randint(-10,10,30)
对数\移动\平均\差值=[i为范围内的i(30)]
#小地块(122)
plt.plt.plot(lag_pacf)
plt.axhline(y=0,线型='--',颜色='灰色')
plt.axhline(y=-1.96/np.sqrt(len(对数移动平均差)),线型='--',颜色='红色')
plt.axhline(y=1.96/np.sqrt(len(对数移动平均差)),线型='--',颜色='绿色')
plt.title(‘偏自相关函数’)
plt.xlim(0,30)
plt.ylim(-10,10)
plt.show()
def测线交叉点(测线1、测线2):
xdiff=(第1行[0][0]-第1行[1][0],第2行[0][0]-第2行[1][0])
ydiff=(第1行[0][1]-第1行[1][1],第2行[0][1]-第2行[1][1])#这里有打字错误
def数据(a、b):
返回a[0]*b[1]-a[1]*b[0]
div=det(xdiff,ydiff)
如果div==0:
一无所获
d=(数据表(*第1行),数据表(*第2行))
x=det(d,xdiff)/div
y=det(d,ydiff)/div
返回x,y
def接近(a、b、rtol=1e-5、atol=1e-8):
返回abs(a-b)<(atol+rtol*abs(b))
def交叉(第1行、第2行):
"""
如果线段line1与线段line2相交,则返回True
第1行和第2行不平行。
"""
(x1,y1),(x2,y2)=第1行
(u1,v1),(u2,v2)=第2行
(a,b),(c,d)=(x2-x1,u1-u2),(y2-y1,v1-v2)
e、 f=u1-x1,v1-y1
denom=浮点数(a*d-b*c)
如果接近(denom,0):
#平行的
返回错误
其他:
t=(e*d-b*f)/denom
s=(a*f-e*c)/denom

#Alex,你什么时候提出的解决方案没有解决你的问题?抱歉,我一直非常忙。我现在要填写:)亚历克斯,提议的解决方案没有解决你的问题吗?对不起,我一直非常忙。我现在就填:)谢谢!对耽误了回来表示歉意。上面的代码是有效的,但我不认为我有足够的技能来编写它!谢谢对耽误了回来表示歉意。上面的代码是有效的,但我不认为我有足够的技能来编写它!