Python Forum
Tkinter & matplotlib
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Tkinter & matplotlib
#10
(Nov-22-2019, 05:56 PM)Larz60+ Wrote: OK, now that My code copy is correct, I added some debugging printouts as follows:
import tkinter
from tkinter import *
 
from matplotlib.backends.backend_tkagg import *
from matplotlib.backend_bases import key_press_handler
from matplotlib.figure import Figure
from matplotlib import pyplot as plt
from matplotlib import animation
 
import numpy as np
from scipy.special import comb
 
 
def inputik():
    global nPoints
    fig.clear("all")
    nPoints = int(pocetbodu_entry.get())
    pocetbodu_entry.delete(0, "end")
    vykresleni()
 
def bernstein_poly(i, n, t):
    """
     Bernsteinův polynom
     t ... parametr
     n ... počet bodů
     i ... (počet bodů - 1)
 
    """
 
    return comb(n, i) * ( t**(n-i) ) * (1 - t)**i
 
 
def bezier_curve(points, nTimes=1000):
    """
       Ze zadaných bodů a kroku sestrojí bezierovu křivku
    """
 
    nPoints = len(points)
    xPoints = np.array([p[0] for p in points])
    yPoints = np.array([p[1] for p in points])
 
    t = np.linspace(0.0, 1.0, nTimes)
 
    polynomial_array = np.array([ bernstein_poly(i, nPoints-1, t) for i in range(0, nPoints)   ])
 
    xvals = np.dot(xPoints, polynomial_array)
    yvals = np.dot(yPoints, polynomial_array)
 
    return xvals, yvals
 
 
def vykresleni():
 
    if __name__ == "__main__":
        from matplotlib import pyplot as plt
        global ax1
 
        #nPoints = (pocetbodu_entry.get())
        points = np.random.rand(nPoints, 2) * 200
        xpoints = [p[0] for p in points]
        ypoints = [p[1] for p in points]
 
 
        xvals, yvals = bezier_curve(points, nTimes=1000)
 
        print(f"\nPoints")
        for x, y in points:
            print(f"x: {x:20}, y: {y:20}")
        print()

        ax1 = fig.add_subplot(111).plot(xvals, yvals)
        ax2 = fig.add_subplot(111).plot(xpoints, ypoints, "ro")
        for nr in range(len(points)):
            print(f"nr: {nr}, points[nr][0]: {points[nr][0]}, points[nr][1]: {points[nr][1]}")
            ax2.text(points[nr][0], points[nr][0], nr)
 
        for i in range(nPoints-1):
            pomocna_1x = [xpoints[i], xpoints[i+1]]
            pomocna_1y = [ypoints[i], ypoints[i+1]]
            ax3=fig.add_subplot(111).plot(pomocna_1x, pomocna_1y,'b--', linewidth=2)
 
 
        canvas.draw()
 
 
# canvas a vykreslovací ploška
 
root = tkinter.Tk()
root.wm_title("Beziereček")
 
 
fig = plt.Figure(figsize=(5, 4), dpi=100, frameon=False)
canvas = FigureCanvasTkAgg(fig, master=root)  # A tk.DrawingArea.
canvas.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1)
 
toolbar = NavigationToolbar2Tk(canvas, root)
toolbar.update()
canvas.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1)
 
 
def on_key_press(event):
    print("you pressed {}".format(event.key))
    key_press_handler(event, canvas, toolbar)
 
 
canvas.mpl_connect("key_press_event", on_key_press)
 
 
def konec():
    root.quit()
    root.destroy()
 
 
#tlačítka a blbítka
 
pocetbodu = Label(root, text = "Počet řídících bodů: ")
pocetbodu_entry = Entry(root)
buttonQuit = Button(master=root, text="Quit", command=konec)
buttonQuit.pack(side=tkinter.BOTTOM)
buttonSubmit = Button(master=root, text="Submit", command=inputik)
buttonSubmit.pack(side=tkinter.BOTTOM)
pocetbodu_entry.pack(side=tkinter.BOTTOM)
pocetbodu.pack(side=tkinter.BOTTOM)
 
 
 
tkinter.mainloop()
which quickly exposed what I think is part the problem.
your line 69 reads ax2.text(points[nr][0], points[nr][0], nr)
and I think it should be: ax2.text(points[nr][0], points[nr][1], nr)

There are still some issues
Is that correct?

Oh yeah, you're right, line 69 should in fact read as you said. Still can't figure out how to get it into the plot
Reply


Messages In This Thread
Tkinter & matplotlib - by PillyChepper - Nov-21-2019, 05:18 PM
RE: Tkinter & matplotlib - by Larz60+ - Nov-21-2019, 07:34 PM
RE: Tkinter & matplotlib - by PillyChepper - Nov-21-2019, 08:13 PM
RE: Tkinter & matplotlib - by Larz60+ - Nov-22-2019, 02:37 AM
RE: Tkinter & matplotlib - by PillyChepper - Nov-22-2019, 03:40 PM
RE: Tkinter & matplotlib - by Larz60+ - Nov-22-2019, 04:43 PM
RE: Tkinter & matplotlib - by PillyChepper - Nov-22-2019, 05:08 PM
RE: Tkinter & matplotlib - by Larz60+ - Nov-22-2019, 05:18 PM
RE: Tkinter & matplotlib - by Larz60+ - Nov-22-2019, 05:56 PM
RE: Tkinter & matplotlib - by PillyChepper - Nov-23-2019, 10:36 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Interaction between Matplotlib window, Python prompt and TKinter window NorbertMoussy 3 493 Mar-17-2024, 09:37 AM
Last Post: deanhystad
  [Tkinter] Tkinter Matplotlib Animation Graph not rendering dimidgen 3 552 Mar-12-2024, 02:09 PM
Last Post: deanhystad
  How to use rangesliders feature from matplotlib in tkinter? pymn 2 2,973 Feb-28-2022, 05:06 PM
Last Post: pymn
  Tkinter Matplotlib Nepo 1 2,450 Sep-27-2020, 10:20 AM
Last Post: Gribouillis
  Making a Matplotlib in Tkinter using a slider Jemeronimo 1 5,667 Dec-05-2018, 08:06 AM
Last Post: Gribouillis
  how to install numpy and matplotlib to Tkinter elwolv 4 4,764 Dec-02-2017, 06:17 PM
Last Post: Barrowman

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020