运行之后报错:index -1 is out of bounds for axis 0 with size 0。请问这该怎么解决?
import pandas as pd
import mplfinance as mpf
import matplotlib.pyplot as plt
import numpy as np
import sys
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg
from PyQt5.QtWidgets import QDialog,QApplication,QLabel, QLineEdit,QMainWindow,QWidget, QVBoxLayout, QPushButton, QFileDialog, QTableWidget, QTableWidgetItem
from matplotlib.figure import Figure
class ExcelAnalyzer(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
# 设置窗口标题和初始大小
self.setWindowTitle('Excel数据分析器')
self.setGeometry(100, 100, 800, 600)
# 创建垂直布局
layout = QVBoxLayout()
# 创建按钮,用于打开文件选择对话框
self.loadButton = QPushButton('加载Excel文件', self)
self.loadButton.clicked.connect(self.loadExcelFile)
layout.addWidget(self.loadButton)
# 设置布局
self.setLayout(layout)
# 打开文件选择对话框
def loadExcelFile(self):
filename, _ = QFileDialog.getOpenFileName(self, "选择Excel文件", "", "Excel Files (*.xlsx *.xls);;All Files (*)")
if filename:
self.loadExcelData(filename)
#读取表格
def loadExcelData(self, filename):
df = pd.read_excel(filename)
self.search_window = SearchScreen(self,df)
self.hide()
self.search_window.show()
class SearchScreen(QMainWindow):
def __init__(self, parent,df):
super().__init__(parent)
self.setWindowTitle("股票查询")
self.setGeometry(100, 100, 400, 300)
self.df = df
#搜索框
self.code_input = QLineEdit(self)
self.code_input.setPlaceholderText("输入代码(1001至1100)")
#搜索按钮
self.search_btn = QPushButton("搜索", self)
self.search_btn.clicked.connect(self.on_search_clicked)
#返回按钮
self.back_btn = QPushButton("返回", self)
self.back_btn.clicked.connect(self.on_back_clicked)
layout = QVBoxLayout()
layout.addWidget(self.code_input)
layout.addWidget(self.search_btn)
layout.addWidget(self.back_btn)
central_widget = QWidget()
central_widget.setLayout(layout)
self.setCentralWidget(central_widget)
def on_search_clicked(self):
code = self.code_input.text()
if code in [f'{number}' for number in range(1001,1101)]:
self.details_window = show_details(self,code,self.df)
self.details_window.show()
else:
self.currentPage = QLabel("无效的代码")
def on_back_clicked(self):
self.parent().show()
self.hide()
#详细信息页面
class show_details(QMainWindow):
def __init__(self, parent, code,df):
super().__init__(parent)
self.code = code
self.df = df
self.dfi = self.individual()
self.setWindowTitle("详细信息")
self.setGeometry(100, 100, 400, 300)
#返回按钮
self.back_btn = QPushButton("返回", self)
self.back_btn.clicked.connect(self.on_back_clicked)
#折线图按钮
self.price_btn = QPushButton('价格折线图',self)
self.price_btn.clicked.connect(self.show_price)
def individual(self):
dfi = self.df[self.df['股票代码'] == self.code]
dfi.set_index(['日期'],inplace = True)
dfi.index.name = 'Date'
dfi.columns = ['Number','Open','Close','High','Low','Volume','Limit','Amplitude','Turnover']
return dfi
def on_back_clicked(self):
self.parent().show()
self.hide()
def show_price(self):
dialog = priceDialog(self.dfi, self)
dialog.exec_()
class priceDialog(QDialog):
def __init__(self, dfi, parent=None):
super().__init__(parent)
self.dfi = dfi
self.fig= Figure()
self.initUI()
def initUI(self):
self.setWindowTitle('价格折线图')
self.setGeometry(300, 300, 600, 400)
# 创建一个matplotlib图表
self.fig = mpf.plot(self.dfi,
type = 'line',
style = 'charles')
# 创建一个垂直布局并添加图表
self.canvas = FigureCanvasQTAgg(self.fig)
layout = QVBoxLayout()
layout.addWidget(self.canvas) # 确保添加 canvas 到布局
# 设置布局
self.setLayout(layout)
# 创建实例
app = QApplication(sys.argv)
start_window = ExcelAnalyzer()
start_window.show()
sys.exit(app.exec())