0

I have

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()

I also want to plot some points on the graph as well. For example, when x is 0, then the y is -4.49. So I want to plot a list of x,y points. How can I do this on the same plot?

0

2 Answers 2

1

You can add the points in the function call arguments:

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])
Sign up to request clarification or add additional context in comments.

1 Comment

@Shamoon: You can use the alpha parameter, say alpha=0.4 to make the points look faded
1

If you want to be able to add the additional points later after plotting the curve in the function, you can return the axis instance from the figure and then use it later to plot. Following code explains it

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

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

    # Your spines related code here
    # ........

    ax.plot(x, fn)
    return ax

ax_ = plot_fn()

x_data = [0, 1]
y_data = [-4.49, 3.12]
ax_.scatter(x_data, y_data, c='r')
plt.show()

enter image description here

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.