1 | initial version |
To create a plot using mplfinance and PyQt5 that includes a volume graph, follow these steps:
Install the necessary libraries:
pip install mplfinance
pip install PyQt5
Import the required modules:
import mplfinance as mpf
from PyQt5 import QtCore, QtWidgets
from matplotlib.backends.backend_qt5agg import (
FigureCanvasQTAgg as FigureCanvas,
NavigationToolbar2QT as NavigationToolbar)
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# initialize the UI
self.initUI()
# define the data and configuration options
self.data = mpf.datasets.load_data()
self.config = {
'type': 'candle',
'volume': True,
'figsize': (10, 6),
'title': 'Stock Prices'
}
# create the plot
self.createPlot()
initUI()
method for setting up the UI: def initUI(self):
# create the main widget
widget = QtWidgets.QWidget()
self.setCentralWidget(widget)
# create the layout
self.layout = QtWidgets.QGridLayout()
widget.setLayout(self.layout)
# create the figure
self.figure = mpf.plot([], **self.config, volume=True, show_nontrading=True)
# create the canvas and toolbar
self.canvas = FigureCanvas(self.figure)
self.toolbar = NavigationToolbar(self.canvas, self)
# add the canvas and toolbar to the layout
self.layout.addWidget(self.canvas, 0, 0, 1, 2)
self.layout.addWidget(self.toolbar, 1, 0, 1, 2)
createPlot()
method for generating the plot: def createPlot(self):
# clear the existing plot
self.figure.clf()
# create the new plot
mpf.plot(self.data, **self.config, volume=True, show_nontrading=True, ax=self.figure.gca())
# redraw the canvas
self.canvas.draw()
MainWindow
class and show the window:if __name__ == '__main__':
app = QtWidgets.QApplication([])
mw = MainWindow()
mw.show()
app.exec_()
The final code should look like this:
import mplfinance as mpf
from PyQt5 import QtCore, QtWidgets
from matplotlib.backends.backend_qt5agg import (
FigureCanvasQTAgg as FigureCanvas,
NavigationToolbar2QT as NavigationToolbar)
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# initialize the UI
self.initUI()
# define the data and configuration options
self.data = mpf.datasets.load_data()
self.config = {
'type': 'candle',
'volume': True,
'figsize': (10, 6),
'title': 'Stock Prices'
}
# create the plot
self.createPlot()
def initUI(self):
# create the main widget
widget = QtWidgets.QWidget()
self.setCentralWidget(widget)
# create the layout
self.layout = QtWidgets.QGridLayout()
widget.setLayout(self.layout)
# create the figure
self.figure = mpf.plot([], **self.config, volume=True, show_nontrading=True)
# create the canvas and toolbar
self.canvas = FigureCanvas(self.figure)
self.toolbar = NavigationToolbar(self.canvas, self)
# add the canvas and toolbar to the layout
self.layout.addWidget(self.canvas, 0, 0, 1, 2)
self.layout.addWidget(self.toolbar, 1, 0, 1, 2)
def createPlot(self):
# clear the existing plot
self.figure.clf()
# create the new plot
mpf.plot(self.data, **self.config, volume=True, show_nontrading=True, ax=self.figure.gca())
# redraw the canvas
self.canvas.draw()
if __name__ == '__main__':
app = QtWidgets.QApplication([])
mw = MainWindow()
mw.show()
app.exec_()
Note: Replace mpf.datasets.load_data()
with your own data source.
This code will create a window containing a candlestick chart along with a volume graph.