python - 使用 Matplotlib 和系数绘制多项式

标签 python matplotlib

我的代码是:

import numpy as np
import matplotlib as plt
polyCoeffiecients = [1,2,3,4,5]
plt.plot(PolyCoeffiecients)
plt.show()

结果是描述 1,2,3,4,5 中的点的直线以及它们之间的直线,而不是具有 1,2,3,4,5 的 5 次多项式作为它的系数 ( P(x) = 1 + 2x + 3x + 4x + 5x)

我该如何绘制仅包含其系数的多项式?

最佳答案

Eyzuky,看看这是不是你想要的:

import numpy as np
from matplotlib import pyplot as plt

def PolyCoefficients(x, coeffs):
    """ Returns a polynomial for ``x`` values for the ``coeffs`` provided.

    The coefficients must be in ascending order (``x**0`` to ``x**o``).
    """
    o = len(coeffs)
    print(f'# This is a polynomial of order {o}.')
    y = 0
    for i in range(o):
        y += coeffs[i]*x**i
    return y

x = np.linspace(0, 9, 10)
coeffs = [1, 2, 3, 4, 5]
plt.plot(x, PolyCoefficients(x, coeffs))
plt.show()

关于python - 使用 Matplotlib 和系数绘制多项式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37352098/

相关文章:

python - 使用 Series.plot() 绘制每小时数据

python-3.x - imshow 的有效数据

python - 无法解析余数 :

python - 使用 Pandas 查找重复的行并将其 reshape 为一行

python - matplotlib.pyplot 与参数 s 中的 matplotlib.pyplot.scatter 出现问题

python - Matplotlib 正在打印线图两次/多次

python - 多边形内的轮廓不规则数据

python - 尝试使用 seek() 获取 csv 文件的最后一行时出现 AttributeError

python - 使用 Python Peewee 指定要连接的表

python - 将具有形状 (M, N, P) 数组的 numpy int 数组高效转换为具有 (N, P) 形状的二维对象数组