I'm currently trying to plot chart/diagram in python with Matplotlib and PyQt5 with open file dialog. The matplotlib script works separately aside from pyqt5 without open file dialog with filename (only works when calling specific file directly). The PyQt script and ui also work yet separately from matplotlib. It has problem when trying to triggered plot toolbar into showing plot using pyqt5, and the data successfully loaded yet seems not read. Every time I try to call the plot function it won't worked but closed with error with "Process finished with exit code -1073741819 (0xC0000005)". It seems I have issues with Open File Dialog and calling Plot function. Thank you in advance, I appreciate any help for this matter. (I have researched before from other questions and scripts, and tried several suggestions and solutions but still not working so I came here, pardon i'm still a beginner).
Here is my code:
import csv
import matplotlib
import sys
from os.path import dirname, realpath, join
from PyQt5 import QtCore,QtWidgets,QtGui
from PyQt5.QtWidgets import QApplication,QMenu,QMenuBar,QPushButton,QVBoxLayout,QWidget,QMainWindow,QAction,QFileDialog,QDialog,QSpinBox,QToolBar,QTableView
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axis3d ,axes3d
from PyQt5.QtGui import QIcon
from PyQt5.uic import loadUiType
from PyQt5.QtCore import Qt
from matplotlib.backends.backend_qt5agg import FigureCanvas as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as Navi
from matplotlib.figure import Figure
from sys import argv
import pandas as pd
scriptDir = dirname(realpath(__file__))
FROM_MAIN, _ = loadUiType(join(dirname(__file__), "test.ui"))
class Main(QMainWindow, FROM_MAIN):
def __init__(self, parent = FROM_MAIN):
super(Main, self).__init__()
QMainWindow.__init__(self)
self.ax = plt.axes(projection='3d')
self.setupUi(self)
self.ToolBar()
self.l = QVBoxLayout(self.frame)
self.sc = Canvas()
def getFile(self):
global filename
filename=QFileDialog.getOpenFileName(self,"Open","","CSV (*.csv);;All Files(*)")
def ToolBar(self):
menubar = self.menuBar()
# Menubar horizontal
fileMenu = menubar.addMenu('File')
editMenu = menubar.addMenu('Edit')
plotMenu = menubar.addMenu('Plot')
helpMenu = menubar.addMenu('Help')
aboutMenu = menubar.addMenu('About')
fileToolBar = self.addToolBar('File')
editToolBar = self.addToolBar('Edit')
plotToolBar = self.addToolBar('Plot')
self.addToolBar(Qt.LeftToolBarArea, plotToolBar)
dataToolBar = self.addToolBar ('Show Data')
self.addToolBar(Qt.LeftToolBarArea, dataToolBar)
# File Menu
self.newAction = QAction("&New", self)
self.newAction.setIcon(QIcon('icons/new.png'))
self.openAction = QAction("&Open", self)
self.openAction.setIcon(QIcon('icons/open.png'))
self.editAction = QAction("&Edit", self)
self.editAction.setIcon(QIcon('icons/edit.png'))
self.saveAction = QAction("&Save", self)
self.saveAction.setIcon(QIcon('icons/save.png'))
self.saveasAction = QAction("&Save As", self)
self.saveasAction.setIcon(QIcon('icons/saveas.png'))
self.exitAction = QAction("&Exit", self)
self.exitAction.setIcon(QIcon('icons/exit.png'))
fileMenu.addAction(self.newAction)
fileMenu.addAction(self.openAction)
fileMenu.addAction(self.editAction)
fileMenu.addAction(self.saveAction)
fileMenu.addAction(self.saveasAction)
fileMenu.addAction(self.exitAction)
fileToolBar.addAction(self.newAction)
fileToolBar.addAction(self.openAction)
fileToolBar.addAction(self.editAction)
fileToolBar.addAction(self.saveAction)
fileToolBar.addAction(self.saveasAction)
fileToolBar.addAction(self.exitAction)
self.openAction.triggered.connect(self.OpenFile)
# Edit Menu
self.copyAction = QAction("&Copy", self)
self.copyAction.setIcon(QIcon('icons/copy.png'))
self.pasteAction = QAction("&Paste", self)
self.pasteAction.setIcon(QIcon('icons/paste.png'))
self.cutAction = QAction("&Cut", self)
self.cutAction.setIcon(QIcon('icons/cut.png'))
self.dataAction = QAction("Show Data", self)
self.dataAction.setIcon(QIcon('icons/push.png'))
editMenu.addAction(self.copyAction)
editMenu.addAction(self.pasteAction)
editToolBar.addAction(self.copyAction)
editToolBar.addAction(self.pasteAction)
editToolBar.addAction(self.cutAction)
dataToolBar.addAction(self.dataAction)
# Find and Replace submenu in the Edit menu
findMenu = editMenu.addMenu("Find and Replace")
self.findAction = QAction ("Find", self)
self.replaceAction = QAction ("Replace", self)
findMenu.addAction(self.findAction)
findMenu.addAction(self.replaceAction)
# Plot Menu
self.plot2Action = QAction("&2D Plot", self)
self.plot2Action.setIcon(QIcon('icons/plot2d.png'))
self.plot3Action = QAction("&3D Plot", self)
self.plot3Action.setIcon(QIcon('icons/plot3d.png'))
self.zoominAction = QAction("Zoom In Plot", self)
self.zoominAction.setIcon(QIcon('icons/zoomin.png'))
self.zoomoutAction = QAction("Zoom Out Plot", self)
self.zoomoutAction.setIcon(QIcon('icons/zoomout.png'))
plotMenu.addAction(self.plot2Action)
plotMenu.addAction(self.plot3Action)
plotToolBar.addAction(self.plot2Action)
plotToolBar.addAction(self.plot3Action)
plotToolBar.addAction(self.zoominAction)
plotToolBar.addAction(self.zoomoutAction)
self.plot2Action.triggered.connect(self.Plot2D)
self.plot3Action.triggered.connect(self.Plot3D)
# Help Menu
self.helpContentAction = QAction("&Help Content", self)
helpMenu.addAction(self.helpContentAction)
# About Menu
self.aboutAction = QAction("&About", self)
aboutMenu.addAction(self.aboutAction)
def OpenFile(self):
global filename
filename, _ = QFileDialog.getOpenFileName(self, "Open", "", "CSV Files (*.csv);;All Files (*)")
def readData(self):
x = []
y = []
z = []
with open(filename) as f:
plots = csv.reader(csvfile, delimiter=',')
for row in plots:
x.append(float(row[0]))
y.append(float(row[1]))
z.append(float(row[2]))
self.ax = plt.axes(projection='3d')
self.ax.scatter(x, y, z)
self.ax.plot_trisurf(x, y, z)
self.ax.set(xlabel='X', ylabel='Y',
zlabel='Z', title='test 3D dulu')
self.ax.grid()
self.show()
def Plot2D(self):
x, y = self.readData(filename)
self.sc.axes.plot2(x, y)
def Plot3D(self):
x, y, z = self.readData(filename)
self.sc.axes.plot3(x, y, z)
class Canvas(FigureCanvas):
def __init__(self, parent=None, dpi=120):
fig, self.ax = plt.subplots(figsize=(5, 4), dpi=200)
super().__init__(fig)
self.setParent(parent)
def plot2(self, x, y):
self.fig.clear()
self.ax = fig.add_subplot(111)
self.ax.scatter(x, y, color='red')
self.ax.plot(x, y, color='yellow', edgecolor='black', linewidth=0.1, shade=2)
self.ax.set(xlabel='X', ylabel='Y', title='2D')
self.ax.grid()
self.draw()
def plot3(self, x, y, z):
self.fig.clear()
self.ax = plt.axes(projection='3d')
self.ax.scatter(x, y, z)
self.ax.plot_trisurf(x, y, z)
self.ax.set(xlabel='X', ylabel='Y',
zlabel='Z', title='test 3D dulu')
self.ax.grid()
self.draw()
def main():
app = QApplication(sys.argv)
window = Main()
window.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
This is the 'test.ui' file:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QFrame" name="frame">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>791</width>
<height>561</height>
</rect>
</property>
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>21</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
<action name="actionOpen">
<property name="text">
<string>Open</string>
</property>
</action>
</widget>
<resources/>
<connections/>
</ui>
User contributions licensed under CC BY-SA 3.0