w3resource

Matplotlib Pie Chart: Create a pie chart with a title

Matplotlib Pie Chart: Exercise-2 with Solution

Write a Python programming to create a pie chart with a title of the popularity of programming Languages.

Sample data:
Programming languages: Java, Python, PHP, JavaScript, C#, C++
Popularity: 22.2, 17.6, 8.8, 8, 7.7, 6.7

Sample Solution:

Python Code:

import matplotlib.pyplot as plt
# Plot data
languages = 'Java', 'Python', 'PHP', 'JavaScript', 'C#', 'C++'
popuratity = [22.2, 17.6, 8.8, 8, 7.7, 6.7]
#colors = ['red', 'gold', 'yellowgreen', 'blue', 'lightcoral', 'lightskyblue']
colors = ["#1f77b4", "#ff7f0e", "#2ca02c", "#d62728", "#9467bd", "#8c564b"]
# explode 1st slice
explode = (0.1, 0, 0, 0, 0, 0)  
# Plot
plt.pie(popuratity, explode=explode, labels=languages, colors=colors,
autopct='%1.1f%%', shadow=True, startangle=140)
plt.title("PopularitY of Programming Language\n" + "Worldwide, Oct 2017 compared to a year ago", bbox={'facecolor':'0.8', 'pad':5})
plt.show()

Sample Output:

Matplotlib PieChart: Create a pie chart with a title of the popularity of programming Languages

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python programming to create a pie chart of the popularity of programming Languages.
Next: Write a Python programming to create a pie chart with a title of the popularity of programming Languages. Make multiple wedges of the pie.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.