Skip to content

Commit 818133a

Browse files
kopytjukQuLogic
andauthored
Show Axes updating in "Embedding in Tk" example (#20218)
* Update plot in tk-embedding example * Flake 8 fix * Change from button to slider * Fix linting. * Update data via set_data(). * Labels to physical units. * Update examples/user_interfaces/embedding_in_tk_sgskip.py Co-authored-by: Elliott Sales de Andrade <quantum.analyst@gmail.com> * Update examples/user_interfaces/embedding_in_tk_sgskip.py Co-authored-by: Elliott Sales de Andrade <quantum.analyst@gmail.com> Co-authored-by: Elliott Sales de Andrade <quantum.analyst@gmail.com>
1 parent 69b51f1 commit 818133a

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

examples/user_interfaces/embedding_in_tk_sgskip.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@
2121

2222
fig = Figure(figsize=(5, 4), dpi=100)
2323
t = np.arange(0, 3, .01)
24-
fig.add_subplot().plot(t, 2 * np.sin(2 * np.pi * t))
24+
ax = fig.add_subplot()
25+
line, = ax.plot(t, 2 * np.sin(2 * np.pi * t))
26+
ax.set_xlabel("time [s]")
27+
ax.set_ylabel("f(t)")
2528

2629
canvas = FigureCanvasTkAgg(fig, master=root) # A tk.DrawingArea.
2730
canvas.draw()
@@ -30,18 +33,34 @@
3033
toolbar = NavigationToolbar2Tk(canvas, root, pack_toolbar=False)
3134
toolbar.update()
3235

33-
3436
canvas.mpl_connect(
3537
"key_press_event", lambda event: print(f"you pressed {event.key}"))
3638
canvas.mpl_connect("key_press_event", key_press_handler)
3739

38-
button = tkinter.Button(master=root, text="Quit", command=root.quit)
40+
button_quit = tkinter.Button(master=root, text="Quit", command=root.quit)
41+
42+
43+
def update_frequency(new_val):
44+
# retrieve frequency
45+
f = float(new_val)
46+
47+
# update data
48+
y = 2 * np.sin(2 * np.pi * f * t)
49+
line.set_data(t, y)
50+
51+
# required to update canvas and attached toolbar!
52+
canvas.draw()
53+
54+
55+
slider_update = tkinter.Scale(root, from_=1, to=5, orient=tkinter.HORIZONTAL,
56+
command=update_frequency, label="Frequency [Hz]")
3957

4058
# Packing order is important. Widgets are processed sequentially and if there
4159
# is no space left, because the window is too small, they are not displayed.
4260
# The canvas is rather flexible in its size, so we pack it last which makes
4361
# sure the UI controls are displayed as long as possible.
44-
button.pack(side=tkinter.BOTTOM)
62+
button_quit.pack(side=tkinter.BOTTOM)
63+
slider_update.pack(side=tkinter.BOTTOM)
4564
toolbar.pack(side=tkinter.BOTTOM, fill=tkinter.X)
4665
canvas.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1)
4766

0 commit comments

Comments
 (0)