Build a pie chart with pyG2
Pie chart is a very strange type of chart. It is the most simple graph to understand. Every small child who has eaten a slice of cheese or a piece of cake knows what sectors of a disk mean.
However it is one of the hardest elementary charts to design. If you know how to design a pie you know most of the concepts in charting.
Installation
pip install pyG2
PyG2 supports only Jupyter notebooks. (Jupyterlab and Colab are not still supported.)
- Open jupyter notebook and create a new notebook file.
- Import G2 module:
from pyG2 import G2
Prepare the data set (Details of several cars from mtcars data set):
data =[
{‘name’: ‘Mazda RX4’, ‘mpg’: 21.0, ‘cyl’: 6},
{‘name’: ‘Mazda RX4 Wag’, ‘mpg’: 21.0, ‘cyl’: 6},
{‘name’: ‘Datsun 710’, ‘mpg’: 22.8, ‘cyl’: 4},
{‘name’: ‘Valiant’, ‘mpg’: 18.1, ‘cyl’: 6},
{‘name’: ‘Duster 360’, ‘mpg’: 14.3, ‘cyl’: 8},
{‘name’: ‘Merc 280’, ‘mpg’: 19.2, ‘cyl’: 6}
]
First let us create a point chart to understand the data set.
chart = G2.Chart(height=300,width=500)
chart.data(data)
chart.point().position(‘name*mpg’)
chart.render()
Let us explore qualities of a pie chart.
- Coordinate system is polar coordinates.
- Geometry is interval (an area between two points just as in the case of bars in a bar chart).
- Radius is constant.
- Dependent variable (mpg) is mapped to an angle and independent variable (name) is mapped to a color.
- Intervals are in a stack
Let us build up a pie chart step by step
Let us change the geometry to interval
chart = G2.Chart(height=300,width=500)
chart.data(data)
chart.interval().position(‘name*mpg’)
chart.render()
Let us map independent variable (name) to a color
chart = G2.Chart(height=300,width=500)
chart.data(data)
chart.interval().position(‘name*mpg’).color(‘name’)
chart.render()
Now let us change the coordinate system to polar
chart = G2.Chart(height=300,width=500)
chart.data(data)
chart.interval().position(‘name*mpg’).color(‘name’)
chart.coordinate(‘polar’)
chart.render()
Oops! Something wrong.
LOOK! The polar coordinate (r,theta) = (name,mpg)
This is not what we want.
In a pie chart we map theta to mpg and r is not mapped to a variable (radius is constant in a pie chart.)
We want (r,theta) = (,mpg)
Hence we use transposed polar coordinates which is named theta
Then theta = (theta,r)
So (theta,r) = (mpg,)
Now mpg is mapped to theta
chart = G2.Chart(height=300,width=500)
chart.data(data)
chart.interval().position(‘mpg’).color(‘name’)
chart.coordinate(‘theta’)
chart.render()
Still something odd here
Let us see what is odd here in the Cartesian coordinates.
chart = G2.Chart(height=300,width=500)
chart.data(data)
chart.interval().position(‘mpg’).color(‘name’)
# chart.coordinate(‘theta’)
chart.render()
Bars are placed on top of another. (In the case of the polar coordinate system, sectors are placed on top of another.)
What we want is a stacked set of intervals. Let us adjust that.
chart = G2.Chart(height=300,width=500)
chart.data(data)
chart.interval().position(‘mpg’).color(‘name’).adjust(‘stack’)
# chart.coordinate(‘theta’)
chart.render()
Here you go! You want this in polar coordinates
This is our pie chart
chart = G2.Chart(height=300,width=500)
chart.data(data)
chart.interval().position(‘mpg’).color(‘name’).adjust(‘stack’)
chart.coordinate(‘theta’)
chart.render()
Theta coordinate system is nothing but transposed polar. Therefore,alternatively you can code in the following way too.
chart = G2.Chart(height=300,width=500)
chart.data(data)
chart.interval().position(‘mpg’).color(‘name’).adjust(‘stack’)
chart.coordinate(‘polar’).transpose()
chart.render()
Remember
Pie chart is a stacked interval chart in polar coordinates such that independent variable is mapped to a color and dependent variable is mapped to an angle.