1

I have a graph plot and now I need a circle on it for every point where y equals zero. The graph works fine but the circle (code in the if statement) gives an error:

import numpy as np
import matplotlib.pyplot as plt


def graph(formula, x_range):
    x = np.array(x_range)
    y = formula(x)  # <-----
    #if x == y:
    if y == 0:
        circle2=plt.Circle((x,y),.2,color='b')
        fig = plt.gcf()
        fig.gca().add_artist(circle2)
        plt.show()
    plt.plot(x, y, 'r--')
    plt.show()

graph(lambda x: (x+2) * (x-1) * (x-2), range(-3,3))
1
  • You'd better to include the error traceback in the question. Commented Sep 14, 2014 at 8:27

1 Answer 1

2

You're passing an array x, y. Pass a single values.

def graph(formula, x_range):
    x = np.array(x_range)
    y = formula(x)
    for x0, y0 in zip(x, y):
        if y0 == 0:
            circle2 = plt.Circle((x0, y0), 0.1, color='b')  # <------
            fig = plt.gcf()
            fig.gca().add_artist(circle2)
    plt.plot(x, y, 'r--')
    plt.show()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.