# Better visualization of Pie charts by MatPlotLib

--

We as humans always like to interpret pictures more than text. This is supported by the biological fact that 90% of facts that come to the brain are in form of Visuals.

When it comes to Data, visualization is a key concept. We grasp more by seeing visuals(graphs, charts, etc.) rather than just seeing data in the form of raw text. Visualization through images makes our brain to process faster and looks pleasant rather than just a big junk of text.

When it comes to making graphs, we should make it in such a way that it feels pleasant to mind. A lot of people make data visualization without considering the most important factor “Data-Ink Ratio” — term coined by Edward Rolf Tufte in his writings Information design.

Below GIF taught us the whole concept of the Data-ink ratio.

Pretty Simple, Huh ?!

Now let’s start making our Pie chart — a good looking Pie chart. let’s directly work on an example given in matplotlib documentation:

`import matplotlib.pyplot as plt# Pie chartlabels = ['Frogs', 'Hogs', 'Dogs', 'Logs']sizes = [15, 30, 45, 10]# only "explode" the 2nd slice (i.e. 'Hogs')explode = (0, 0.1, 0, 0)  fig1, ax1 = plt.subplots()ax1.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%',        shadow=True, startangle=90)# Equal aspect ratio ensures that pie is drawn as a circleax1.axis('equal')  plt.tight_layout()plt.show()`

The above code gives you a pie chart as below:

Let’s make this chart into a good looking chart:

1. Always use good colors (preferably light shades)
2. Change labels, percent label color
3. Change positions of labels and percent labels
4. Change your pie chart to donut chart(optional)

Let’s add color to graph :

Colors can be added to graph by one of the following ways:

• Hex value (#_ _ _ _ _ _)
• rgb value ( r , g , b ) or ( r , g , b , a ) where r, g, b, a are in range 0–1
• as in active color cycle ( “c0”, “c1”)[capital C followed by digit]
`import matplotlib.pyplot as plt# Pie chartlabels = ['Frogs', 'Hogs', 'Dogs', 'Logs']sizes = [15, 30, 45, 10]# only "explode" the 2nd slice (i.e. 'Hogs')explode = (0, 0.1, 0, 0)#add colorscolors = ['#ff9999','#66b3ff','#99ff99','#ffcc99']fig1, ax1 = plt.subplots()ax1.pie(sizes, explode=explode, labels=labels, colors=colors, autopct='%1.1f%%',        shadow=True, startangle=90)# Equal aspect ratio ensures that pie is drawn as a circleax1.axis('equal')plt.tight_layout()plt.show()`

Changing the color of labels on the chart

We can change the color of labels and percent labels by set_color() property of matplotlib.text.Text object which are return type of function plot.pie().

Modify the above code as below, you can find more info about the pie function’s return datatype here. Also, let’s turn off shadow effect and explode effect.

`import matplotlib.pyplot as plt# Pie chartlabels = ['Frogs', 'Hogs', 'Dogs', 'Logs']sizes = [15, 30, 45, 10]#colorscolors = ['#ff9999','#66b3ff','#99ff99','#ffcc99'] fig1, ax1 = plt.subplots()patches, texts, autotexts = ax1.pie(sizes, colors = colors, labels=labels, autopct='%1.1f%%', startangle=90)for text in texts:    text.set_color('grey')for autotext in autotexts:    autotext.set_color('grey')# Equal aspect ratio ensures that pie is drawn as a circleax1.axis('equal')  plt.tight_layout()plt.show()`

Now our pie chart looks something like this:

Changing the pie chart to donut chart to look more cool and AWESOME!!

Method of making donut graph from pie chart in MatPlotLib documentation is complicated rather we can just change the pie chart to donut chart by drawing a circle with white color at origin(Source: here).

Modify code as below to draw a circle centered at (0,0)

`import matplotlib.pyplot as plt# Pie chartlabels = ['Frogs', 'Hogs', 'Dogs', 'Logs']sizes = [15, 30, 45, 10]#colorscolors = ['#ff9999','#66b3ff','#99ff99','#ffcc99'] fig1, ax1 = plt.subplots()ax1.pie(sizes, colors = colors, labels=labels, autopct='%1.1f%%', startangle=90)#draw circlecentre_circle = plt.Circle((0,0),0.70,fc='white')fig = plt.gcf()fig.gca().add_artist(centre_circle)# Equal aspect ratio ensures that pie is drawn as a circleax1.axis('equal')  plt.tight_layout()plt.show()`

Changing positions of labels

We can change the position of labels (both outer and percent labels) by modifying labeldistance(defaul:1) and pctdistance(default:0.6)

Let’s modify code by adding pctdistance=0.85 and use explosion property to make it looks better.

`import matplotlib.pyplot as plt# Pie chartlabels = ['Frogs', 'Hogs', 'Dogs', 'Logs']sizes = [15, 30, 45, 10]#colorscolors = ['#ff9999','#66b3ff','#99ff99','#ffcc99']#explsionexplode = (0.05,0.05,0.05,0.05) plot.pie(sizes, colors = colors, labels=labels, autopct='%1.1f%%', startangle=90, pctdistance=0.85, explode = explode)#draw circlecentre_circle = plt.Circle((0,0),0.70,fc='white')fig = plt.gcf()fig.gca().add_artist(centre_circle)# Equal aspect ratio ensures that pie is drawn as a circleax1.axis('equal')  plt.tight_layout()plt.show()`

Let’s work on another example and see how we can implement multiple-pie charts to make data more visualizable.

Pie charts for the above data will be as follows:

Instead of visualizing different pie charts of gender for different languages, we can implement all the pie charts in one chart as given below:

`import matplotlib.pyplot as plt # Data to plotlabels = ['Python', 'C++', 'Ruby', 'Java']sizes = [504, 337, 415, 280]labels_gender = ['Man','Woman','Man','Woman','Man','Woman','Man','Woman']sizes_gender = [315,189,125,212,270,145,190,90]colors = ['#ff6666', '#ffcc99', '#99ff99', '#66b3ff']colors_gender = ['#c2c2f0','#ffb3e6', '#c2c2f0','#ffb3e6', '#c2c2f0','#ffb3e6', '#c2c2f0','#ffb3e6'] # Plotplt.pie(sizes, labels=labels, colors=colors, startangle=90,frame=True)plt.pie(sizes_gender,colors=colors_gender,radius=0.75,startangle=90)centre_circle = plt.Circle((0,0),0.5,color='black', fc='white',linewidth=0)fig = plt.gcf()fig.gca().add_artist(centre_circle) plt.axis('equal')plt.tight_layout()plt.show()`

Or we can just use the explosion property of slices of a pie chart to make it more cool..

`import matplotlib.pyplot as plt # Data to plotlabels = ['Python', 'C++', 'Ruby', 'Java']sizes = [504, 337, 415, 280]labels_gender = ['Man','Woman','Man','Woman','Man','Woman','Man','Woman']sizes_gender = [315,189,125,212,270,145,190,90]colors = ['#ff6666', '#ffcc99', '#99ff99', '#66b3ff']colors_gender = ['#c2c2f0','#ffb3e6', '#c2c2f0','#ffb3e6', '#c2c2f0','#ffb3e6', '#c2c2f0','#ffb3e6']explode = (0.2,0.2,0.2,0.2) explode_gender = (0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1)#Plotplt.pie(sizes, labels=labels, colors=colors, startangle=90,frame=True, explode=explode,radius=3)plt.pie(sizes_gender,colors=colors_gender,startangle=90, explode=explode_gender,radius=2 )#Draw circlecentre_circle = plt.Circle((0,0),1.5,color='black', fc='white',linewidth=0)fig = plt.gcf()fig.gca().add_artist(centre_circle) plt.axis('equal')plt.tight_layout()plt.show()`

Looking cool, right ?!!