Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Pie Chart | More Statistical Plots
Ultimate Visualization with Python

Swipe to show menu

book
Pie Chart

Note
Definition

Pie chart is a graph which uses a circle divided into slices (segments) to represent the numerical proportion (percentage distribution) of nominal data.

This chart represents the percentage distribution of the population by region.

Note
Note

Despite being neat, pie charts should mostly be avoided, since they distort the view of the data. A category with a lot of instances will seem even bigger, a category with few instances will seem even smaller.

Pie Chart with Labels

Use the pie() function from the pyplot module to create a pie chart. Its only required parameter is the data (x).

The labels parameter assigns labels to each segment and should be a sequence of strings.

123456
import pandas as pd url = 'https://content-media-cdn.codefinity.com/courses/47339f29-4722-4e72-a0d4-6112c70ff738/population.csv' population_df = pd.read_csv(url) print(population_df)
copy

This DataFrame contains the population of each region.

123456
import matplotlib.pyplot as plt import pandas as pd population_df = pd.read_csv('https://content-media-cdn.codefinity.com/courses/47339f29-4722-4e72-a0d4-6112c70ff738/population.csv') # Creating a pie chart and setting the labels for each region plt.pie(population_df['Population'], labels=population_df['Region']) plt.show()
copy

The pie() function was called with the Series of population data passed as the x parameter, and the Series of region names provided as segment labels using the labels parameter.

Adding Percentages

To display the percentage of each segment, use the autopct parameter in the pie() function. This controls how the percentages are formatted and shown inside the chart.

It accepts either a format string or a function. Here, the focus is on using a format string.

123456789
import matplotlib.pyplot as plt import pandas as pd population_df = pd.read_csv('https://content-media-cdn.codefinity.com/courses/47339f29-4722-4e72-a0d4-6112c70ff738/population.csv') # Setting the label for each region and its percentage plt.pie(population_df['Population'], labels=population_df['Region'], autopct='%1.1f%%') plt.show()
copy

Format String

The string %1.1f%% formats the percentage values shown in the pie chart.

  • %f means the value is a floating-point number;

  • .1 specifies one digit after the decimal point;

  • The double % ensures a percent sign appears in the output.

Note
Study More

If you want to explore more parameters, here is pie() documentation for you.

Task

Swipe to start coding

  1. Use the correct function to create a pie chart.
  2. Use incomes as the data for the pie chart (the first argument).
  3. Set the labels to names via the second argument.
  4. Set the format of the percentage to a floating number with one digit after the decimal point via the third argument.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 4. ChapterΒ 3
We're sorry to hear that something went wrong. What happened?

Ask AI

expand
ChatGPT

Ask anything or try one of the suggested questions to begin our chat

book
Pie Chart

Note
Definition

Pie chart is a graph which uses a circle divided into slices (segments) to represent the numerical proportion (percentage distribution) of nominal data.

This chart represents the percentage distribution of the population by region.

Note
Note

Despite being neat, pie charts should mostly be avoided, since they distort the view of the data. A category with a lot of instances will seem even bigger, a category with few instances will seem even smaller.

Pie Chart with Labels

Use the pie() function from the pyplot module to create a pie chart. Its only required parameter is the data (x).

The labels parameter assigns labels to each segment and should be a sequence of strings.

123456
import pandas as pd url = 'https://content-media-cdn.codefinity.com/courses/47339f29-4722-4e72-a0d4-6112c70ff738/population.csv' population_df = pd.read_csv(url) print(population_df)
copy

This DataFrame contains the population of each region.

123456
import matplotlib.pyplot as plt import pandas as pd population_df = pd.read_csv('https://content-media-cdn.codefinity.com/courses/47339f29-4722-4e72-a0d4-6112c70ff738/population.csv') # Creating a pie chart and setting the labels for each region plt.pie(population_df['Population'], labels=population_df['Region']) plt.show()
copy

The pie() function was called with the Series of population data passed as the x parameter, and the Series of region names provided as segment labels using the labels parameter.

Adding Percentages

To display the percentage of each segment, use the autopct parameter in the pie() function. This controls how the percentages are formatted and shown inside the chart.

It accepts either a format string or a function. Here, the focus is on using a format string.

123456789
import matplotlib.pyplot as plt import pandas as pd population_df = pd.read_csv('https://content-media-cdn.codefinity.com/courses/47339f29-4722-4e72-a0d4-6112c70ff738/population.csv') # Setting the label for each region and its percentage plt.pie(population_df['Population'], labels=population_df['Region'], autopct='%1.1f%%') plt.show()
copy

Format String

The string %1.1f%% formats the percentage values shown in the pie chart.

  • %f means the value is a floating-point number;

  • .1 specifies one digit after the decimal point;

  • The double % ensures a percent sign appears in the output.

Note
Study More

If you want to explore more parameters, here is pie() documentation for you.

Task

Swipe to start coding

  1. Use the correct function to create a pie chart.
  2. Use incomes as the data for the pie chart (the first argument).
  3. Set the labels to names via the second argument.
  4. Set the format of the percentage to a floating number with one digit after the decimal point via the third argument.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 4. ChapterΒ 3
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt