Build a pie chart with pyG2

Thamalu Piyadigama
Fun with Data Science
4 min readJul 7, 2020

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()

Point chart

Let us explore qualities of a pie chart.

  1. Coordinate system is polar coordinates.
  2. Geometry is interval (an area between two points just as in the case of bars in a bar chart).
  3. Radius is constant.
  4. Dependent variable (mpg) is mapped to an angle and independent variable (name) is mapped to a color.
  5. 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()

Interval chart

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()

Colored by name

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()

Polar coordinate: name for r and mpg for theta

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()

theta coordinates. sectors are overlapped.

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()

Corresponding bar chart: Bars are overlapped.

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()

Stacked intervals

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()

Pie chart

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()

Pie chart

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.

--

--

Thamalu Piyadigama
Fun with Data Science

I am a data engineer at hSenid Mobile Solutions. BSc (Electronic and Telecommunication Engineering)