当前位置 博文首页 > python一绘制元二次方程曲线的实例分析

    python一绘制元二次方程曲线的实例分析

    作者:小妮浅浅 时间:2021-08-04 18:18

    说明

    1、Matplotlib函数可以绘制图形,使用plot函数绘制曲线。

    2、需要将200个点的x坐标和Y坐标分别以序列的形式输入plot函数,然后调用show函数来显示图形。

    实例

    import matplotlib.pyplot as plt
    #200个点的x坐标
    x=range(-100,100)
    #生成y点的坐标
    y=[i**2 for i in x ]
    #绘制一元二次曲线
    plt.plot(x,y)
    #调用savefig将一元二次曲线保存为result.jpg
    plt.savefig('result.jpg') #如果直接写成 plt.savefig('cos') 会生成cos.png
    plt.show()

    实例扩展:

    import matplotlib.pyplot as plt
    
    # 定义定义域[-10, 11]
    x_val = [i for i in range(-10,11)]
    # 求解应变量
    y_val = [5*x**2 for x in x_val]
    print(x_val)
    print(y_val)
    
    # 设置matplotlib作图工具
    fig, ax = plt.subplots()
    ax.plot(x_val, y_val)
    ax.set(xlabel='x(independentVariable)', ylabel='y(strain)', title='On the equation of a quadratic function')
    ax.grid()
    
    # 保存图片
    fig.savefig("test.png")
    # 展示图片
    plt.show()
    
    jsjbwy
    下一篇:没有了