Я пытаюсь использовать matplotlib в Kivy с линейной диаграммой, которая имеет всплывающие подсказки с интерактивными метками данных, когда мышь наводится на различные точки на диаграмме.Я знаю, что это может быть сделано в matplotlib на основе этого примера: Можно ли заставить ярлыки появляться при наведении курсора на точку в matplotlib?
При использовании Kivy с matplotlib я получаю сообщение об ошибке, когдаЯ пытаюсь использовать это событие:
self.fig.canvas.mpl_connect ("motion_notify_event", hover)
ошибка: "NameError: наведение имени не определено"
import kivy
import matplotlib
matplotlib.use('module://kivy.garden.matplotlib.backend_kivy')
import matplotlib.pyplot as plt
from kivy.garden.matplotlib import FigureCanvasKivyAgg
from matplotlib.ticker import (MultipleLocator, FormatStrFormatter,
AutoMinorLocator)
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.properties import BooleanProperty, ListProperty, StringProperty,
ObjectProperty
from kivy.lang import Builder
class testApp(App):
def __init__(self, **kwargs):
super(testApp, self).__init__(**kwargs)
def hover(event):
# code to track the mouse position within the matplotlib chart and
figure out what point in the chart it is hovering over, then display
the popup_label defined below
pass
def build(self):
mylayout = FloatLayout()
# create the matplot lib chart and add it to the kivy float layout
self.fig,self.ax = plt.subplots(1)
self.plt_canvas = self.fig.canvas
mylayout.add_widget(self.plt_canvas)
# load x and y axis data into variables and use in chart
year = [1960, 1970, 1980, 1990, 2000, 2010]
pop_pakistan = [44.91, 58.09, 78.07, 107.7, 138.5, 170.6]
pop_india = [449.48, 553.57, 696.783, 870.133, 1000.4, 1309.1]
# set line colors
plt.plot(year, pop_pakistan, color='g')
plt.plot(year, pop_india, color='orange')
# set x and y axis labels and chart title
plt.xlabel('Countries')
plt.ylabel('Population in million')
plt.title('Pakistan India Population till 2010')
# set minor ticks as a multiple of 2
minorLocator = MultipleLocator(2)
self.ax.xaxis.set_minor_locator(minorLocator)
# create a test popup matplotlib chart label
popup_label = self.ax.annotate("(1970, 45)", xy=(1970,70))
popup_label.set_visible(True) #popup label works on the chart as an
example. this would be triggered in the def(hover) event above when
mouse hovers over a point..
self.fig.canvas.mpl_connect("motion_notify_event", hover) #CODE
ERRORS HERE - CAN'T RECOGNIZED HOVER
return mylayout
if __name__ == '__main__':
testApp().run()