Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python tkinter中的悬停功能matplotlib图形_Python_Matplotlib_Tkinter_Graph_Annotations - Fatal编程技术网

Python tkinter中的悬停功能matplotlib图形

Python tkinter中的悬停功能matplotlib图形,python,matplotlib,tkinter,graph,annotations,Python,Matplotlib,Tkinter,Graph,Annotations,我在tkinter窗口中嵌入的图形上显示注释时遇到一些问题 当显示在matplotlib窗口中时,悬停功能可以正常工作,但一旦嵌入,它似乎就失去了该功能 要查看图表应该如何响应,请取消注释底部附近的“#plt.show”命令 正在寻找有关如何在tkinter窗口中显示注释的帮助。 谢谢 import tkinter import matplotlib.pyplot as plt from matplotlib.backends.backend_tkagg import FigureCanvasT

我在tkinter窗口中嵌入的图形上显示注释时遇到一些问题

当显示在matplotlib窗口中时,悬停功能可以正常工作,但一旦嵌入,它似乎就失去了该功能

要查看图表应该如何响应,请取消注释底部附近的“#plt.show”命令

正在寻找有关如何在tkinter窗口中显示注释的帮助。
谢谢

import tkinter
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
# Implement the default Matplotlib key bindings.
from matplotlib.backend_bases import key_press_handler


def update_annot(ind, line, annot, ydata):
    x, y = line.get_data()
    annot.xy = (x[ind["ind"][0]], y[ind["ind"][0]])
    # Get x and y values, then format them to be displayed
    x_values = " ".join(list(map(str, ind["ind"])))
    y_values = " ".join(str(ydata[n]) for n in ind["ind"])
    # Format of label can be changed here
    text = "{}, {}".format(x_values, y_values)
    annot.set_text(text)
    annot.get_bbox_patch().set_alpha(0.4)


def hover(event, line, annot, ydata):
    vis = annot.get_visible()
    if event.inaxes == ax:
        # Draw annotations if cursor in right position
        cont, ind = line.contains(event)
        if cont:
            update_annot(ind, line, annot, ydata)
            annot.set_visible(True)
            fig.canvas.draw_idle()
        else:
            # Don't draw annotations
            if vis:
                annot.set_visible(False)
                fig.canvas.draw_idle()


def plot_line(x, y):
    line, = plt.plot(x, y, marker="o")
    # Annotation style may be changed here
    annot = ax.annotate("", xy=(0, 0), xytext=(-20, 20), textcoords="offset points",
                        bbox=dict(boxstyle="round", fc="w"),
                        arrowprops=dict(arrowstyle="->"))
    annot.set_visible(False)
    fig.canvas.mpl_connect("motion_notify_event",
                           lambda event: hover(event, line, annot, y))


def on_key_press(event):
    print("you pressed {}".format(event.key))
    key_press_handler(event, canvas)


root = tkinter.Tk()
root.wm_title("Embedding in Tk")
x1 = range(21)
y1 = range(0, 21)
# Plot line graphs
fig, ax = plt.subplots()
plot_line(x1, y1)
# plt.show()
canvas = FigureCanvasTkAgg(fig, master=root)  # A tk.DrawingArea.
canvas.draw()
canvas.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1)

canvas.mpl_connect("key_press_event", on_key_press)


def _quit():
    root.quit()     # stops mainloop
    root.destroy()  # this is necessary on Windows to prevent
    # Fatal Python Error: PyEval_RestoreThread: NULL tstate


button = tkinter.Button(master=root, text="Quit", command=_quit)
button.pack(side=tkinter.BOTTOM)

tkinter.mainloop()
# If you put root.destroy() here, it will cause an error if the window is
# closed with the window manager.