Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PYQT charts in tabs
#11
import sys
from PyQt5.QtWidgets import QMainWindow, QApplication, QPushButton, QWidget, QAction, QTabWidget,QVBoxLayout
from PyQt5.QtGui import QIcon, QPainter
from PyQt5.QtCore import pyqtSlot, Qt, QPointF
from PyQt5.QtWidgets import QLabel
from PyQt5.QtChart import QBarSet, QChart, QBarSeries, QLineSeries, QBarCategoryAxis, QChartView

from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg, NavigationToolbar2QT as NavigationToolbar
from matplotlib.figure import Figure


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(MplCanvas, self).__init__(fig)
  
class App(QMainWindow):
  
    def __init__(self):
        super().__init__()
        self.title = 'CHART'
        self.left = 0
        self.top = 0
        self.width = 800
        self.height = 600
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)
        self.table_widget = MyTableWidget(self)
        self.setCentralWidget(self.table_widget)
        self.show()
     
class MyTableWidget(QWidget):
  
    def __init__(self, parent):
        super(QWidget, self).__init__(parent)
        self.layout = QVBoxLayout(self)
          
        # Initialize tab screen
        self.tabs = QTabWidget()
        self.tab1 = QWidget()
        self.tab2 = QWidget()
        self.tabs.resize(300,200)
          
        # Add tabs
        self.tabs.addTab(self.tab1,"Tab 1")
        self.tabs.addTab(self.tab2,"Tab 2")
         
       # add matplotlib  
        sc = MplCanvas(self, width=5, height=4, dpi=100)
        sc.axes.plot([0,1,2,3,4], [10,1,20,3,40])

        # Create toolbar, passing canvas as first parament, parent (self, the MainWindow) as second.
        toolbar = NavigationToolbar(sc, self)

        layout = QVBoxLayout()
        layout.addWidget(toolbar)
        layout.addWidget(sc)

        self.tab1.layout = QVBoxLayout(self)
        self.tab1.layout.addLayout(layout)
        self.tab1.setLayout(self.tab1.layout)
       
        # Create second tab
        ### chart example 2
        series = QLineSeries(self)
  
        series << QPointF( 1, 91.9) << QPointF( 2, 89.5) << QPointF( 3, 84.6) << QPointF( 4, 92.0) << QPointF( 5, 87.6) << QPointF( 6, 89.4) << QPointF( 7, 83.9) << QPointF( 8, 90.1) << QPointF( 9, 89.7) << QPointF( 10, 89.6) << QPointF( 11, 86.1) << QPointF( 12, 85.3) << QPointF( 13, 91.3) << QPointF( 14, 89.0) << QPointF( 15, 85.3) << QPointF( 16, 91.6) << QPointF( 17, 91.1) << QPointF( 18, 91.3) << QPointF( 19, 88.3) << QPointF( 20, 84.1) << QPointF( 21, 88.9) << QPointF( 22, 91.1) << QPointF( 23, 86.6) << QPointF( 24, 88.2) << QPointF( 25, 90.3) << QPointF( 26, 89.3) << QPointF( 27, 85.0) << QPointF( 28, 88.6) << QPointF( 29, 86.0) << QPointF( 30, 87.1) << QPointF( 31, 84.3)
  
        chart =  QChart()
  
        chart.addSeries(series)
        chart.createDefaultAxes()
        chart.setAnimationOptions(QChart.SeriesAnimations)
        chart.setTitle("Line Chart Example")
  
        chart.legend().setVisible(True)
        chart.legend().setAlignment(Qt.AlignBottom)
  
  
        chartview = QChartView(chart)
        chartview.setRenderHint(QPainter.Antialiasing)        
        ###
        self.tab2.layout = QVBoxLayout(self)
        self.tab2.layout.addWidget(chartview)
        self.tab2.setLayout(self.tab2.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_())
Reply


Messages In This Thread
PYQT charts in tabs - by frohr - Feb-11-2022, 06:55 PM
RE: PYQT charts in tabs - by Axel_Erfurt - Feb-11-2022, 07:48 PM
RE: PYQT charts in tabs - by frohr - Feb-12-2022, 05:15 PM
RE: PYQT charts in tabs - by Axel_Erfurt - Feb-12-2022, 05:54 PM
RE: PYQT charts in tabs - by frohr - Feb-12-2022, 06:39 PM
RE: PYQT charts in tabs - by Axel_Erfurt - Feb-12-2022, 06:59 PM
RE: PYQT charts in tabs - by Axel_Erfurt - Feb-12-2022, 07:27 PM
RE: PYQT charts in tabs - by frohr - Feb-13-2022, 11:40 AM
RE: PYQT charts in tabs - by Axel_Erfurt - Feb-13-2022, 12:34 PM
RE: PYQT charts in tabs - by frohr - Feb-13-2022, 01:48 PM
RE: PYQT charts in tabs - by Axel_Erfurt - Feb-13-2022, 04:31 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  I want to create custom charts in Python. js1152410 1 565 Nov-13-2023, 05:45 PM
Last Post: gulshan212
  Buttons not working in tabs kenwatts275 2 1,412 May-02-2022, 04:45 PM
Last Post: kenwatts275
  create new tabs and populate using python reggie4 2 2,287 Jan-23-2021, 11:25 PM
Last Post: Larz60+
  Line charts error "'isnan' not supported for the input types," issac_n 1 2,444 Jul-22-2020, 04:34 PM
Last Post: issac_n
  HELP TabError: inconsistent use of tabs and spaces in indentation blackjesus24 2 3,600 Jan-30-2020, 10:25 AM
Last Post: blackjesus24
  Setting Tabs in a listbox scratchmyhead 2 2,851 Nov-12-2019, 11:18 PM
Last Post: scratchmyhead
  removing spaces/tabs after used .strip() zarize 0 1,618 Sep-11-2019, 12:46 PM
Last Post: zarize
  Is there any way to transfer charts generated from excel to powerpoint? zcabaaf 1 7,987 Aug-05-2019, 07:34 AM
Last Post: zcabaaf
  removing tabs from input text jenya56 3 3,276 Mar-28-2019, 03:10 PM
Last Post: DeaD_EyE
  Why interpreter accepts indentation made with spaces and not those by tabs sylas 13 5,651 Jan-10-2019, 11:17 PM
Last Post: texadactyl

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020