首页 > 解决方案 > 如何自动缩放绘图画布

问题描述

我有大小为 10x10 的情节

y, x =  np.mgrid[-10:10:100j, -10:10:100j]

当我画小图形时 - 一切都很好。但是当坐标超出图表时,我会看到问题。如何自动缩放绘图画布?

def f(x,a,b):
    return x**3+a*x + b

def show_plot(plt, A,B,P,Q): #A=-7, B=10, P=4, Q=8
    y, x =  np.mgrid[-10:10:100j, -10:10:100j]

    xp = P
    yp = np.sqrt(f(xp,A,B))

    xq = Q
    yq = np.sqrt(f(xq,A,B))

    m = 0.0

    if P==Q:
        xx = 3*pow(xp,2)
        m = (xx+A)/(2*yp)
        pass
    else:
        m=(yp-yq)/(xp-xq) #m

    xr = m*m-xp-xq
    yr1 = yp+m*(xr-xp)
    yr2 = yq+m*(xr-xq)

    plt.contour(x, y, y**2 - f(x,A,B), levels=[0])

    b = -xp*m + yp
    poly = np.poly1d([-1, m**2, 2*m*b+3, b**2-5])

    x = np.roots(poly)
    y = np.sqrt(f(x,A,B))

    x = np.linspace(-5, 5)
    plt.plot(x, m*x+b)

    coordinates = [('P',xp,yp), ('Q',xq,yq), ('-R',xr,-yr1)]

    for x in coordinates: 
        plt.plot(x[1], x[2], 'ro-')
        plt.annotate(x[0], xy=(x[1]-0.2, x[2]+0.2), horizontalalignment='right', verticalalignment='top',family='fantasy')
        
    plt.plot([xr,xr],[yr1,-yr1],'g-')   
    plt.grid(True)

在此处输入图像描述

在此处输入图像描述

标签: pythonmatplotlib

解决方案


推荐阅读