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, Pie, Radar, Box, Dot, 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.add('Females', [22,25,18,35,33,18]) bar_chart.add('Males', [30,20,23,31,39,44]) bar_chart.title = "Males and Females First Marage Age" bar_chart.x_labels=(range(1,6)) bar_chart.render()
Sample code for Basic bar chart![]() |
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.add('Females', [22,25,18,35,33,18]) HBar.add('Males', [30,20,23,31,39,44]) 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.add('Females', [22,25,18,35,33,18]) stackedbar.add('Males', [30,20,23,31,39,44]) 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:
stackedbar.add(‘Divorses’, [35,22,45,33,40,38])
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 |
By: Ali Radwani
-
January 10, 2021 at 7:11 amPython: Data Visualization Part-2 | Ali's Photography Space...
Search in site..
Taking pictures is not my main daily practices, but when i start playing with my camera, i really enjoy my self.
Thanks for visiting my Space..
The Archives
- March 2023 (4)
- February 2023 (1)
- October 2022 (3)
- September 2022 (7)
- August 2022 (9)
- July 2022 (11)
- June 2022 (8)
- March 2022 (7)
- February 2022 (6)
- January 2022 (1)
- December 2021 (1)
- November 2021 (9)
- October 2021 (10)
- September 2021 (10)
- August 2021 (13)
- July 2021 (13)
- June 2021 (12)
- May 2021 (5)
- April 2021 (11)
- March 2021 (12)
- February 2021 (5)
- January 2021 (4)
- December 2020 (1)
- November 2020 (6)
- October 2020 (2)
- September 2020 (2)
- August 2020 (6)
- July 2020 (10)
- June 2020 (6)
- May 2020 (2)
- April 2020 (2)
- March 2020 (9)
- February 2020 (6)
- January 2020 (4)
- December 2019 (9)
- November 2019 (8)
- October 2019 (5)
- August 2019 (12)
- July 2019 (18)
- June 2019 (11)
- April 2019 (16)
- December 2018 (7)
- November 2018 (7)
- April 2018 (2)
- March 2018 (14)
- February 2018 (14)
- January 2018 (16)
- December 2017 (9)
- January 2017 (1)
- November 2016 (6)
- October 2016 (14)
- September 2016 (16)
- August 2016 (14)
- July 2016 (16)
- June 2016 (15)
- May 2016 (2)
- March 2016 (5)
- February 2016 (5)
- January 2016 (17)
- December 2015 (4)
- September 2015 (12)
- August 2015 (13)
- July 2015 (3)
- June 2015 (12)
- May 2015 (9)
- April 2015 (11)
- March 2015 (14)
- February 2015 (15)
- January 2015 (16)
- December 2014 (16)
- November 2014 (15)
- October 2014 (7)
- September 2014 (11)
- August 2014 (11)
- July 2014 (12)
- June 2014 (15)
- May 2014 (14)
- April 2014 (14)
- March 2014 (14)
- February 2014 (12)
- January 2014 (14)
- December 2013 (11)
- November 2013 (13)
- October 2013 (11)
- September 2013 (14)
- August 2013 (10)
- July 2013 (15)
- June 2013 (13)
- May 2013 (23)
- April 2013 (19)
- March 2013 (13)
- February 2013 (11)
- January 2013 (17)
- December 2012 (14)
- November 2012 (12)
- October 2012 (11)
- September 2012 (14)
- August 2012 (14)
- July 2012 (12)
- June 2012 (10)
- May 2012 (16)
- April 2012 (20)
- March 2012 (23)
- February 2012 (13)
- January 2012 (1)
- December 2011 (1)
- October 2011 (2)
- September 2011 (5)
- August 2011 (7)
- July 2011 (13)
- June 2011 (4)
- March 2011 (7)
- February 2011 (13)
- January 2011 (6)
- December 2010 (8)
- November 2010 (10)
- May 2010 (1)
- March 2010 (2)
- December 2009 (1)
- November 2009 (1)
- October 2009 (1)
- August 2009 (1)
- July 2009 (1)
- March 2009 (5)
- February 2009 (3)
- September 2008 (1)
- July 2008 (2)
Tags
ahradwani ahradwani.com Ali Ali Hassan Radwani animal Art Before and After Camera Code Computer D90 D7100 database Design doha Doha-Qatar Draw Experiment Flower Flowers fujifilm fujifilm x-t30 ii Galaxy galaxynote3 Home Made HowTo Language learn Learning Lens Lesson Light light box London London 2012 London 2013 London 2014 Macro Macro on Flowers moleskin my sketchbook Nikon Nikon 18-300mm Nikon 35mm Nikon 105mm Note3 pen Pencil Photo Photography photos Photos from Qatar photoshop Photoshop action Products Project ProjectEuler Python Qatar radwani S9900 SB900 Sketch Sketch2017 Sketch2021 Sketchbook Sketches sql sqlite Stamp Strobist Studio الدوحه - قطر تصوير كاميراMost Readed..
Recent Posts..
My Flickr: Hawee - Ta3kees
Visitors Stats
- 98,052 hits