Nov-21-2019, 05:18 PM
Hey!
I've been trying to solve a problem with matlib and Tkinter where i am unable to assign a number to points in a subplot. I'm trying to make a script for creating Bezier curves for my homework assignment and have been stuck on this.
Thanks in advance! Have a good day
I've been trying to solve a problem with matlib and Tkinter where i am unable to assign a number to points in a subplot. I'm trying to make a script for creating Bezier curves for my homework assignment and have been stuck on this.
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) ax1 = fig.add_subplot(111).plot(xvals, yvals) ax2 = fig.add_subplot(111).plot(xpoints, ypoints, "ro") for nr in range(len(points)): 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()here's the problematic part
#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) ax1 = fig.add_subplot(111).plot(xvals, yvals) ax2 = fig.add_subplot(111).plot(xpoints, ypoints, "ro") for nr in range(len(points)): ax2.text(points[nr][0], points[nr][0], nr)since matlib is creating a weird list as ax2, I seem to be unable to assign the numbers to the indiviual points.
Thanks in advance! Have a good day