第五章 使用 matplotlib 绘制饼图

您所在的位置:网站首页 python画饼图示例 第五章 使用 matplotlib 绘制饼图

第五章 使用 matplotlib 绘制饼图

2023-01-22 08:26| 来源: 网络整理| 查看: 265

系列文章目录

第一章 使用 matplotlib 绘制折线图第二章 使用 matplotlib 绘制条形图第三章 使用 matplotlib 绘制直方图第四章 使用 matplotlib 绘制散点图第五章 使用 matplotlib 绘制饼图第六章 使用 matplotlib 绘制热力图第七章 使用 matplotlib 绘制堆叠条形图第八章 使用 matplotlib 在一个画布内绘制多个图

文章目录 系列文章目录 前言 一、什么是饼图? 二、饼图的绘制 三、应用场景 * 1.适用场景 2.不适用场景 四、总结 前言

上一章我们讲述了散点图的绘制,本章我们来讲述饼图的绘制。

一、什么是饼图?

饼图,或饼图,是一种将一个圆划分为几个扇区的统计图。在饼图中,每个扇子的弧长代表了物种占总数的比例,这些扇子恰好在一起是一个完整的圆圈。

[En]

A pie chart, or pie chart, is a statistical chart that divides a circle into several sectors. In the pie chart, the arc length of each fan represents the proportion of the species to the total, and these fans together happen to be a complete circle.

饼形图最突出的功能就是显示出其所占比例。传统上,人们还会用饼图来比较风扇的大小,从而了解数据。然而,由于人类对“角度”的感知不如“长度”,所以当需要准确的数值时(尤其是数值接近或较大时),饼图往往无能为力,因此建议改用条形图。

[En]

The most prominent function of the pie chart is to show the “proportion”. Traditionally, people also use pie charts to compare the size of the fan, so as to gain an understanding of the data. However, because human perception of “angle” is not as good as “length”, pie charts are often incompetent when accurate numerical values are needed (especially when the values are close or large), so it is recommended to use bar charts instead.

在使用时,要确保每个扇区的数据总和等于100%;避免超过5个扇区,并尽可能保持图表的简洁和清晰。

[En]

When in use, it is necessary to make sure that the total data of each sector is equal to 100%; avoid more than 5 sectors and keep the chart as concise and clear as possible.

二、饼图的绘制

让我们通过一个例子来看一下饼图的绘制。示例代码如下:

[En]

Let’s look at the drawing of the pie chart through an example. The sample code is as follows:

import matplotlib.pyplot as plt plt.style.use('fivethirtyeight') languages = ['JavaScript', 'HTML/CSS', 'SQL', 'Python', 'Java'] popularity = [59219, 55466, 47544, 36443, 35917] plt.pie(popularity) plt.tight_layout() plt.show()

在上面的代码中,我们只要将一个列表传入到 pie 函数中,便可以绘制一个饼图。代码执行后得到的图形如下图所示:

第五章 使用 matplotlib 绘制饼图我们不知道上面饼图中每个扇区代表的数据,所以让我们为每个扇区贴上标签。 [En]

We don’t know the data represented by each sector from the pie chart above, so let’s label each sector.

import matplotlib.pyplot as plt plt.style.use('fivethirtyeight') languages = ['JavaScript', 'HTML/CSS', 'SQL', 'Python', 'Java'] popularity = [59219, 55466, 47544, 36443, 35917] plt.pie(popularity, labels=languages) plt.tight_layout() plt.show()

为了给每个扇形打上标签,我们只需要传入参数 labels 即可。代码执行后得到的图形如下图所示:

第五章 使用 matplotlib 绘制饼图除了标记每个扇区,我们还可以在图表上标记每个扇区的百分比。示例代码如下: [En]

In addition to labeling each sector, we can also mark the percentage of each sector on the graph. The example code is as follows:

import matplotlib.pyplot as plt plt.style.use('fivethirtyeight') languages = ['JavaScript', 'HTML/CSS', 'SQL', 'Python', 'Java'] popularity = [59219, 55466, 47544, 36443, 35917] plt.pie(popularity, labels=languages, autopct='%1.1f%%') plt.tight_layout() plt.show()

上面的代码中,我们通过参数 autopct 为每个扇形标上占比。代码执行后得到的图形如下图所示:

第五章 使用 matplotlib 绘制饼图在上图中,风扇是从大到小逆时针排列的。一般来说,我们习惯于顺时针。让我们用参数设置顺时针方向。示例代码如下: [En]

In the figure above, the fan is arranged counterclockwise from large to small. In general, we are used to clockwise. Let’s set the clockwise direction with parameters. The example code is as follows:

import matplotlib.pyplot as plt plt.style.use('fivethirtyeight') languages = ['JavaScript', 'HTML/CSS', 'SQL', 'Python', 'Java'] popularity = [59219, 55466, 47544, 36443, 35917] plt.pie(popularity, labels=languages, autopct='%1.1f%%', counterclock=False, startangle=90) plt.title('top5 编程语言占比') plt.tight_layout() plt.show()

上面代码中,通过设置参数 counterclock 为 False,使得方向改为顺时针方向,通过设置参数 startangle 为 90,将最大扇形放在 12 点钟方向。代码执行后得到的图形如下图所示:

第五章 使用 matplotlib 绘制饼图有时,为了强调一个风扇,我们将风扇从整个圈子中分离出来。示例代码如下: [En]

Sometimes, in order to emphasize a fan, we detach the fan from the whole circle. The sample code is as follows:

import matplotlib.pyplot as plt plt.style.use('fivethirtyeight') languages = ['JavaScript', 'HTML/CSS', 'SQL', 'Python', 'Java'] popularity = [59219, 55466, 47544, 36443, 35917] plt.pie(popularity, labels=languages, autopct='%1.1f%%', counterclock=False, startangle=90,explode=[0,0,0.1,0,0]) plt.title('top5 编程语言占比') plt.tight_layout() plt.show()

在上面的代码中,我们通过设置参数 explode 来强调 SQL 这个扇形,要强调那个扇形,就把相应的值设置成非 0。代码执行后得到的图形如下图所示:

第五章 使用 matplotlib 绘制饼图 三、应用场景 1.适用场景 希望突出一部分在整体中的比例,特别是当该部分的比例达到整体的25%或50%时 [En]

want to highlight the proportion of a part in the whole, especially when the proportion of that part reaches 25% or 50% of the total.*

分类数量最好小于5个。不同分类之间存在明显差异。[En]

there are significant differences among different classifications.*

需要确定的图表空间大小(随着分类的增加不会有更多的画布空间)。[En]

the size of the charting space that needs to be determined (there will not be more canvas space as the classification increases).*

2.不适用场景如果变量相互独立且不构成一个整体,则不能使用饼图。[En]

if the variables are independent of each other and do not form a whole, then you cannot use a pie chart.*

饼图不能用来表现趋势。由于饼图使用面积而不是长度,增加了比较各种数据的难度因此,当你需要比较数据时,区分谁大谁小,特别是当数据接近时,条形图更合适。[En]

because the pie chart uses area instead of length, it makes it more difficult to compare the various data. Therefore, when you need to compare the data, to distinguish which is big or small, especially when the data is close, the bar chart is more appropriate.*

品类过多时不建议做饼图,否则读数不佳[En]

Pie chart is not recommended when there are too many categories, otherwise the reading will be poor.*

四、总结

在本章中,我们讨论了饼图的绘制以及饼图的适用场景和不适用场景。

[En]

In this chapter, we talk about the drawing of the pie chart and the applicable and inapplicable scenes of the pie chart.

上一章 使用 matplotlib 绘制散点图

Original: https://blog.csdn.net/mr_songw/article/details/124317403Author: mr_songwTitle: 第五章 使用 matplotlib 绘制饼图

相关阅读 Title: pandas将某一列变为索引_Pandas| pd.melt()使用示例

具体工作内容如下,主要是将二维表转换为一维表,如下图(表为替代):

[En]

The specific contents of the work are as follows, mainly to convert a two-dimensional table into an one-dimensional table, such as the following figure (the table is a substitute):

第五章 使用 matplotlib 绘制饼图

于是我马上想到了pandas,想着这么强大的函数肯定有这个功能,于是我开始翻阅资料,没想到还真找到了,而且仅用三行代码就搞定了,惊的朋友直呼python牛批

接下来,我们来详细介绍一下整个过程。

[En]

Next, let’s introduce the whole process in detail.

1.正确读取表格

首先,用传统的方式读表:

[En]

First, read the table in the traditional way:

import pandas as pddata1 = pd.read_excel('高中生数量.xlsx')data1

第五章 使用 matplotlib 绘制饼图

发现索引列没有被识别,产生了Unnamed: 0列,所以我们应该把第一列设置为索引列,代码如下:

import pandas as pddata1 = pd.read_excel('高中生数量.xlsx',index_col=0)  #index_col用来设置索引列data1

第五章 使用 matplotlib 绘制饼图

通过这种方式,可以正常读取和识别表格。

[En]

In this way, the form can be read and recognized normally.

2.重置索引

此步骤主要是将索引列重置为正常列,以方便下一步,代码如下

[En]

This step is mainly to reset the index column to a normal column, so as to facilitate the next step, the code is as follows

data2=data1.reset_index()data2

第五章 使用 matplotlib 绘制饼图

可以发现,之前的索引列编程’index’列了

3.将列名转换为列数据

这一步主要用到pandas的melt函数,melt是逆转操作函数,可以将列名转换为列数据(columns name → column values),重构DataFrame,用法如下:

pandas.melt(frame, id_vars=None, value_vars=None, var_name=None, value_name='value', col_level=None)

参数解释:

frame:要处理的数据集;id_vars:不需要被转换的列名;value_vars:需要转换的列名,如果剩下的列全部都要转换,就不用写了;var_name和value_name是自定义设置对应的列名;col_level :如果列是MultiIndex,则使用此级别。

我们把’index’列保留,并把转换后的列命名为’year’,value命名为’stu_num’:

data3=data2.melt(id_vars='index', var_name='year',value_name='stu_num')data3

第五章 使用 matplotlib 绘制饼图 4.把第一列设置为索引列

要防止保存的表具有数字索引,需要将第一列设置为索引列:

[En]

To prevent the saved table from having a numeric index, you need to set the first column as the index column:

data4=data3.set_index('index')data4

第五章 使用 matplotlib 绘制饼图 5.保存表格 data4.to_excel('转换后表格.xlsx')

完成后,只需一行代码即可完成上述代码:

[En]

When it’s done, the above code can be done with 1 line of code:

data=data.reset_index().melt('index', var_name='col').set_index('index')

第五章 使用 matplotlib 绘制饼图

第五章 使用 matplotlib 绘制饼图第五章 使用 matplotlib 绘制饼图

精彩推荐牵引小哥教你学Python数据可视化Python和Anaconda的安装方法Spyder基本操作和使用技巧绘图要素——颜色、线型、标记图形认识并使用Matplotlib快速绘图更多精彩原创内容!

第五章 使用 matplotlib 绘制饼图

关注我,一起学Python!

Original: https://blog.csdn.net/weixin_35950078/article/details/112495066Author: 氢气青丘Title: pandas将某一列变为索引_Pandas| pd.melt()使用示例

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/324995/

转载文章受原作者版权保护。转载请注明原作者出处!



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3