📜  如何从 Pandas DataFrame 创建饼图?

📅  最后修改于: 2022-05-13 01:55:02.619000             🧑  作者: Mango

如何从 Pandas DataFrame 创建饼图?

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

圆形图中的数据用饼图表示,饼图是图形的一种形式。在研究、工程和商业中,它经常被使用。饼图的各个部分描述了数据的相对强度,是一种数据的图形表示。饼图需要类别和数值变量的列表。短语“馅饼”是指整体,而“切片”是指馅饼的各个组成部分。它分为段和扇区,每个段和扇区代表整个饼图的一部分(百分比)。所有数据加起来是 360 度。馅饼的全部价值始终是 100%。

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

简单的饼图

首先,导入所有必需的模块并初始化数据框。要绘制饼图,使用 plot()函数并将 kind 属性设置为 pie。

句法:

plot(kind='pie')

示例:一个简单的饼图

Python3
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')


Python3
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%%')


Python3
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)


Python3
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)


Python3
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)


Python3
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)


输出:

饼图百分比

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

句法:

plot(kind='pie', autopct)

示例:向饼图添加百分比

Python3

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%%')

输出:

在饼图中定义颜色

要将颜色添加到饼图,请将颜色属性设置为适当的颜色列表。

句法:

plot(kind='pie', colors)

示例:向饼图添加颜色

Python3

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)

输出:

饼图中的爆炸效果

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

句法:

plot(kind='pie', explode)

示例:分解饼图

Python3

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)

输出 :

饼图中的阴影效果

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

句法:

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

示例:饼图中的阴影效果

Python3

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)

输出:

在饼图中设置起始角度

起始角度意味着我们可以根据我们指定的度数角度旋转饼图。为此,将 startangle 属性设置为适当的值。

句法:

plot(kind='pie', startangle)

示例:在饼图中设置起始角度

Python3

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)

输出 :