How to create a pie chart using Matplotlib
This article is about creating pie charts using Matplotlib. Xiaobian thinks it is quite practical, so share it with everyone for reference. Let's follow Xiaobian and have a look.
background introduction
You'll learn how to use Matplotlib to create pie charts, which are ideal for displaying data in a way that clearly shows the proportions of each category. We will learn how to draw pie charts, customize its appearance and more. Let's get started...
Getting Started Examples
Let's start with the simplest pie chart example, looking at apples and oranges as a percentage of sales:
from matplotlib import pyplot as pltfrom matplotlib import rcParams#Set chart font to prevent Chinese garbled rcParams <$'font.family'] = 'Microsoft YaHei'rcParams <$'font.sans-serif'] = ['Microsoft YaHei']#Set chart style plt.style.use ('fivethirtyeight ')#Define pie chart data slices = [60,40]#Define data representation labels = [' apple','orange']#Construct pie chart plt.pie(slices,labels=labels)#Set title layout display plt.title("My first pie chart? ")plt.tight_layout()plt.show() Run result:
Comprehensive case
Next we look at a complete pie chart example, drawing a pie chart of programming language usage and customizing the pie chart appearance and style:
from matplotlib import pyplot as plt from matplotlib import rcParams#Set chart font to prevent Chinese garbled rcParams <$'font.family'] = 'Microsoft YaHei'rcParams <$'font.sans-serif'] = ['Microsoft YaHei']#Set chart style plt.style.use ('fivethirtyeight ')#defines the number of usage of the development language slices = [59219, 55466, 47544, 36443, 35917]#development language label names labels = [' JavaScript','HTML/CSS', 'SQL',' Python','Java']#sets python language to pop up 10% of the distance in pie chart display explode = [0, 0, 0, 0.1, 0]#Start constructing pie chart #shadow: show shadows to look 3D #startangle: rotate pie chart 90 degrees counterclockwise from x-axis #autopct: show percentage of data #wedgeprops: set edge line color plt.pie(slices, labels=labels, explore = explore, shadow=True,startangle=90,autopct='%1.1f%%', wedgeprops={'edgecolor':'black'})
plt.title("Programming Language Usage Scale Diagram")plt.tight_layout()plt.show() Thank you for reading! About "how to use Matplotlib to create pie chart" this article is shared here, I hope the above content can be of some help to everyone, so that everyone can learn more knowledge, if you think the article is good, you can share it to let more people see it!