简体   繁体   English

使用matplotlib在图中绘制圆

[英]plotting circle in the graph using matplotlib

I have a graph plot and now I need a circle on it for every point where y equals zero. 我有一个图形图,现在对于每个y等于零的点都需要一个圆。 The graph works fine but the circle (code in the if statement) gives an error: 该图工作正常,但圆(if语句中的代码)给出了错误:

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

You're passing an array x , y . 您正在传递数组xy 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM