如何从Pandas数据框架中创建饼图

如何从Pandas数据框架中创建饼图

在这篇文章中,我们将讨论如何使用Python从Pandas数据框架创建饼图。

环形图中的数据由饼状图表示,它是一种图表形式。在研究、工程和商业中,它经常被利用。饼的各段描述了数据的相对强度,是一种数据的图形表示。饼状图需要一个类别和数字变量的清单。饼 “是指整体,而 “片 “是指饼的各个组成部分。它被分为段和部门,每个段和部门代表整个饼图的一块(百分比)。所有的数据加起来是360度。饼的整个价值总是百分之百。

让我们首先创建一个简单的饼图。

简单饼状图

首先,所有需要的模块被导入,一个数据框架被初始化。为了绘制饼状图,使用了plot()函数,并将种类属性设置为饼状。

语法:

plot(kind='pie')

例子:一个简单的饼状图

import pandas as pd
  
# DataFrame of each student and the votes they get
dataframe = pd.DataFrame({'Name': ['Aparna', 'Aparna', 'Aparna',
                                   'Aparna', 'Aparna', 'Juhi', 
                                   'Juhi', 'Juhi', 'Juhi', 'Juhi',
                                   'Suprabhat', 'Suprabhat', 'Suprabhat',
                                   'Suprabhat', 'Suprabhat'],
                          'votes_of_each_class': [12, 9, 17, 19, 20, 
                                                  11, 15, 12, 9, 4, 
                                                  22, 19, 17, 19, 18]})
  
# Plotting the pie chart for above dataframe
dataframe.groupby(['Name']).sum().plot(kind='pie', y='votes_of_each_class')

输出:

如何从Pandas数据框架中创建饼图?

饼状图为百分比

要添加百分比的autopct属性被设置为一个适当的值,这将自动添加百分比到每个部分。

语法:

plot(kind='pie', autopct)

例子:在饼状图中添加百分比

import pandas as pd
  
# DataFrame of each student and the votes they get
dataframe = pd.DataFrame({'Name': ['Aparna', 'Aparna', 'Aparna', 
                                   'Aparna', 'Aparna', 'Juhi', 
                                   'Juhi', 'Juhi', 'Juhi', 'Juhi',
                                   'Suprabhat', 'Suprabhat', 
                                   'Suprabhat', 'Suprabhat', 
                                   'Suprabhat'],
                          'votes_of_each_class': [12, 9, 17, 19, 
                                                  20, 11, 15, 12, 
                                                  9, 4, 22, 19, 17, 
                                                  19, 18]})
  
# Plotting the pie chart for above dataframe
dataframe.groupby(['Name']).sum().plot(
    kind='pie', y='votes_of_each_class', autopct='%1.0f%%')

输出:

如何从Pandas数据框架中创建饼图?

在饼状图中定义颜色

为了给饼状图添加颜色,颜色属性被设置为一个适当的颜色列表。

语法:

plot(kind='pie', colors)

例子:为饼状图添加颜色

import pandas as pd
  
# DataFrame of each student and the votes they get
dataframe = pd.DataFrame({'Name': ['Aparna', 'Aparna', 'Aparna', 
                                   'Aparna', 'Aparna', 'Juhi',
                                   'Juhi', 'Juhi', 'Juhi', 'Juhi',
                                   'Suprabhat', 'Suprabhat', 
                                   'Suprabhat', 'Suprabhat', 
                                   'Suprabhat'],
                          'votes_of_each_class': [12, 9, 17, 19,
                                                  20, 11, 15, 12, 
                                                  9, 4, 22, 19, 17,
                                                  19, 18]})
  
# Defining colors for the pie chart
colors = ['pink', 'silver', 'steelblue']
  
# Plotting the pie chart for above dataframe
dataframe.groupby(['Name']).sum().plot(
    kind='pie', y='votes_of_each_class', 
  autopct='%1.0f%%', colors=colors)

输出:

如何从Pandas数据框架中创建饼图?

饼状图中的爆炸效果

饼状图的爆炸意味着将其分解成各个部分。为此,我们使用explode属性并将其设置为一个适当的值。

语法:

plot(kind='pie', explode)

例子: 爆裂饼状图

import pandas as pd
  
# DataFrame of each student and the votes they get
dataframe = pd.DataFrame({'Name': ['Aparna', 'Aparna', 'Aparna', 
                                   'Aparna', 'Aparna', 'Juhi', 
                                   'Juhi', 'Juhi', 'Juhi', 'Juhi',
                                   'Suprabhat', 'Suprabhat', 
                                   'Suprabhat', 'Suprabhat', 
                                   'Suprabhat'],
                          'votes_of_each_class': [12, 9, 17, 19, 
                                                  20, 11, 15, 12,
                                                  9, 4, 22, 19, 17, 
                                                  19, 18]})
  
# Defining colors for the pie chart
colors = ['pink', 'silver', 'steelblue']
  
# Define the ratio of gap of each fragment in a tuple
explode = (0.05, 0.05, 0.05)
  
# Plotting the pie chart for above dataframe
dataframe.groupby(['Name']).sum().plot(
    kind='pie', y='votes_of_each_class', autopct='%1.0f%%',
  colors=colors, explode=explode)

输出 :

如何从Pandas数据框架中创建饼图?

饼图中的阴影效果

阴影为我们的饼图增加了一个额外的维度,为此只需将阴影属性设置为True。

语法:

plot(kind='pie', shadow=True)

例子:饼状图的阴影效果

import pandas as pd
  
# DataFrame of each student and the votes they get
dataframe = pd.DataFrame({'Name': ['Aparna', 'Aparna', 'Aparna', 
                                   'Aparna', 'Aparna', 'Juhi', 
                                   'Juhi', 'Juhi', 'Juhi', 'Juhi',
                                   'Suprabhat', 'Suprabhat', 
                                   'Suprabhat', 'Suprabhat', 
                                   'Suprabhat'],
                          'votes_of_each_class': [12, 9, 17, 19, 
                                                  20, 11, 15, 12, 
                                                  9, 4, 22, 19, 17, 
                                                  19, 18]})
  
  
# Plotting the pie chart for above dataframe
# and implementing shadow effect
dataframe.groupby(['Name']).sum().plot(
    kind='pie', y='votes_of_each_class', autopct='%1.0f%%', shadow=True)

输出:

如何从Pandas数据框架中创建饼图?

在饼状图中设置起始角度

start Angle意味着我们可以根据我们指定的度数角度来旋转饼状图。为此startangle属性被设置为适当的值。

语法:

plot(kind='pie', startangle)

例子:在饼状图中设置一个起始角度

import pandas as pd
  
# DataFrame of each student and the votes they get
dataframe = pd.DataFrame({'Name': ['Aparna', 'Aparna', 'Aparna', 
                                   'Aparna', 'Aparna', 'Juhi',
                                   'Juhi', 'Juhi', 'Juhi', 'Juhi',
                                   'Suprabhat', 'Suprabhat', 
                                   'Suprabhat', 'Suprabhat', 
                                   'Suprabhat'],
                          'votes_of_each_class': [12, 9, 17, 19,
                                                  20, 11, 15, 12, 
                                                  9, 4, 22, 19, 17, 
                                                  19, 18]})
  
  
# Plotting the pie chart for above dataframe
# and rotating the pie chart by 60 degrees
dataframe.groupby(['Name']).sum().plot(
    kind='pie', y='votes_of_each_class', autopct='%1.0f%%', startangle=60)

输出 :

如何从Pandas数据框架中创建饼图?

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程