#matplotlib #tkinter #tkinter-canvas #matplotlib-widget
Вопрос:
Я пытаюсь добавить виджет курсора в свой график, который отображает данные о запасах, встроенные в окно tkinter.
У меня было довольно много проблем с matplotlib, встроенным в tkinter, так что это неудивительно.
Я попробовал стандартный способ следующим образом:
cursor = Cursor(ax, horizOn=True, vertOn=True, useblit=False, color='red', linewidth=2)
Однако это не дало ни результатов, ни ошибок, так что мне не с чем работать. Сам график встроен в холст, который затем встроен в рамку. Это мой код для встраивания matplotlib в tkinter:
import yfinance as yf from datetime import date from datetime import datetime import datetime as dt import matplotlib.pyplot as plt import matplotlib.dates as mdates from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk from matplotlib.figure import Figure import matplotlib.ticker as mticker from matplotlib.widgets import Cursor def function_thing(self, root): fig = plt.Figure(figsize=(4, 4), dpi=100) ax = fig.add_subplot(1, 1, 1) x = [*bunch of dates*] y_axis = [5,6,7,8,9,10,11] ax.plot(x, y_axis) ax.xaxis.set_major_formatter(mdates.DateFormatter('%m-%d')) ax.xaxis.set_major_locator(mticker.MaxNLocator(7)) ax.set_xticklabels(x,rotation=45) # Matplotlib cursor cursor = Cursor(ax, horizOn=True, vertOn=True, useblit=False, color='red', linewidth=2) def onClick(event): x1, y1 = event.xdata, event.ydata print(x1,y1) fig.canvas.mpl_connect('button_press_event',onClick) #embed into tkinter canvas = FigureCanvasTkAgg(fig, master = new_frame) canvas.draw() toolbar = NavigationToolbar2Tk(canvas, self.detailed_frame) toolbar.update() canvas.get_tk_widget().pack(side=TOP, fill=BOTH, expand=1) get_widz = canvas.get_tk_widget() get_widz.pack(side=TOP, fill=BOTH, expand=1)