Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. adding as one of multiple widgets in a PyQt6 Window

adding as one of multiple widgets in a PyQt6 Window

Scheduled Pinned Locked Moved Solved Qt for Python
9 Posts 3 Posters 924 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    EarlyBird
    wrote on 20 Oct 2024, 20:17 last edited by
    #1

    When I add 2 plots as widgets from Pandas DataFrame's into one layout, it shows 2 plots with its unique toolbar. Works fine, thats why I do not show this code.

    When I try to combine 1 Pandas DataFrame plot with one mplfinance as 2 widgets into one layout, it shows at first one window with the mplfinance content and after closing the window a second unique window appears and shows the 1 Pandas DataFrame plot.

    How do I place a mplfinance plot with other non mplfinance tables, plots or other in one window?
    In this example I want to stack the 2 windows vertically.

    My code:

    import sys, os
    from PyQt6.QtWidgets import (QApplication, QWidget, QMainWindow, QVBoxLayout)
            #QLineEdit, QPushButton, QLabel,
            #QMessageBox, QGraphicsView, QGraphicsScene,
            #QFontComboBox)
    #from PyQt6.QtGui import QMovie, QFont, QFontInfo, QPixmap
    #from PyQt6.QtCore import QTimer
    #from PyQt6 import uic
    import matplotlib
    import pandas as pd
    from matplotlib.backends.backend_qtagg import (FigureCanvasQTAgg,
            NavigationToolbar2QT as NavigationToolbar)
    from matplotlib.figure import Figure
    import matplotlib.pyplot as plt
    #from inspect import getmembers, isfunction
    #import random
    import yfinance as yf
    import mplfinance as mpf
    
    class MplCanvas(FigureCanvasQTAgg):
        def __init__(self, parent=None, width=5, height=4, dpi=100):
            fig=Figure(figsize=(width, height), dpi=dpi)
            self.axes = fig.add_subplot(111)
            super().__init__(fig)
    
    class MainWindow(QMainWindow):
        def __init__(self, *args, **kwargs):
            super().__init__(*args, **kwargs)
            sc1 = MplCanvas(self, width=5, height=4, dpi=100)
            df = pd.DataFrame([
                [0,10,2.876],
                [5,15,6],
                [2,20,5],
                [15,25,25],
                [4,10,0],
                ],
                columns=["A","B","C"])
            df.plot(ax=sc1.axes)
            sc2 = MplCanvas(self, width=5, height=4, dpi=100)
            ticker_symbol = 'AAPL'
            header = ticker_symbol + ' Candlestick Chart'
            s_d = yf.download(ticker_symbol,start='2023-09-01',end='2024-10-01')
            toolbar = NavigationToolbar(sc1, self)
            toolbar2 = NavigationToolbar(sc2, self)
            layout = QVBoxLayout()
            layout.addWidget(toolbar)
            layout.addWidget(sc1)
            layout.addWidget(toolbar2)
            layout.addWidget(mpf.plot(s_d, type='candle', style='charles', title=header))
            widget = QWidget()
            widget.setLayout(layout)
            self.setCentralWidget(widget)
    
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        window = MainWindow()
        window.show()
        sys.exit(app.exec())
    
    

    Regards
    Sven

    J 1 Reply Last reply 21 Oct 2024, 07:49
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 25 Oct 2024, 18:42 last edited by
      #8

      Thanks for the feedback and example !

      One thing: you should cleanup your super call, since Python 3, it does not need parameters anymore.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • E EarlyBird
        20 Oct 2024, 20:17

        When I add 2 plots as widgets from Pandas DataFrame's into one layout, it shows 2 plots with its unique toolbar. Works fine, thats why I do not show this code.

        When I try to combine 1 Pandas DataFrame plot with one mplfinance as 2 widgets into one layout, it shows at first one window with the mplfinance content and after closing the window a second unique window appears and shows the 1 Pandas DataFrame plot.

        How do I place a mplfinance plot with other non mplfinance tables, plots or other in one window?
        In this example I want to stack the 2 windows vertically.

        My code:

        import sys, os
        from PyQt6.QtWidgets import (QApplication, QWidget, QMainWindow, QVBoxLayout)
                #QLineEdit, QPushButton, QLabel,
                #QMessageBox, QGraphicsView, QGraphicsScene,
                #QFontComboBox)
        #from PyQt6.QtGui import QMovie, QFont, QFontInfo, QPixmap
        #from PyQt6.QtCore import QTimer
        #from PyQt6 import uic
        import matplotlib
        import pandas as pd
        from matplotlib.backends.backend_qtagg import (FigureCanvasQTAgg,
                NavigationToolbar2QT as NavigationToolbar)
        from matplotlib.figure import Figure
        import matplotlib.pyplot as plt
        #from inspect import getmembers, isfunction
        #import random
        import yfinance as yf
        import mplfinance as mpf
        
        class MplCanvas(FigureCanvasQTAgg):
            def __init__(self, parent=None, width=5, height=4, dpi=100):
                fig=Figure(figsize=(width, height), dpi=dpi)
                self.axes = fig.add_subplot(111)
                super().__init__(fig)
        
        class MainWindow(QMainWindow):
            def __init__(self, *args, **kwargs):
                super().__init__(*args, **kwargs)
                sc1 = MplCanvas(self, width=5, height=4, dpi=100)
                df = pd.DataFrame([
                    [0,10,2.876],
                    [5,15,6],
                    [2,20,5],
                    [15,25,25],
                    [4,10,0],
                    ],
                    columns=["A","B","C"])
                df.plot(ax=sc1.axes)
                sc2 = MplCanvas(self, width=5, height=4, dpi=100)
                ticker_symbol = 'AAPL'
                header = ticker_symbol + ' Candlestick Chart'
                s_d = yf.download(ticker_symbol,start='2023-09-01',end='2024-10-01')
                toolbar = NavigationToolbar(sc1, self)
                toolbar2 = NavigationToolbar(sc2, self)
                layout = QVBoxLayout()
                layout.addWidget(toolbar)
                layout.addWidget(sc1)
                layout.addWidget(toolbar2)
                layout.addWidget(mpf.plot(s_d, type='candle', style='charles', title=header))
                widget = QWidget()
                widget.setLayout(layout)
                self.setCentralWidget(widget)
        
        if __name__ == '__main__':
            app = QApplication(sys.argv)
            window = MainWindow()
            window.show()
            sys.exit(app.exec())
        
        

        Regards
        Sven

        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 21 Oct 2024, 07:49 last edited by
        #2

        @EarlyBird said in adding as one of multiple widgets in a PyQt6 Window:

        mpf.plot(s_d, type='candle', style='charles', title=header)

        What does this return? I guess it is not a QWidget.
        Does mplfinance have anything to do with Qt at all?

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        2
        • E Offline
          E Offline
          EarlyBird
          wrote on 21 Oct 2024, 16:39 last edited by EarlyBird
          #3

          Hi jsulm,

          mpf.plot(... does not return anything. My question is how do I connect mplfinance with Qt using PyQt6.
          Running mplfinance in notebooks is ok until you want a standalone app.

          Qt itself has nothing to do with mplfinance. Qt is graphical framework and mplfinance creates a content I would like to use in Qt.

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 21 Oct 2024, 18:04 last edited by
            #4

            Hi,

            mplfinance sounds like matplotlib and it has a backend for Qt.

            Check this tutorial to integrate matplotlib with PyQt5. It should likely translate to what you are doing now.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            2
            • E Offline
              E Offline
              EarlyBird
              wrote on 21 Oct 2024, 18:20 last edited by
              #5

              Hi SGaist,
              did that, it nests matplotlib items with the QVBoxLayout:
              Bildschirmfoto 2024-10-21 um 20.16.14.png

              There is also a totarial on this website that does that with PyQt6.

              But with mplfinance, I did not find a way to connect the plot to a layout. This only with mplfinance.
              Any gues or advice?

              1 Reply Last reply
              0
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 21 Oct 2024, 18:57 last edited by
                #6

                This stack overflow thread might give you a starting point even though it's about 3D volumes, there's an example that uses mplfinance.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                1
                • E Offline
                  E Offline
                  EarlyBird
                  wrote on 25 Oct 2024, 18:32 last edited by
                  #7

                  @SGaist, @jsulm its fixed. Thanks for your help and the link, that gave the hint where to look. Daniel from mplfinance gave me another tip. You first put every plot into a figure, preventing mpf.plot to send it to the window and add the figures into any QxLayout with layout.addWidget(figure), call xxx.show().
                  This is the working code to display:

                  #pyqt6 - PyQt6_V2.0_Designer_08.10.2024.py
                  import sys, os
                  from PyQt6.QtWidgets import (QApplication, QWidget, QMainWindow, QVBoxLayout)
                          #QLineEdit, QPushButton, QLabel,
                          #QMessageBox, QGraphicsView, QGraphicsScene,
                          #QFontComboBox)
                  #from PyQt6.QtGui import QMovie, QFont, QFontInfo, QPixmap
                  #from PyQt6.QtCore import QTimer
                  #from PyQt6 import uic
                  import matplotlib
                  matplotlib.use('QtAgg')
                  import pandas as pd
                  from matplotlib.backends.backend_qtagg import (FigureCanvasQTAgg,
                          NavigationToolbar2QT as NavigationToolbar)
                  from matplotlib.figure import Figure
                  import matplotlib.pyplot as plt
                  #from inspect import getmembers, isfunction
                  #import random
                  import yfinance as yf
                  import mplfinance as mpf
                  
                  class MplCanvas(FigureCanvasQTAgg):
                      def __init__(self, fig):
                          super(MplCanvas,self).__init__(fig)
                  
                  class MainWindow(QMainWindow):
                      def __init__(self, *args, **kwargs):
                          super().__init__(*args, **kwargs)
                  
                          #Pandas data
                          df = pd.DataFrame([
                              [0,10,2.876],
                              [5,15,6],
                              [2,20,5],
                              [15,25,25],
                              [4,10,0],
                              ],
                              columns=["A","B","C"])
                  
                          #Pandas plot
                          fig1, ax = plt.subplots()
                          ax.plot(df)
                          sc1 = MplCanvas(fig1)
                  
                          # finance data
                          ticker_symbol = 'AAPL'
                          header = ticker_symbol + ' Candlestick Chart'
                          s_d = yf.download(ticker_symbol,start='2023-09-01',end='2024-10-01')
                          fig2, axes = mpf.plot(s_d, type='candle', style='charles', title=header,returnfig=True)
                          sc2 = MplCanvas(fig2)
                  
                          #PyQt6 setup
                          toolbar = NavigationToolbar(sc1, self)
                          toolbar2 = NavigationToolbar(sc2, self)
                          layout = QVBoxLayout()
                          layout.addWidget(toolbar2)
                          layout.addWidget(sc2)
                          layout.addWidget(toolbar)
                          layout.addWidget(sc1)
                          widget = QWidget()
                          widget.setLayout(layout)
                          self.setCentralWidget(widget)
                          self.show()
                  
                  if __name__ == '__main__':
                      app = QApplication(sys.argv)
                      window = MainWindow()
                      window.show()
                      sys.exit(app.exec())
                  
                  
                  1 Reply Last reply
                  1
                  • S Offline
                    S Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on 25 Oct 2024, 18:42 last edited by
                    #8

                    Thanks for the feedback and example !

                    One thing: you should cleanup your super call, since Python 3, it does not need parameters anymore.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    2
                    • E EarlyBird has marked this topic as solved on 4 Nov 2024, 17:12
                    • E Offline
                      E Offline
                      EarlyBird
                      wrote on 4 Nov 2024, 17:13 last edited by
                      #9

                      Thanks @SGaist, did that.

                      1 Reply Last reply
                      0

                      1/9

                      20 Oct 2024, 20:17

                      • Login

                      • Login or register to search.
                      1 out of 9
                      • First post
                        1/9
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved