![]() |
Python has become one of the most popular programming languages for data science because of its vast collection of libraries. In data science, data visualization plays a crucial role that helps us to make it easier to identify trends, patterns, and outliers in large data sets. Pygal is best suited for Data visualizations. It is used to draw charts and plots such as bar plots, line charts, and box plots. What is Pygal?Pygal is open source Python library that is specifically designed to create interactive and visually appealing charts and plots. It is used to create a wide range of chart types, including bar charts, line charts, pie charts, radar charts, scatter plots, and more. We can customize the graphs using various styles, colors, and labels to make them more interactive. Pygal allows saving the output in SVG(Scalable Vector Graphics). Key Features of Pygal Library
Checking the Python VersionThe Pygal library is available for Python 3.6, 3.7, 3.8, 3.9, and pypy. So, It is important to install Python 3.6 or a later version in our system we can also check the version already installed python version by executing the below command in the terminal. python --version Installing PygalTo use the features of Pygal library it should be installed on our system. To install Pygal execute the below command in a terminal or command prompt and if pip command is not working refer to pip install. pip install pygal How to use Pygal in Python?To use the Pygal library we have to first import it into our Python Program using the import command and then we have to create the instance of plot or chart. In the below example, we have created the instance of a bar plot using pygal.Bar() method. After that, we created the Bar Chart by passing adding some random data and saving the bar chart as a ‘BarPlot.svg’ file using the render_to_file() method. Python3
Output: We can see in the below output that when we hover over the bar it shows data of that bar. Examples of Creating Charts and Plots using Pygal in PythonNow we will see some examples of creating interactive Charts and Plots using Pygal in Python. Create a Pie Chart using Pygal in PythonIn the below example, we will draw a pie chart using the Pygal library. First, we import the Pygal library, and then create an instance of a pie chart using pygal.Pie() method. Set the title of a pie chart. Add data to the pie chart using add() method by passing the label name and value as an argument in it. Finally, save the output to the SVG file. Python3
Output:
In the below example, we will draw a stacked line chart using the Pygal library. First, we import the Pygal library and then create sample data of 6 months’ sales of various courses at GFG. Create an instance of a stacked line chart using pygal.StackedLine() method and set fill as true so that the area between two lines or a line and the x-axis is filled with color. Set the title of a pie chart and x-axis labels. Add data to the pie chart using add() method by passing the label name and value as an argument in it. Finally, save the output to the SVG file. Python3
Output: |
Reffered: https://www.geeksforgeeks.org
Python |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |