In this article we are going to learn How to Create Modern Charts in Python, so for creating Charts in Python we are going to use PySide6 GUI library, PySide6 is Python binding for Qt framework, which allows developers to create graphical user interfaces (GUIs) for desktop applications. PySide6 is set of Python modules that provides access to Qt application framework, enabling developers to create cross platform applications that can run on Windows, Linux and macOS.
PySide6 is community driven project that is designed to be compatible with the latest version of Qt currently Qt 6. PySide6 is released under the LGPL (Lesser General Public License), which allows developers to create commercial applications without the need to release their source code.
PySide6 provides number of features, including support for many of Qt modules, such as QtCore, QtGui, QtWidgets, QtNetwork, QtSvg and many more. PySide6 also includes support for internationalization enabling developers to create applications that can be easily translated into different languages, in PySide6 there a module called QtCharts and that is used for building different types of charts.
So now first of all we need to install PySide6, you can use pip for installation of PySide6.
1 |
pip install PySide6 |
In this article we want to create different charts.
Create LineChart in Python PySide6
Creating LineCharts is important part of data visualization, which helps to gain insights into complex data patterns. In this lesson we are going to explore how to create LineCharts in Python using PySide6 library.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
import sys from PySide6.QtCore import Qt from PySide6.QtGui import QPainter from PySide6.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget from PySide6.QtCharts import QChart, QChartView, QLineSeries, QValueAxis class MainWindow(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("GeeksCoders LineChart Example") # Create the main widget and layout widget = QWidget() layout = QVBoxLayout(widget) # Create the chart view and add it to the layout chart_view = QChartView() chart_view.setRenderHint(QPainter.Antialiasing) layout.addWidget(chart_view) # Create the chart and add it to the view chart = QChart() chart.setTitle("GeeksCoders Monthly Sales") chart.setAnimationOptions(QChart.SeriesAnimations) # Add data to the chart series = QLineSeries() series.setName("Sales Data") sales_data = [10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65] months = range(1, 13) for i in range(12): series.append(months[i], sales_data[i]) chart.addSeries(series) # Add axes to the chart axis_x = QValueAxis() axis_x.setLabelFormat("%i") axis_x.setTitleText("Month") chart.addAxis(axis_x, Qt.AlignBottom) series.attachAxis(axis_x) axis_y = QValueAxis() axis_y.setLabelFormat("%i") axis_y.setTitleText("Sales") chart.addAxis(axis_y, Qt.AlignLeft) series.attachAxis(axis_y) # Set the chart view's chart chart_view.setChart(chart) # Set the central widget self.setCentralWidget(widget) if __name__ == '__main__': app = QApplication(sys.argv) window = MainWindow() window.show() sys.exit(app.exec()) |
In the above code we have imported necessary modules from PySide6. after that we have define custom MainWindow class that extends from QMainWindow and create main widget and layout.
Next we have created QChartView instance that will display the LineChart and add it to the layout. we also have created QChart instance and set its title and animation options. after that we added QLineSeries instance to the chart and populate it with the sales_data and months arrays.
We also added two QValueAxis instances to the chart, one for the X-axis and one for the Y-axis, and attach them to the QLineSeries, and finally we have set the chart view’s chart to the QChart instance and set the central widget of the main window to the widget.
Run the complete code and this will be the result
Create BarChart in Python PySide6
Bar charts are an effective way to visualize categorical data and it makes it easy to compare the values of different groups. In this lesson we are going to explore how to create simple bar chart in Python PySide6 using the QtCharts module.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
import sys from PySide6.QtCore import Qt from PySide6.QtGui import QPainter from PySide6.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget from PySide6.QtCharts import QChart, QChartView,QBarSeries, QBarSet, QBarCategoryAxis, QValueAxis class MainWindow(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("GeeksCoders BarChart Example") # Create the main widget and layout widget = QWidget() layout = QVBoxLayout(widget) # Create the chart view and add it to the layout chart_view = QChartView() chart_view.setRenderHint(QPainter.Antialiasing) layout.addWidget(chart_view) # Set the central widget self.setCentralWidget(widget) # Add some data to the chart chart = QChart() chart.setTitle("GeeksCoders Monthly Sales") chart.setAnimationOptions(QChart.SeriesAnimations) series = QBarSeries() series.setName("Sales Data") sales_data = [10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65] months = range(1, 13) set0 = QBarSet("Sales") for i in range(12): set0.append(sales_data[i]) series.append(set0) chart.addSeries(series) # Add axes to the chart axis_x = QBarCategoryAxis() axis_x.append(months) chart.addAxis(axis_x, Qt.AlignBottom) series.attachAxis(axis_x) axis_y = QValueAxis() axis_y.setLabelFormat("%i") axis_y.setTitleText("Sales") chart.addAxis(axis_y, Qt.AlignLeft) series.attachAxis(axis_y) # Set the chart view's chart chart_view.setChart(chart) if __name__ == '__main__': app = QApplication(sys.argv) window = MainWindow() window.show() sys.exit(app.exec()) |
In the above code we have imported necessary modules from PySide6. after that we have defined custom MainWindow class that extends from QMainWindow and create the main widget and layout.
after that we have created QChartView instance that will display the BarChart and add it to the layout. we also created QChart instance and set its title and animation options. Then, we create a QBarSeries instance and populate it with the sales_data array using QBarSet instance.
after that we have added QBarSeries instance to the QChart instance using the addSeries method. we also added QBarCategoryAxis instance for the X-axis and QValueAxis instance for the Y-axis, and attach them to the QBarSeries using the attachAxis method.
and finally we set QChartView’s chart to the QChart instance and set the central widget of the main window to the widget.
Run the complete code and this will be the result
Create PieChart in Python PySide6
Pie charts are an effective way to visualize proportions and it makes it easier to compare the relative sizes of different categories. In this lesson we are going to explore how to create simple pie chart in PySide6 using the QtCharts module.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
import sys from PySide6.QtCore import Qt from PySide6.QtGui import QPainter from PySide6.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget from PySide6.QtCharts import QChart, QChartView, QPieSeries class MainWindow(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("GeeksCoders PieChart Example") # Create the main widget and layout widget = QWidget() layout = QVBoxLayout(widget) # Create the chart view and add it to the layout chart_view = QChartView() chart_view.setRenderHint(QPainter.Antialiasing) layout.addWidget(chart_view) # Set the central widget self.setCentralWidget(widget) # Add some data to the chart chart = QChart() chart.setTitle("GeeksCoders Sales by Product") chart.setAnimationOptions(QChart.SeriesAnimations) series = QPieSeries() series.append("Product A", 10) series.append("Product B", 20) series.append("Product C", 30) series.append("Product D", 40) chart.addSeries(series) # Set the chart view's chart chart_view.setChart(chart) if __name__ == '__main__': app = QApplication(sys.argv) window = MainWindow() window.show() sys.exit(app.exec()) |
In the above code we have imported necessary modules from PySide6. after that we have define custom MainWindow class that extends from QMainWindow and create the main widget and layout.
after that we create QChartView instance that will display PieChart and add it to the layout. we also creates QChart instance and set its title and animation options. after that we create QPieSeries instance and add four data points to represent the sales of four products. we use the append method to add each data point to the series.
we add the QPieSeries instance to the QChart instance using the addSeries method, and finally we set the QChartView’s chart to the QChart instance and set the central widget of the main window to the widget.
Run the complete code and this will be the result
Create DonutChart in Python PySide6
Donut charts are an effective way to visualize proportions, it is similar to pie charts. however donut charts also have hole in the center and it allows for additional information to be displayed. In this lesson we are going to explore how to create simple donut chart in PySide6 using the QtCharts module.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
import sys from PySide6.QtCore import Qt from PySide6.QtGui import QPainter from PySide6.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget from PySide6.QtCharts import QChartView, QChart, QPieSeries class MainWindow(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("GeeksCoders DonutChart Example") # Create the main widget and layout widget = QWidget() layout = QVBoxLayout(widget) # Create the chart view and add it to the layout chart_view = QChartView() chart_view.setRenderHint(QPainter.Antialiasing) layout.addWidget(chart_view) # Set the central widget self.setCentralWidget(widget) # Add some data to the chart chart = QChart() chart.setTitle("GeeksCoders Sales by Region") chart.setAnimationOptions(QChart.SeriesAnimations) series = QPieSeries() series.append("North", 10) series.append("South", 20) series.append("East", 30) series.append("West", 40) chart.addSeries(series) # Set the chart view's chart chart_view.setChart(chart) # Add a hole to the chart chart.setTheme(QChart.ChartThemeDark) chart.setDropShadowEnabled(False) chart.legend().setVisible(True) chart.legend().setAlignment(Qt.AlignBottom) if __name__ == '__main__': app = QApplication(sys.argv) window = MainWindow() window.show() sys.exit(app.exec()) |
In the above code we have imported necessary modules from PySide6. after that we have defined custom MainWindow class that extends from QMainWindow and creates the main widget and layout.
Next we have created QChartView instance that will display DonutChart and add it to the layout. we also creates QChart instance and set its title and animation options. then we have created QPieSeries instance and add four data points to represent the sales of four regions. we use append method to add each data point to the series.
we have added QPieSeries instance to the QChart instance using the addSeries method. and finally we set QChartView’s chart to the QChart instance and set central widget of the main window to the widget.
we also added hole to the chart by setting theme to QChart.ChartThemeDark and disabling the drop shadow, also we have enabled the chart’s legend and set its alignment.
Run the complete code and this will be the result
Learn More on Python
- PyQt5 QTableWidget Tutorial: Create a Dynamic Table
- Create GUI Applications with Python & TKinter
- Python TKinter Layout Management
- How to Create Label in TKinter
- How to Create Buttin in Python TKinter
- Build Music Player in Python TKinter
- Python GUI Programming with TKinter
- TKinter VS PyQt, Which one is Good
- Creating Custom Widgets in TKinter
- How to Create Menus in TKinter