【问题标题】:Update plot on mouse click, after pressing a button and plotting, Python鼠标点击更新绘图,按下按钮并绘图后,Python
【发布时间】:2021-04-27 01:14:03
【问题描述】:

你好! 所以我得到了这个按钮,我单击以绘制一个图形,然后我希望该图形随着每次鼠标按钮按下而更新,以绘制一个十字准线。

但我无法在按下按钮后更新图形,但如果我只是在课堂外绘制,效果会很好。

import cross_hair
import numpy as np
import matplotlib.pyplot as plt


from PyQt5.QtWidgets import QApplication, QWidget, QPushButton

导入系统

class PushButton(QWidget):
    def __init__(self):
        super(PushButton,self).__init__()
        self.initUI()

    def initUI(self):
        self.setWindowTitle("PushButton")
        self.setGeometry(400,400,300,260)
        self.closeButton = QPushButton(self)
        self.closeButton.setText("Press")          #text                     
        self.closeButton.clicked.connect(self.button_pressed)


    def button_pressed(self):
        #Fuction variables

        self.x= np.arange(0, 1, 0.01)
        self.y = np.sin(2 * 2 * np.pi * self.x)
        #Figure
        self.fig, self.ax = plt.subplots()
        #title
        self.ax.set_title('Snapping cursor')
        #Plotting
        self.line, = self.ax.plot(self.x, self.y, 'o')
        #
        snap_cursor = cross_hair.SnappingCursor(self.ax, self.line)
        self.fig.canvas.mpl_connect('button_press_event', snap_cursor.on_mouse_click)
        plt.show()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = PushButton()
    ex.show()
    sys.exit(app.exec_())

十字线代码如下所示 (cross_hair.py)

import numpy as np

类 SnappingCursor:

def __init__(self, ax, line):
    self.ax = ax
    self.horizontal_line = ax.axhline(color='k', lw=0.8, ls='--')
    self.vertical_line = ax.axvline(color='k', lw=0.8, ls='--')
    self.x, self.y = line.get_data()
    self._last_index = None
    # text location in axes coords
    self.text = ax.text(0.72, 0.9, '', transform=ax.transAxes)

def set_cross_hair_visible(self, visible):
    need_redraw = self.horizontal_line.get_visible() != visible
    self.horizontal_line.set_visible(visible)
    self.vertical_line.set_visible(visible)
    self.text.set_visible(visible)
    return need_redraw

def on_mouse_click(self, event):
    if not event.inaxes:
        self._last_index = None
        need_redraw = self.set_cross_hair_visible(False)
        print('you no pressed' )#, event.button, event.xdata, event.ydata)

        if need_redraw:
            self.ax.figure.canvas.draw()
    else:
        self.set_cross_hair_visible(True)
        x, y = event.xdata, event.ydata

        print('you pressed')  # , event.button, event.xdata, event.ydata)

        index = min(np.searchsorted(self.x, x), len(self.x) - 1)
        if index == self._last_index:
            return  # still on the same data point. Nothing to do.
        self._last_index = index
        x = self.x[index]
        y = self.y[index]
        # update the line positions
        self.horizontal_line.set_ydata(y)
        self.vertical_line.set_xdata(x)
        self.text.set_text('x=%1.2f, y=%1.2f' % (x, y))

        self.ax.figure.canvas.draw()

【问题讨论】:

    标签: python matplotlib plot widget


    【解决方案1】:

    已解决

    所以我尝试了一段时间来解决这个问题,现在用最简单的解决方案做到了......我想?

    在 PushButton 类中添加了一个函数 update_plot(),如下所示

        def update_plot(self):
            print('Testing')
            self.fig.canvas.mpl_connect('button_press_event', self.snap_cursor.on_mouse_click)
    

    并在 def button_pressed 中添加

        self.update_plot()
        plt.show()
        self.timer = QtCore.QTimer()
        self.timer.timeout.connect(self.update_plot)
        self.timer.start(100)
    

    在 def button_pressed 中

    和ofc也用过

    from PyQt5 import QtCore
    

    【讨论】:

      猜你喜欢
      • 2020-03-17
      • 2020-10-03
      • 1970-01-01
      • 2020-03-23
      • 1970-01-01
      • 2016-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多