Home > Learning, Lesson, plotting, Problem, Projects/Experiments, Python > Python: Data Visualization Part-1

## Python: Data Visualization Part-1

Learning : python, pygal, Data Visualization, Bar Chart
Subject: Data visualization using pygal library

pygal is a Data Visualization library in python to help us showing our Data as a graph. In coming several posts we will discover and learn how to use the pygal library in simple and easy configuration and style.

First we need to install pygal packeg, to do so write this:
pip install pygal

Now we need some Data to show, in this leson I am using aGalaxy Tab S4, so all the codes will be tested and applyed on trinket.io website [trinket.io alow us to use pygal package online so we don’t need to install it on our divice]

Type of Chart:
pygal has several types of charts that we can use, here we will list them all then in coming posts will use each one with simple data. So what we have:
Line, Bar, Histogram, XY,
Funnel, SolidGauge, Gauge, Pyramid,
Treemap, Maps

Some of those charts has a sub-types such as in Bar char we have: Basic, Stacked and Horizontal. Also for each chart we can add a title and labels and we can use some styles.

So let’s start ..
First we will go for the Bar chart, and we have three sub-types as Basic, Stacked and Horizontal.

First chart: Bar chart:
In this part we will demonstrate the Bar Chart, it has three sub-types as Basic, Stacked and Horizontal.

We assume that our data is the Males and Female ages on first marage, the data will be as dictionary (later we will see how to customize each bar)

``` # Basic Bar Chart using pygal

import pygal
bar_chart = pygal.Bar() # To create a bar graph object

bar_chart.title = "Males and Females First Marage Age"
bar_chart.x_labels=(range(1,6))

bar_chart.render()
```

Another sub-type in Bar chart is Horizontal-Bar, it is semelar to the Basic but as if fliped 90 degree. Here is the code ..

``` # Horizontal Bar Chart using pygal

import pygal

# HorizontalBar()
HBar = pygal.HorizontalBar()

HBar.title = "Males and Females First Marage Age"

HBar.x_labels=(range(1,6))

HBar.render()
```

 Sample code for Horizontal Bar chart Last sub-type in Bar chart is Stacked Bar were all data of each element will be in one bar. Here is the code and example..

``` # Stacked Bar Chart using pygal

import pygal
# StackedBar()
stackedbar = pygal.StackedBar()
stackedbar.x_labels=(range(1,0))
stackedbar.title = "Males and Females First Marage Age"

stackedbar.render()
```

If we say we have another data-set as “age in First-Divorces” and we want to add this set to the Stacked Bar chart, then we first will create the data-set as:
and we will arrange the code line to be at top,middle or bottom of the bar. Here is the code..

 Sample code for stacke Bar chart with Divorce data Next we will talk about Line chart.

:: Data Visualization using pygal ::

 Part-1Bar-Chart Part-2 Part-3 Part-4 Follow me on Twitter..