Pie chart displays the data in Circular graphical representation, the size of each sector is directly proportional to the total quantity it represents.
For example, above pie chart represents the favourite sports percentage of people.
Advantages
Pie charts are simple to draw and easy to understand. Using Piecharts, we can compare the relative sizes of different categories.
How to draw a piechart?
Using 'plt.pie' method, we can define a pie chart.
# Sample data and labels
categories = ['Football', 'Basketball', 'Chess', 'Cricket', 'Others']
values = [25, 35, 10, 20, 10]
# Create a pie chart
plt.pie(values, labels=categories, autopct='%1.1f%%')
In this example:
a. categories define the labels for each category.
b. values contains the numerical values for each category.
c. plt.pie() is used to create the pie chart. The autopct parameter formats the values as percentages and display the on the chart.
Find the below working application.
hello_world.py
import matplotlib.pyplot as plt
plt.title('Favourite Sports percentage')
# Sample data and labels
categories = ['Football', 'Basketball', 'Chess', 'Cricket', 'Others']
values = [25, 35, 10, 20, 10]
# Create a pie chart
plt.pie(values, labels=categories, autopct='%1.1f%%')
plt.show()
Output
How to add different colors to different sectors of pie chart?
Using ‘colors’ parameter, we can apply different coolors to different sectors of the pie chart.
Example
# Sample data and labels
categories = ['Football', 'Basketball', 'Chess', 'Cricket', 'Others']
values = [25, 35, 10, 20, 10]
colors = ['r', 'green', '#fedcba', (0, 1, 1), (1, 0, 0, 0.5)]
# Create a pie chart
plt.pie(values, labels=categories, autopct='%1.1f%%', colors=colors)
Find the below working application.
add_colors_to_pie_chart.py
import matplotlib.pyplot as plt
plt.title('Favourite Sports percentage')
# Sample data and labels
categories = ['Football', 'Basketball', 'Chess', 'Cricket', 'Others']
values = [25, 35, 10, 20, 10]
colors = ['r', 'green', '#fedcba', (0, 1, 1), (1, 0, 0, 0.5)]
# Create a pie chart
plt.pie(values, labels=categories, autopct='%1.1f%%', colors=colors)
plt.show()
Output
Explode a particular segment of Pie chart
Using ‘explode’ parameter, we can explode particular segment of Pie chart. This functionality is useful when you want to highlight a specific category or data point within the pie chart.
Example
# Sample data and labels
categories = ['Football', 'Basketball', 'Chess', 'Cricket', 'Others']
values = [25, 35, 10, 20, 10]
explode = [0, 0, 0, 0, 0.3]
# Create a pie chart
plt.pie(values, labels=categories, autopct='%1.1f%%', explode=explode)
In this example, the explode list contains five values, with 0.3 corresponding to the last category (Others). This means that the wedge representing Category ‘Others’ will be moved away from the center by 30% of the radius of the pie.
Find the below working application.
explode_segments.py
import matplotlib.pyplot as plt
plt.title('Favourite Sports percentage')
# Sample data and labels
categories = ['Football', 'Basketball', 'Chess', 'Cricket', 'Others']
values = [25, 35, 10, 20, 10]
explode = [0, 0, 0, 0, 0.3]
# Create a pie chart
plt.pie(values, labels=categories, autopct='%1.1f%%', explode=explode)
plt.show()
Output
Previous Next Home
No comments:
Post a Comment