[Python] Matplotlib: How does it work and why does it only show the positive x-axis?...

Discussão em 'Python' iniciado por Stack, Julho 10, 2025.

  1. Stack

    Stack Membro Participativo

    I have code where I want the user to be able to give a function, which will then be graphed with Matplotlib. So I look up online how Matplotlib works, and I end up with this code:

    framegraph = Frame(master=window)
    fig = Figure(figsize = (5, 5), dpi = 100)
    y = [x**2 for x in range(101)]
    plot1 = fig.add_subplot(111)
    plot1.plot(y)
    canvas = FigureCanvasTkAgg(fig, master = framegraph)
    canvas.draw()
    canvas.get_tk_widget().pack()
    toolbar = NavigationToolbar2Tk(canvas, framegraph)
    toolbar.update()
    canvas.get_tk_widget().pack()
    framegraph.grid(row=0, column=0,padx=10, pady=10)

    def update():
    global entryfunc, canvas, plot1, framegraph, window, y
    x = var('x')
    func = entryfunc.get()
    func = sympify(func)
    y = [func.subs(x, z) for z in range(101)]
    plot1.clear()
    plot1.plot(y)
    canvas.draw()
    canvas.get_tk_widget().pack()


    And it works well, except, the graph looks like this:

    Graph output

    It's the squared function, so it should also be graphed on the negative x-axis, but I cannot figure out how to do it.

    Continue reading...

Compartilhe esta Página