How to create a BarChart in pyqt5

Viewed 31

I have created a barchart using qchart. I want plot the total of males and females against their respective positions but the total does not correspond with the positions labels as shown in the image below.

Below is what I have done.

 def second_create_bar(self):
       
        male_barchart = ''' SELECT position, COUNT(*) AS Male_Count FROM employee WHERE gender='Male' GROUP BY position  '''

        male_barchart_sql = cur.execute(male_barchart).fetchall()

        series = QBarSeries()
        set0 = QBarSet("Male")

        for male in male_barchart_sql:

            set0 << male[1]
            series.append(set0)

        female_barchart = ''' SELECT position, COUNT(*) AS Female_Count FROM employee WHERE gender='Female' GROUP BY position  '''

        female_barchart_sql = cur.execute(female_barchart).fetchall()

        set1 = QBarSet("Female")
        for female in female_barchart_sql:
            set1 << female[1]
            series.append(set1)
  
          
        chart = QChart()
        chart.addSeries(series)
        chart.setTitle("Gender- Employee Positions Total ")
        chart.setAnimationOptions(QChart.SeriesAnimations)

        categories = ["Auditor", "Clerk", "Directors", "Managers", "VOs"]
        axis = QBarCategoryAxis()
        axis.append(categories)
        chart.createDefaultAxes()
        chart.setAxisX(axis, series)

        chart.legend().setVisible(True)
        chart.legend().setAlignment(Qt.AlignBottom)

        chartView = QChartView(chart)
        chartView.setRenderHint(QPainter.Antialiasing)

        self.ui.widget_321.setChart(chart)



enter image description here

0 Answers
Related