Check out Kite (free AI Coding Assistant) → Link



Buy Me a Coffee? Your support is much appreciated!

PayPal Me: https://www.paypal.me/jiejenn/5
Venmo: @Jie-Jenn

Source Code:  

import sys
import math

from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QFont, QColor
# pip install PyQtChart
from PyQt5.QtChart import QChart, QChartView, QStackedBarSeries, QBarSet, QBarCategoryAxis, QValueAxis


class MyApp(QWidget):
    def __init__(self):
        super().__init__()
        self.window_width, self.window_height = 1900, 1200
        self.setMinimumSize(self.window_width, self.window_height)

        self.layout = QVBoxLayout()
        self.setLayout(self.layout)

        low_data_points = [-52, -50, -45.3, -37.0, -25.6, -8.0,
                    -6.0, -11.8, -19.7, -32.8, -43.0, -48.0]

        high_data_points = [11.9, 12.8, 18.5, 26.5, 32.0, 34.8,
                     38.2, 34.8, 29.8, 20.4, 15.1, 11.8]

        barSet_low_data_points = QBarSet('Min') 
        barSet_high_data_points = QBarSet('Max')

        barSet_low_data_points.append(low_data_points)
        barSet_high_data_points.append(high_data_points)

        # add dataset to Stacked Bar Series object
        self.series = QStackedBarSeries()
        self.series.append(barSet_high_data_points)
        self.series.append(barSet_low_data_points)
        self.series.setLabelsVisible(True)

        # create QChart object to present the chart(s)
        self.chart = QChart()
        self.chart.addSeries(self.series)
        self.chart.setTitle('QStackedBar Chart Example')
        self.chart.setTitleFont(QFont('Open Sans', 15, QFont.Bold))

        # to create animation effect
        self.chart.setAnimationOptions(QChart.SeriesAnimations)

        # prepare x-axis labels
        months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']

        self.axis_x = QBarCategoryAxis()
        self.axis_x.setTitleText('Month')
        self.axis_x.append(months)
        self.axis_x.setLabelsFont(QFont('Open Sns', 10))
        self.chart.addAxis(self.axis_x, Qt.AlignBottom)


        # prepare y-axis labels
        self.axis_y = QValueAxis()

        range_upper = int(math.ceil(max(high_data_points) / 10)) + 40
        range_lower = int(math.floor(min(low_data_points) / 10)) - 50
        self.axis_y.setTitleText('Tempearture [°C]')
        self.axis_y.setLabelsFont(QFont('Open Sans', 10))
        self.axis_y.setRange(range_lower, range_upper)
        self.axis_y.setLabelFormat('%.1f')
        self.axis_y.setTickCount(12)
        self.chart.addAxis(self.axis_y, Qt.AlignLeft)

        self.series.attachAxis(self.axis_x)
        self.series.attachAxis(self.axis_y)

        # prepare the legend
        self.chart.legend().setVisible(True)
        self.chart.legend().setAlignment(Qt.AlignBottom)
        self.chart.legend().setFont(QFont('Open Sans', 12))

        # chart view object to display the chart
        self.chart_view = QChartView(self.chart)
        self.layout.addWidget(self.chart_view)

if __name__ == '__main__':
    # don't auto scale when drag app to a different monitor.
    # QApplication.setAttribute(Qt.HighDpiScaleFactorRoundingPolicy.PassThrough)
    
    app = QApplication(sys.argv)
    app.setStyleSheet('''
        QWidget {
            font-size: 30px;
        }
    ''')
    
    myApp = MyApp()
    myApp.show()

    try:
        sys.exit(app.exec_())
    except SystemExit:
        print('Closing Window...')