Pythonmatplotlib:无法使用链接到偶数的函数在图上显示文本

2024-10-02 00:27:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图与matplotlib人物互动,但有些事情没有按计划进行。。。在

下面是脚本的示例和它应该做什么的描述:在test_figure类的init部分,我创建一个图(图1),添加一个子图,绘制100个随机点,为文本框定义属性,并将事件“pick_event”连接到函数onpick()。 当使用这个函数获取数据的x和y坐标时,应该使用它来绘制一条使用它们的线(在图2中),同时使用文本在图1上显示x和y坐标。在

除了最后一部分:x和y坐标没有显示在图1上,我不知道为什么。。。你有什么想法吗?在

谢谢!在

import numpy as np
import matplotlib.pyplot as plt

class test_figure:

    def __init__(self):
        self.fig = plt.figure(1)
        self.ax = self.fig.add_subplot(111)
        self.ax.set_title('Click on data')
        # 3 pixels around point
        line, = self.ax.plot(np.random.rand(100), 'o', picker=3) 
        self.fig.canvas.mpl_connect('pick_event', self.onpick)

    def onpick(self, event):
        thisline = event.artist
        xdata = thisline.get_xdata()
        ydata = thisline.get_ydata()
        ind = event.ind
        self.fig2 = plt.figure(2)
        self.ax2 = self.fig2.add_subplot(111)
        line, = self.ax2.plot(xdata[ind]*range(60)+ydata[ind])
        self.fig.text(0.5,0.5,'pouet :'+str(xdata[ind]))

a = test_figure()

Tags: testselfeventmatplotlibinitfig绘制plt

热门问题