I want to bring in a text file through PyQt5 and draw a graph with data values.
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QTextEdit, QAction, QFileDialog
from PyQt5.QtGui import QIcon
import numpy as np
import matplotlib.pyplot as plt
class MyApp(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.textEdit = QTextEdit()
self.setCentralWidget(self.textEdit)
self.statusBar()
openFile = QAction(QIcon('folder.png'), 'Open', self)
openFile.setShortcut('Ctrl+O')
openFile.setStatusTip('Text.txt')
openFile.triggered.connect(self.show)
menubar = self.menuBar()
menubar.setNativeMenuBar(False)
fileMenu = menubar.addMenu('&File')
fileMenu.addAction(openFile)
self.setWindowTitle('File Dialog')
self.setGeometry(300, 300, 300, 200)
self.show()
def show(self):
fname = QFileDialog.getOpenFileName(self, 'Open file', './')
if fname[0]:
f = open(fname[0], 'r')
with f:
data = f.read()
data2=np.array(data)
x=data2[1:,0]
y=data2[1:,1]
plt.plot(x,y)
plt.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = MyApp()
sys.exit(app.exec_())
Text file photo.
This is the error that appears:
x=data2[1:,0]
IndexError: too many indices for array: array is 0-dimensional, but 2 were indexed