Complete Tutorial on Pygal: A Python Tool for Interactive and Scalable visualization
Data visualization is a scientific method of finding out the patterns, anomalies, and outliers in the data. It is a method that is used to create the graphs and plots which helps in finding out the trend in the data because it makes data simpler in terms of understanding by the human brain and we identify trends with naked eyes. Python provides different visualization packages that help in creating different types of charts, graphs, and plots. Pygal is an open-source python library that not only creates highly interactive plots but also creates SVG images of the graphs/plots so that we can use it and customize it accordingly. Pygal is highly customizable and creates graphs with a few lines of code. Pygal creates a variety of plots like a bar, area, histogram, line, etc and due to the high scalability of the images of the plot downloaded as SVG, we will have good quality images that can be embedded into different projects, websites, etc. In this article, we will go through different charts that we can create using pygal and also see how we can download them in the SVG format. Implementation: We will start exploring pygal but before that, we need to install pygal using pip install pygal. Importing required libraries We will be importing pygal for visualization and pandas for importing the dataset for which we will be creating the visualizations. import pandas as pd import pygal Loading the Dataset We will be working with an Advertisement dataset that contains different attributes like ‘TV’, ‘Radio’, etc. and a target variable ‘Sales’. df = pd.read_csv(‘Advertising.csv’) df.head() Creating Bars and Graphs Now we will use pygal to create different bars and graphs using the dataset we have loaded. We will start by creating some basic charts and then move to some advance charts and downloading all the graphs and charts in SVG formats. Bar Charts Bar charts represent the data in the form of horizontal or vertical bars with the values they represent. Pygal supports different types of Bar charts. bar_graphs = pygal.Bar() bar_graphs.add('Sales', df['Sales'][:5]) bar_graphs.render_to_file('bar1.svg') bar_graphs.add('Sales', df['Sales'][:5]) bar_graphs.add('TV', df['TV'][:5]) bar_graphs.render_to_file('bar2.svg') Line charts A line chart displays the data or information as a series of data points on a line. line_chart = pygal.Line() line_chart.add('Sales', df['Sales'][:15]) line_chart.render_to_file('line1.svg') line_chart.add('Sales', df['Sales'][:15]) line_chart.add('TV', df['TV'][:15]) line_chart.add('Newspaper', df['Newspaper'][:15]) line_chart.add('Radio', df['Radio'][:15]) line_chart.render_to_file('line2.svg') Box Plots Box Plots are considered as the statistical graphs which are used to display the numerical data with their quartile information. They are generally used to visualize the outliers in the data. box_plot = pygal.Box() box_plot.title = 'Advertising' box_plot.add('TV', df['TV']) box_plot.add('Radio', df['Radio']) box_plot.add('Newspaper', df['Newspaper']) box_plot.render_to_file('box1.svg') Dot Plots A dot plot is a statistical plot that is used to display the data in forms of the dot, the size of the dot represents the values i.e higher the value bigger is the dot. dot_chart = pygal.Dot() dot_chart.title = 'Advertising' dot_chart.add('Sales', df['Sales'][:5]) dot_chart.add('TV', df['TV'][:5]) dot_chart.add('Radio', df['Radio'][:5]) dot_chart.add('Newspaper', df['Newspaper'][:5]) dot_chart.render_to_file('dot1.svg') Funnel Charts Funnel charts are used to visualize the progressive reduction or expansion of data when it passes from different phases. It is generally used to represent the Sales data. funnel_chart = pygal.Funnel() funnel_chart.title = 'Advrtising' funnel_chart.add('TV', df['TV'][:15]) funnel_chart.add('Radio', df['Radio'][:15]) funnel_chart.add('Newspaper', df['Newspaper'][:15]) funnel_chart.render_to_file('funnel1.svg') Gauge Chart A gauge chart combines the doughnut chart and pie chart, it is used to visualize the data at a particular value. gauge_chart = pygal.Gauge(human_readable=True) gauge_chart.title = 'Advertising' gauge_chart.range = [0, 500] gauge_chart.add('Sales', df['Sales'][:1]) gauge_chart.add('TV', df['TV'][:1]) gauge_chart.add('Radio', df['Radio'][:1]) gauge_chart.add('Newspaper', df['Newspaper'][:1]) gauge_chart.render_to_file('gauge.svg') Treemap Treemaps are used to represent data in nested forms usually in rectangles shape. The size of the map resent the value i.e higher the value greater is the size of the treemap. treemap = pygal.Treemap() treemap.title = 'Advertisement TreeMap' treemap.add('Sales', df['Sales'][:10]) treemap.add('TV', df['TV'][:10]) treemap.add('Radio', df['Radio'][:10]) treemap.add('Newspaper', df['Newspaper'][:10]) treemap.render_to_file('treemap1.svg') Pygal provides different types of parameters for different graphs, we can manipulate different designs, features, and render them accordingly. Conclusions: In this article, we learned about Pygal, a visualization package in python which provides highly interactive and scalable visualizations. We saw how we can create different charts and plots from basic to advanced in just a few lines of code. We downloaded the images in SVG format which makes it scalable for using it in different applications or websites.
Pygal creates a variety of plots like a bar, area, histogram, line, etc and due to the high scalability of the images of the plot downloaded as SVG
Pygal is an open-source python library for Data Visualization that creates highly interactive plots and their SVG images also.
["ai for beginners tutorial", "Data Visualisation", "interactive plots", "python tool", "scalability", "simple ai tutorial", "trends in bi and data visualization", "tutorial", "Visualization"]
["trends in bi and data visualization", "simple ai tutorial", "python tool", "Scala", "Visualization", "R", "Pandas", "scalability", "programming_languages:Scala", "programming_languages:Python", "Go", "AI", "interactive plots", "programming_languages:R", "ai for beginners tutorial", "Data Visualisation", "programming_languages:Go", "Python", "tutorial"]
["AI", "Pandas", "Python", "R", "Go", "Scala", "programming_languages:Python", "programming_languages:R", "programming_languages:Scala", "programming_languages:Go"]
An aspiring Data Scientist currently Pursuing MBA in Applied Data Science, with an Interest in the financial markets. I have experience in Data Analytics, Data Visualization, Machine Learning, Creating Dashboards and Writing articles related to Data Science.