Ask Your Question
2

How to create a plot using mplfinance and PyQt5 that includes a volume graph?

asked 2021-06-06 11:00:00 +0000

lalupa gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2022-04-12 06:00:00 +0000

scrum gravatar image

To create a plot using mplfinance and PyQt5 that includes a volume graph, follow these steps:

  1. Install the necessary libraries:

    • mplfinance: pip install mplfinance
    • PyQt5: pip install PyQt5
  2. 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)
  1. Define a class for the main window:
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()
  1. Define the 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)
  1. Define the 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()
  1. Create an instance of the 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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-06-06 11:00:00 +0000

Seen: 24 times

Last updated: Apr 12 '22