#!/usr/bin/env python # coding: utf-8 # In[ ]: # In[1]: from PyQt5.QtGui import * from PyQt5.QtCore import * from PyQt5.QtWidgets import * import matplotlib.pyplot as plt from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas from matplotlib.figure import Figure import numpy as np from Design_Form import Ui_Form import pandas as pd import sys class main(QWidget,Ui_Form): def __init__(self): QWidget.__init__(self) self.setupUi(self) self.MyUI() width = 99 height = 99 def MyUI(self): canvas = Canvas(self, width=7, height=5) canvas.move(355,9) button = QPushButton("Click Me", self) button.move(100, 450) button2 = QPushButton("Click Me Two", self) button2.move(250, 450) class Canvas(FigureCanvas): def __init__(self, parent = None, width = 3, height = 3, dpi = 100): global fig fig = Figure(figsize=(width, height), dpi=dpi) self.axes = fig.add_subplot(111) FigureCanvas.__init__(self, fig) setParent(parent) self.plot() def plot(self): x = np.array([13,17,50,30,40,12,21,28]) y = [1,3,7,9,12,14,6,2] ax = self.figure.add_subplot(111) ax.scatter(x,y) def mouse_click(self, event): print('click', event) if not event.inaxes: return #left click if event.button == 1: self.xs.append(event.xdata) self.ys.append(event.ydata) #add a line to plot if it has 2 points if len(self.xs) % 2 == 0: line, = self.ax.plot([self.xs[-2], self.xs[-1]], [self.ys[-2], self.ys[-1]], 'r') line.figure.canvas.draw() #right click if event.button == 3: if len(self.xs) > 0: self.xs.pop() self.ys.pop() #delete last line drawn if the line is missing a point, #never delete the original stock plot if len(self.xs) % 2 == 1 and len(self.ax.lines) > 1: self.ax.lines.pop() #refresh plot self.fig.canvas.draw() def mouse_move(self, event): if not event.inaxes: return #dtaw a temporary line from a single point to the mouse position #delete the temporary line when mouse move to another position if len(self.xs) % 2 == 1: line, =self.ax.plot([self.xs[-1], event.xdata], [self.ys[-1], event.ydata], 'r') line.figure.canvas.draw() self.ax.lines.pop() draw_line = Canvas(fig, ax) fig.canvas.mpl_connect('button_press_event', draw_line.mouse_click) fig.canvas.mpl_connect('motion_notify_event', draw_line.mouse_move) plt.show() if __name__ == "__main__": import sys app = QApplication(sys.argv) Form = main() Form.show() sys.exit(app.exec_())