finplot as a widget in layout

Question:

I am trying to add finplot, https://pypi.org/project/finplot/, as a widget to one of the layouts in my UI. I created a widget for finplot and added it to the widgets in the layout but I get the following error:

self.tab1.layout.addWidget(self.tab1.fplt_widget)
TypeError: addWidget(self, QWidget, stretch: int = 0, alignment: Union[Qt.Alignment, Qt.AlignmentFlag] = Qt.Alignment()): argument 1 has unexpected type 'PlotItem'

Here is the code that I used.

import sys
from PyQt5.QtWidgets import *
import finplot as fplt
import yfinance


# Creating the main window
class App(QMainWindow):
    def __init__(self):
        super().__init__()
        self.title = 'PyQt5 - QTabWidget'
        self.left = 0
        self.top = 0
        self.width = 600
        self.height = 400
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)

        self.tab_widget = MyTabWidget(self)
        self.setCentralWidget(self.tab_widget)

        self.show()

    # Creating tab widgets


class MyTabWidget(QWidget):
    def __init__(self, parent):
        super(QWidget, self).__init__(parent)
        self.layout = QVBoxLayout(self)

        # Initialize tab screen
        self.tabs = QTabWidget()
        self.tabs.setTabPosition(QTabWidget.East)
        self.tabs.setMovable(True)

        self.tab1 = QWidget()

        self.tabs.resize(600, 400)

        # Add tabs
        self.tabs.addTab(self.tab1, "tab1")


        self.tab1.layout = QVBoxLayout(self)
        self.tab1.label = QLabel("AAPL")
        self.tab1.df = yfinance.download('AAPL')
        self.tab1.fplt_widget = fplt.create_plot_widget(self.window())
        fplt.candlestick_ochl(self.tab1.df[['Open', 'Close', 'High', 'Low']])
        self.tab1.layout.addWidget(self.tab1.label)
        self.tab1.layout.addWidget(self.tab1.fplt_widget)
        self.tab1.setLayout(self.tab1.layout)

        # Add tabs to widget
        self.layout.addWidget(self.tabs)
        self.setLayout(self.layout)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_())
Asked By: afp_2008

||

Answers:

The create_plot_widget() function creates a PlotItem that cannot be added to a layout, the solution is to use some QWidget that can display the content of the PlotItem as the PlotWidget:

import pyqtgraph as pg

# ...

self.tab1.df = yfinance.download("AAPL")
self.tab1.fplt_widget = pg.PlotWidget(
    plotItem=fplt.create_plot_widget(self.window())
)
fplt.candlestick_ochl(self.tab1.df[["Open", "Close", "High", "Low"]])
Answered By: eyllanesc

To add a widget you use the widget member of the axis you create, like so:

self.tab1.layout.addWidget(self.tab1.fplt_widget.ax_widget)
self.window().axs = [self.tab1.fplt_widget] # required property of window

To get the window to scale the X-axis properly as displaying you call show() here:

self.setCentralWidget(self.tab_widget)
fplt.show(qt_exec=False)
self.show()
Answered By: Jonas Byström

managed to make it work

from PyQt5.QtWidgets import *
import finplot as fplt
import yfinance
import pyqtgraph as pg
import sys

# Creating the main window
class App(QMainWindow):
    def __init__(self):
        super().__init__()
        self.title = 'PyQt5 - QTabWidget'
        self.left = 0
        self.top = 0
        self.width = 600
        self.height = 400
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)

        self.tab_widget = MyTabWidget(self)
        self.setCentralWidget(self.tab_widget)

        # Insert policies
       
        fplt.refresh()
        self.show()

    # Creating tab widgets


class MyTabWidget(QWidget):
    def __init__(self, parent):
        super(QWidget, self).__init__(parent)
        self.layout = QVBoxLayout(self)

        # Initialize tab screen
        self.tabs = QTabWidget()
        self.tabs.setTabPosition(QTabWidget.East)
        self.tabs.setMovable(True)

        self.tab1 = QWidget()

        self.tabs.resize(600, 400)

        # Add tabs
        self.tabs.addTab(self.tab1, "tab1")
        self.tab1.layout = QVBoxLayout(self)
        self.tab1.label = QLabel("AAPL")

        # finplot
        self.tab1.df = yfinance.download("AAPL")
        self.tab1.fplt_widget = pg.PlotWidget(
            plotItem=fplt.create_plot_widget(self.window())
        )
        fplt.candlestick_ochl(self.tab1.df[["Open", "Close", "High", "Low"]])
        self.tab1.layout.addWidget(self.tab1.fplt_widget)
        self.window().axs = [self.tab1.fplt_widget.plotItem]


        self.tab1.setLayout(self.tab1.layout)
        # Add tabs to widget
        self.layout.addWidget(self.tabs)
        self.setLayout(self.layout)



if __name__ == '__main__':
    app = QApplication([])
    ex = App()
    sys.exit(app.exec_())


Answered By: TrashBin2324
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.