Python 如何使用matplotlib在函数图上绘制点?

Python 如何使用matplotlib在函数图上绘制点?,python,matplotlib,Python,Matplotlib,我有 我还想在图上画一些点。例如,当x为0时,y为-4.49。因此,我想绘制一个x,y点的列表。如何在同一绘图上执行此操作?您可以在函数调用参数中添加点: def f(x): return (x**2 / 10) - 2 * np.sin(x) def plot_fn(): x = np.arange(-10, 10, 0.1) fn = f(x) fig = plt.figure() ax = fig.add_subplot(1, 1, 1)

我有


我还想在图上画一些点。例如,当
x
为0时,
y
为-4.49。因此,我想绘制一个
x,y
点的列表。如何在同一绘图上执行此操作?

您可以在函数调用参数中添加点:

def f(x):
    return (x**2 / 10) - 2 * np.sin(x)


def plot_fn():
    x = np.arange(-10, 10, 0.1)
    fn = f(x)

    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1)

    # Move left y-axis and bottim x-axis to centre, passing through (0,0)
    ax.spines['left'].set_position('center')
    ax.spines['bottom'].set_position('center')

    # Eliminate upper and right axes
    ax.spines['right'].set_color('none')
    ax.spines['top'].set_color('none')

    # Show ticks in the left and lower axes only
    ax.xaxis.set_ticks_position('bottom')
    ax.yaxis.set_ticks_position('left')

    plt.plot(x, fn)
    plt.show()

您可以在函数调用参数中添加点:

def f(x):
    return (x**2 / 10) - 2 * np.sin(x)


def plot_fn():
    x = np.arange(-10, 10, 0.1)
    fn = f(x)

    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1)

    # Move left y-axis and bottim x-axis to centre, passing through (0,0)
    ax.spines['left'].set_position('center')
    ax.spines['bottom'].set_position('center')

    # Eliminate upper and right axes
    ax.spines['right'].set_color('none')
    ax.spines['top'].set_color('none')

    # Show ticks in the left and lower axes only
    ax.xaxis.set_ticks_position('bottom')
    ax.yaxis.set_ticks_position('left')

    plt.plot(x, fn)
    plt.show()

如果希望在函数中绘制曲线后能够添加其他点,可以从图形中返回轴实例,然后稍后使用它进行绘制。下面的代码对此进行了解释

def plot_fn(xpoints=None, ypoints=None):
   #...your code before plt.show
   if x is not None:
       ax.plot(x_points , y_points, 'go')
   plt.show()

plot_fn([0], [-4.99])

如果希望在函数中绘制曲线后能够添加其他点,可以从图形中返回轴实例,然后稍后使用它进行绘制。下面的代码对此进行了解释

def plot_fn(xpoints=None, ypoints=None):
   #...your code before plt.show
   if x is not None:
       ax.plot(x_points , y_points, 'go')
   plt.show()

plot_fn([0], [-4.99])

@Shamoon:您可以使用alpha参数,比如说
alpha=0.4
,使点看起来像faded@Shamoon:您可以使用alpha参数,例如
alpha=0.4
,使点看起来褪色