1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
| from PyQt5.QtWidgets import QMainWindow, QApplication, QPushButton, QLineEdit
from PyQt5 import QtCore, QtGui
import sys
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
import numpy as np
import pandas as pd
class Window(QMainWindow):
def __init__(self):
super().__init__()
title = "Find or zoom to point"
top = 400
left = 400
width = 900
height = 550
self.setWindowTitle(title)
self.setGeometry(top, left, width, height)
self.MyUI()
def MyUI(self):
self.canvas = Canvas(self, width=8, height=4)
self.canvas.move(0, 0)
line_edit1 = QLineEdit("entre x", self)
line_edit1.move(100, 430)
line_edit2 = QLineEdit("entre y", self)
line_edit2.move(100, 470)
self.line_edit3 = QLineEdit("entre point name", self) # Ajout/Modif
self.line_edit3.move(380, 450) # Ajout/Modif
button1 = QPushButton("Find(zoom) by coordinate", self)
button1.setGeometry(QtCore.QRect(200, 430, 135, 70))
button2 = QPushButton("Find(zoom) by point name", self)
button2.setGeometry(QtCore.QRect(480, 450, 135, 30))
button2.clicked.connect(self.zoom_lettre) # Ajout/Modif
def zoom_lettre(self):
lettre = self.line_edit3.text().upper()
if lettre not in self.canvas.data["point_name"]:
print("ce point n'existe pas !!!")
return
indice_lettre = self.canvas.data["point_name"].index(lettre)
x_lettre = self.canvas.data["x"][indice_lettre]
y_lettre = self.canvas.data["y"][indice_lettre]
print(x_lettre, y_lettre)
self.canvas.ax.set_xlim(x_lettre-1, x_lettre+1)
self.canvas.ax.set_ylim(y_lettre-1, y_lettre+1)
self.canvas.draw()
class Canvas(FigureCanvas):
def __init__(self, parent=None, width=5, height=5, dpi=100):
fig = Figure(figsize=(width, height), dpi=dpi)
self.axes = fig.add_subplot(111)
FigureCanvas.__init__(self, fig)
self.setParent(parent)
self.plot()
def plot(self):
self.data = {'point_name': ["A", "B", "C", "D", "E"], 'x': [
12, 18, 25, 42, 9], 'y': [9, 15, 2, 23, 5]}
df = pd.DataFrame(self.data)
self.ax = self.figure.add_subplot(111)
self.ax.scatter(df['x'], df['y'])
labels = list(df.point_name)
xlabel = list(df.x)
ylabel = list(df.y)
for i, txt in enumerate(labels):
self.ax.annotate(txt, (xlabel[i], ylabel[i]))
app = QApplication(sys.argv)
window = Window()
window.show()
app.exec_() |
Partager