Donut Charts Creation in Python Programming
Hey fellow Python coder! In this tutorial, we will be exploring Donut charts and learning how to implement them in detail using Python programming language. So, without any delay, let’s begin!
Introduction to Donut Charts
In this section, we will learn what Donut charts are and will also make ourselves familiar with the terminologies revolving around them. A donut chart is simply a chart representing data in a circular form, similar to a pie chart. But why “donut”? This is because this type of chart has a hole in the center just like a real-life donut.
To understand the terminologies have a look at the illustration below. In the illustration, we are considering a bakery that sells donuts and to represent the specials in the bakery, the chef makes use of a donut chart outside on the menu board.
The terminologies involved in donut charts are as follows:
- Categories: By category, we mean different objects being evaluated on the donut chart. In the case shown above, categories will be different types of donuts available in the bakery.
- Values: Each category in the donut chart is always associated with a value, which determines the portion of the donut chart a particular category occupies around the donut chart. In this case, it can be the popularity percentage of a certain donut in the bakery.
- Center: Unlike a pie chart, a donut chart has a hole in the center. This extra space can be used to provide any additional information. For instance, in this case, we can add information about prices or special offers.
- Legend: A legend is often included alongside the donut chart to provide labels for each category represented.
- Colors: Colors are used to differentiate between different categories present in the chart.
Code Implementation for Donut Charts
This section will cover, the implementation of Donuts in Python using various libraries and functions. We will start by creating the dataset using the code snippet below. We will make use of the same example shown above (the Bakery selling Donuts).
donutFlavors = ['Chocolate frosted donut', 'Blueberry cake donut', 'Jelly-filled donut', 'Pumpkin spice donut', 'Red velvet donut'] donutSales = [150, 100, 120, 80, 90]
Implementation of Pie Chart
We can visualize a basic Pie chart using the code snippet below. If you have any confusion regarding the implementation of Pie charts, do read the tutorial below.
Also Read: Create a pie chart using Matplotlib in Python
import matplotlib.pyplot as plt plt.pie(donutSales, labels=donutFlavors, autopct='%1.1f%%', startangle=90) plt.title("Pie Chart for Donut Bakery") plt.show()
The startangle=90
parameter helps to rotate the chart to start it at 90 degrees. The code execution results in the plot below:
Implementation of the Donut Chart
We can visualize a corresponding donut chart using the code snippet below. We will start by plotting a basic pie chart using the similar code snippet as shown below and then we create a white circular center using the plt.Circle()
function.
fig, ax = plt.subplots() ax.pie(donutSales, labels=donutFlavors, autopct='%1.1f%%', startangle=90) centre_circle = plt.Circle((0,0),0.70,fc='white') fig = plt.gcf() fig.gca().add_artist(centre_circle) ax.axis('equal') plt.title("Donut Chart for Donut Bakery") plt.tight_layout() plt.legend(loc='right', bbox_to_anchor=(1.7, 1)) plt.show()
The code execution results in the plot below:
Adding Information in the circular center
Now as I mentioned earlier, we can provide some additional information in the center of the donut chart by simply using the plt.text
function as displayed in the code below.
fig, ax = plt.subplots() ax.pie(donutSales, labels=donutFlavors, autopct='%1.1f%%', startangle=90) centre_circle = plt.Circle((0,0),0.70,fc='white') fig = plt.gcf() fig.gca().add_artist(centre_circle) offer = "Buy 1 Get 1 Free" plt.text(0, 0, offer, ha='center', va='center', fontsize=14, color='red') ax.axis('equal') plt.title("Donut Chart for Donut Bakery") plt.tight_layout() plt.legend(loc='right', bbox_to_anchor=(1.7, 1)) plt.show()
The code execution results in the plot below:
I hope you learned something new through this tutorial. Have a look at the following tutorials as well:
- Waffle Chart using pyWaffle in Python
- Plot Polar Chart in Python using matplotlib
- Create a Line Chart using Vincent in Python
- Radar chart in pygal
Happy Learning!
Leave a Reply