[英]Embedding an interactive Matplotlib plot into Tkinter?
I have code for an interactive plot, which allows viewing a 3D image through scrolling slice-wise with the mouse roll.我有一个交互式 plot 的代码,它允许通过用鼠标滚动切片来查看 3D 图像。 It includes also a slide bar to adjust contrast.
它还包括一个用于调整对比度的滑动条。
I have been trying to embed this into a Tkinter GUI, for example with help of this sample code: https://matplotlib.org/examples/user_interfaces/embedding_in_tk.html我一直在尝试将其嵌入到 Tkinter GUI 中,例如借助此示例代码: https://matplotlib.org/examples/user_interfaces/embedding_in_tk.ZFC35FZ30D52FC67A5E362
But I don't really understand where my code is supposed to go in there.但我真的不明白我的代码应该在哪里 go 在那里。
This is the application I currently have:这是我目前拥有的应用程序:
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.widgets import Slider
class IndexTracker(object):
def __init__(self, ax, X):
self.ax = ax
ax.set_title('use scroll wheel to navigate images')
self.X = X
rows, cols, self.slices = X.shape
self.ind = self.slices//2
self.im = ax.imshow(self.X[:, :, self.ind], cmap='gray')
self.update()
def onscroll(self, event):
print("%s %s" % (event.button, event.step))
if event.button == 'up':
self.ind = (self.ind + 1) % self.slices
else:
self.ind = (self.ind - 1) % self.slices
self.update()
def contrast(self, event):
print('Changing contrast')
print(smax.val)
self.im.set_clim([0,smax.val])
self.update()
def update(self):
self.im.set_data(self.X[:, :, self.ind])
self.ax.set_ylabel('slice %s' % self.ind)
self.im.axes.figure.canvas.draw()
##### Create some random volumetric data
im = np.array(np.random.rand(10,10,10))
##### Initialize Tracker object with the data and Slider
fig, ax = plt.subplots(1,1)
axmax = fig.add_axes([0.25, 0.01, 0.65, 0.03])
smax = Slider(axmax, 'Max', 0, np.max(im), valinit=50)
tracker = IndexTracker(ax, im)
fig.canvas.mpl_connect('scroll_event', tracker.onscroll)
smax.on_changed(tracker.contrast)
plt.show()
I don't understand what is exacty that I need to embed into the Tkinter application, is it fig
, or IndexTracker
?我不明白我需要嵌入到 Tkinter 应用程序中的确切内容是
fig
还是IndexTracker
? How do I replace fig.canvas.mpl_connect('scroll_event', tracker.onscroll)
so it works within the TKinter GUI?如何替换
fig.canvas.mpl_connect('scroll_event', tracker.onscroll)
使其在 TKinter GUI 中工作?
There is nothing special embedding this into tkinter
- you create a FigureCanvasTkAgg
object first, and then do the rest.将其嵌入到
tkinter
中并没有什么特别之处 - 您首先创建一个FigureCanvasTkAgg
object,然后执行 rest。 The only thing you need to change is instead of plt
, you need to use Figure
which is shown in the sample you quoted.您唯一需要更改的是
plt
,您需要使用您引用的示例中显示的Figure
。
import numpy as np
from matplotlib.widgets import Slider
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
class IndexTracker(object):
...
import tkinter as tk
root = tk.Tk()
fig = Figure()
canvas = FigureCanvasTkAgg(fig, root)
canvas.get_tk_widget().pack(fill="both", expand=True)
im = np.array(np.random.rand(10,10,10))
ax = fig.subplots(1,1)
axmax = fig.add_axes([0.25, 0.01, 0.65, 0.03])
smax = Slider(axmax, 'Max', 0, np.max(im), valinit=50)
tracker = IndexTracker(ax, im)
canvas.mpl_connect('scroll_event', tracker.onscroll)
canvas.mpl_connect('button_release_event', tracker.contrast) #add this for contrast change
root.mainloop()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.