mplfinance嵌入pyqt,用到QGraphicsView
时间: 2023-10-29 10:01:10 浏览: 304
可以使用mplfinance库生成一个Matplotlib图表,然后将其嵌入到PyQt应用程序中,其中使用QGraphicsView来显示图表。以下是简单的示例代码:
```python
import sys
import matplotlib.pyplot as plt
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from PyQt5.QtWidgets import QApplication, QMainWindow, QGraphicsView, QGraphicsScene
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("mplfinance in PyQt5")
self.setGeometry(100, 100, 800, 600)
# create a QGraphicsView and set it as the central widget
self.view = QGraphicsView(self)
self.setCentralWidget(self.view)
# create a QGraphicsScene and set it to the QGraphicsView
self.scene = QGraphicsScene(self)
self.view.setScene(self.scene)
# create a Matplotlib figure and add a subplot
fig = plt.Figure()
ax = fig.add_subplot(111)
# plot some data using mplfinance
import mplfinance as mpf
import pandas as pd
data = pd.read_csv('data.csv', index_col=0, parse_dates=True)
mpf.plot(data, ax=ax)
# create a Matplotlib canvas and add it to the scene
canvas = FigureCanvas(fig)
self.scene.addWidget(canvas)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
```
在这个示例中,我们创建了一个QMainWindow,并设置一个QGraphicsView作为它的中心窗口。然后,我们创建了一个QGraphicsScene,并将其设置为QGraphicsView。接下来,我们创建了一个Matplotlib图表,并使用mplfinance库将数据绘制到图表中。最后,我们创建了一个Matplotlib canvas对象,并将其添加到QGraphicsScene中。这将使Matplotlib图表嵌入到PyQt应用程序中,并使用QGraphicsView来显示它。
阅读全文