Estoy tratando de graficar dos arreglos con matplotlib utilizando PyQt5, el problema de nuevo es que al correr el programa y doy click en el boton graficar se cierra. Declare un widget en Qt Designer para poder mostrar la grafica.
Este es el codigo, primero declaro el widget y le doy la orden de graficar los arreglos x y sx que anteriormente son calculados, no se si tenga que declara alguna libreria extra como QWidget o algo asi para que pueda mostrar la grafica de estos 2 arreglos.
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QMessageBox
from PyQt5 import uic
from PyQt5.QtGui import QIntValidator, QDoubleValidator
from PyQt5.QtCore import QLocale
import math
import matplotlib
#Clase Heredada de QMainWindow (Constructor de ventana)
class Ventana(QMainWindow):
#Metodo constructor de la clase
def __init__(self):
#Iniciar el objeto QMainWindow
QMainWindow.__init__(self)
#Cargar la Configuracion del archivo ui en el objeto
uic.loadUi("Bocinas.ui",self)
self.pushButton.clicked.connect(self.Guardar)
#Convierte los valores en float e int
self._int_validator = QIntValidator()
self._float_validator = QDoubleValidator()
self.lineEdit.setValidator(self._float_validator)
self.lineEdit_2.setValidator(self._int_validator)
self.lineEdit_3.setValidator(self._float_validator)
self.lineEdit_4.setValidator(self._float_validator)
self.lineEdit_5.setValidator(self._float_validator)
#Pone los lineEdit en 0 predeterminado
self.lineEdit.setPlaceholderText("0")
self.lineEdit_2.setPlaceholderText("0")
self.lineEdit_3.setPlaceholderText("0")
self.lineEdit_4.setPlaceholderText("0")
self.lineEdit_5.setPlaceholderText("0")
self._locale = QLocale()
def closeEvent(self,event):
resultado = QMessageBox.question(self,"Salir..","¿Seguro quiere salir?",QMessageBox.Yes | QMessageBox.No)
if resultado == QMessageBox.Yes:
event.accept()
else:
event.ignore()
def Guardar(self):
st, _ = self._locale.toDouble(self.lineEdit.text())
l, _ = self._locale.toInt(self.lineEdit_2.text())
xo, _ = self._locale.toDouble(self.lineEdit_3.text())
m, _ = self._locale.toDouble(self.lineEdit_4.text())
M, _ = self._locale.toDouble(self.lineEdit_5.text())
sx=[]
x=[]
if self.radioButton.isChecked():
ls=l+1
for i in range(ls):
s=st*((i/xo)**2)
sx.append(s)
x.append(i)
elif self.radioButton_2.isChecked():
ls=l+1
for i in range(ls):
s=st*(math.cosh((m*i)/2)**2)
sx.append(s)
x.append(i)
elif self.radioButton_3.isChecked():
ls=l+1
for i in range(ls):
s=st*((math.cosh((m*i)/2))+(M*math.sinh(m*i)/2)**2)
sx.append(s)
x.append(i)
elif self.radioButton_4.isChecked():
ls=l+1
for i in range(ls):
s=st*math.exp(m*i)
sx.append(s)
x.append(i)
else:
QMessageBox.warning(self, 'Advertencia', "Seleccione algun tipo de Bocina",QMessageBox.Retry)
self.widget.canvas.axes.plot(x,sx)
self.widget.canvas.axes.legend("Estilo de Bocina",loc="upper right")
self.widget.canvas.axes.set_title("Bocina")
self.widget.canvas.draw()
#Instancia para iniciar una app
app = QApplication(sys.argv)
#Crear un objeto de la clase
_ventana = Ventana()
#Mostrar ventana
_ventana.show()
#Ejecutar app
app.exec_()
Este es el codigo extraido de Qt designer por si alguna duda extra
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'Bocinas.ui'
#
# Created by: PyQt5 UI code generator 5.14.1
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(560, 140, 191, 51))
self.pushButton.setObjectName("pushButton")
self.layoutWidget = QtWidgets.QWidget(self.centralwidget)
self.layoutWidget.setGeometry(QtCore.QRect(70, 100, 273, 153))
font = QtGui.QFont()
font.setPointSize(14)
self.layoutWidget.setFont(font)
self.layoutWidget.setObjectName("layoutWidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.layoutWidget)
self.verticalLayout.setContentsMargins(3, 1, 0, 10)
self.verticalLayout.setSpacing(4)
self.verticalLayout.setObjectName("verticalLayout")
self.label = QtWidgets.QLabel(self.layoutWidget)
font = QtGui.QFont()
font.setPointSize(14)
self.label.setFont(font)
self.label.setObjectName("label")
self.verticalLayout.addWidget(self.label)
self.label_2 = QtWidgets.QLabel(self.layoutWidget)
font = QtGui.QFont()
font.setPointSize(14)
self.label_2.setFont(font)
self.label_2.setObjectName("label_2")
self.verticalLayout.addWidget(self.label_2)
self.label_3 = QtWidgets.QLabel(self.layoutWidget)
font = QtGui.QFont()
font.setPointSize(14)
self.label_3.setFont(font)
self.label_3.setObjectName("label_3")
self.verticalLayout.addWidget(self.label_3)
self.label_4 = QtWidgets.QLabel(self.layoutWidget)
font = QtGui.QFont()
font.setPointSize(14)
self.label_4.setFont(font)
self.label_4.setObjectName("label_4")
self.verticalLayout.addWidget(self.label_4)
self.label_5 = QtWidgets.QLabel(self.layoutWidget)
font = QtGui.QFont()
font.setPointSize(14)
self.label_5.setFont(font)
self.label_5.setObjectName("label_5")
self.verticalLayout.addWidget(self.label_5)
self.radioButton = QtWidgets.QRadioButton(self.centralwidget)
self.radioButton.setGeometry(QtCore.QRect(410, 340, 171, 21))
self.radioButton.setObjectName("radioButton")
self.radioButton_2 = QtWidgets.QRadioButton(self.centralwidget)
self.radioButton_2.setGeometry(QtCore.QRect(410, 370, 171, 21))
self.radioButton_2.setObjectName("radioButton_2")
self.radioButton_3 = QtWidgets.QRadioButton(self.centralwidget)
self.radioButton_3.setGeometry(QtCore.QRect(410, 400, 171, 21))
self.radioButton_3.setObjectName("radioButton_3")
self.radioButton_4 = QtWidgets.QRadioButton(self.centralwidget)
self.radioButton_4.setGeometry(QtCore.QRect(410, 430, 171, 21))
self.radioButton_4.setObjectName("radioButton_4")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(380, 100, 161, 21))
self.lineEdit.setObjectName("lineEdit")
self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_2.setGeometry(QtCore.QRect(380, 130, 161, 21))
self.lineEdit_2.setObjectName("lineEdit_2")
self.lineEdit_3 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_3.setGeometry(QtCore.QRect(380, 160, 161, 21))
self.lineEdit_3.setObjectName("lineEdit_3")
self.lineEdit_4 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_4.setGeometry(QtCore.QRect(380, 190, 161, 21))
self.lineEdit_4.setObjectName("lineEdit_4")
self.lineEdit_5 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_5.setGeometry(QtCore.QRect(380, 220, 161, 21))
self.lineEdit_5.setObjectName("lineEdit_5")
self.widget = QtWidgets.QWidget(self.centralwidget)
self.widget.setGeometry(QtCore.QRect(70, 280, 311, 231))
self.widget.setObjectName("widget")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 22))
self.menubar.setObjectName("menubar")
self.menuArchivo = QtWidgets.QMenu(self.menubar)
self.menuArchivo.setObjectName("menuArchivo")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.actionAbrir = QtWidgets.QAction(MainWindow)
self.actionAbrir.setObjectName("actionAbrir")
self.actionGuardar = QtWidgets.QAction(MainWindow)
self.actionGuardar.setObjectName("actionGuardar")
self.menuArchivo.addAction(self.actionAbrir)
self.menuArchivo.addAction(self.actionGuardar)
self.menubar.addAction(self.menuArchivo.menuAction())
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "Graficar"))
self.label.setText(_translate("MainWindow", "Área de garganta"))
self.label_2.setText(_translate("MainWindow", "Longitud"))
self.label_3.setText(_translate("MainWindow", "Distancia entre origen y garganta"))
self.label_4.setText(_translate("MainWindow", "Ensanchamiento"))
self.label_5.setText(_translate("MainWindow", "Factor de forma"))
self.radioButton.setText(_translate("MainWindow", "Conica"))
self.radioButton_2.setText(_translate("MainWindow", "Catenoidal"))
self.radioButton_3.setText(_translate("MainWindow", "Hiperbolica"))
self.radioButton_4.setText(_translate("MainWindow", "Exponencial"))
self.menuArchivo.setTitle(_translate("MainWindow", "Archivo"))
self.actionAbrir.setText(_translate("MainWindow", "Abrir"))
self.actionGuardar.setText(_translate("MainWindow", "Guardar"))