adding as one of multiple widgets in a PyQt6 Window
-
wrote on 20 Oct 2024, 20:17 last edited by
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 -
Thanks for the feedback and example !
One thing: you should cleanup your super call, since Python 3, it does not need parameters anymore.
-
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@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? -
wrote on 21 Oct 2024, 16:39 last edited by EarlyBird
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.
-
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.
-
wrote on 21 Oct 2024, 18:20 last edited by
-
This stack overflow thread might give you a starting point even though it's about 3D volumes, there's an example that uses mplfinance.
-
wrote on 25 Oct 2024, 18:32 last edited by
@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())
-
Thanks for the feedback and example !
One thing: you should cleanup your super call, since Python 3, it does not need parameters anymore.
-
1/9