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

Course Content

Ultimate Visualization with Python

Ultimate Visualization with Python

1. Matplotlib Introduction
2. Creating Commonly Used Plots
3. Plots Customization
4. More Statistical Plots
5. Plotting with Seaborn

bookPie Chart

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

This chart represents the percentage distribution of the population by region. Looks pretty neat, doesn’t it?

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.

Now we’ll discuss how to create such a chart in matplotlib.

Pie Chart with Labels

The function we will use is pie() from the pyplot module, and its first and the only required parameter is our data (called x).

Another important parameter is labels which specifies the labels of the segments (it should be a sequence of strings).

Let’s first have a look at the data for our example:

1234
import pandas as pd url = 'https://codefinity-content-media-v2.s3.eu-west-1.amazonaws.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. Now to our example:

123456
import matplotlib.pyplot as plt import pandas as pd population_df = pd.read_csv('https://codefinity-content-media-v2.s3.eu-west-1.amazonaws.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

We called the pie() function passing the Series with population data as x parameter and the Series with region names as the labels for the segments.

Adding Percents

The chart looks alright, but there is still something missing. We don’t know the exact percentage of each individual region. Fortunately, there is an autopct parameter which specifies the format of the labeling of the wedges (the labels are placed inside).

You can either pass a format string or a function, however, we’ll focus here on the format string. Let’s now modify our example:

123456
import matplotlib.pyplot as plt import pandas as pd population_df = pd.read_csv('https://codefinity-content-media-v2.s3.eu-west-1.amazonaws.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

Here we passed the following string: %1.1f%%. f indicates that the value should be treated as a floating-point number (d indicates an integer), and .1 that there should be exactly one digit after the decimal point.

The percent signs specify that it is a format string and that the number should be followed by the percent sign (%).

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

Task

  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.

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
toggle bottom row

bookPie Chart

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

This chart represents the percentage distribution of the population by region. Looks pretty neat, doesn’t it?

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.

Now we’ll discuss how to create such a chart in matplotlib.

Pie Chart with Labels

The function we will use is pie() from the pyplot module, and its first and the only required parameter is our data (called x).

Another important parameter is labels which specifies the labels of the segments (it should be a sequence of strings).

Let’s first have a look at the data for our example:

1234
import pandas as pd url = 'https://codefinity-content-media-v2.s3.eu-west-1.amazonaws.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. Now to our example:

123456
import matplotlib.pyplot as plt import pandas as pd population_df = pd.read_csv('https://codefinity-content-media-v2.s3.eu-west-1.amazonaws.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

We called the pie() function passing the Series with population data as x parameter and the Series with region names as the labels for the segments.

Adding Percents

The chart looks alright, but there is still something missing. We don’t know the exact percentage of each individual region. Fortunately, there is an autopct parameter which specifies the format of the labeling of the wedges (the labels are placed inside).

You can either pass a format string or a function, however, we’ll focus here on the format string. Let’s now modify our example:

123456
import matplotlib.pyplot as plt import pandas as pd population_df = pd.read_csv('https://codefinity-content-media-v2.s3.eu-west-1.amazonaws.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

Here we passed the following string: %1.1f%%. f indicates that the value should be treated as a floating-point number (d indicates an integer), and .1 that there should be exactly one digit after the decimal point.

The percent signs specify that it is a format string and that the number should be followed by the percent sign (%).

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

Task

  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.

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
toggle bottom row

bookPie Chart

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

This chart represents the percentage distribution of the population by region. Looks pretty neat, doesn’t it?

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.

Now we’ll discuss how to create such a chart in matplotlib.

Pie Chart with Labels

The function we will use is pie() from the pyplot module, and its first and the only required parameter is our data (called x).

Another important parameter is labels which specifies the labels of the segments (it should be a sequence of strings).

Let’s first have a look at the data for our example:

1234
import pandas as pd url = 'https://codefinity-content-media-v2.s3.eu-west-1.amazonaws.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. Now to our example:

123456
import matplotlib.pyplot as plt import pandas as pd population_df = pd.read_csv('https://codefinity-content-media-v2.s3.eu-west-1.amazonaws.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

We called the pie() function passing the Series with population data as x parameter and the Series with region names as the labels for the segments.

Adding Percents

The chart looks alright, but there is still something missing. We don’t know the exact percentage of each individual region. Fortunately, there is an autopct parameter which specifies the format of the labeling of the wedges (the labels are placed inside).

You can either pass a format string or a function, however, we’ll focus here on the format string. Let’s now modify our example:

123456
import matplotlib.pyplot as plt import pandas as pd population_df = pd.read_csv('https://codefinity-content-media-v2.s3.eu-west-1.amazonaws.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

Here we passed the following string: %1.1f%%. f indicates that the value should be treated as a floating-point number (d indicates an integer), and .1 that there should be exactly one digit after the decimal point.

The percent signs specify that it is a format string and that the number should be followed by the percent sign (%).

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

Task

  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.

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!

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

This chart represents the percentage distribution of the population by region. Looks pretty neat, doesn’t it?

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.

Now we’ll discuss how to create such a chart in matplotlib.

Pie Chart with Labels

The function we will use is pie() from the pyplot module, and its first and the only required parameter is our data (called x).

Another important parameter is labels which specifies the labels of the segments (it should be a sequence of strings).

Let’s first have a look at the data for our example:

1234
import pandas as pd url = 'https://codefinity-content-media-v2.s3.eu-west-1.amazonaws.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. Now to our example:

123456
import matplotlib.pyplot as plt import pandas as pd population_df = pd.read_csv('https://codefinity-content-media-v2.s3.eu-west-1.amazonaws.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

We called the pie() function passing the Series with population data as x parameter and the Series with region names as the labels for the segments.

Adding Percents

The chart looks alright, but there is still something missing. We don’t know the exact percentage of each individual region. Fortunately, there is an autopct parameter which specifies the format of the labeling of the wedges (the labels are placed inside).

You can either pass a format string or a function, however, we’ll focus here on the format string. Let’s now modify our example:

123456
import matplotlib.pyplot as plt import pandas as pd population_df = pd.read_csv('https://codefinity-content-media-v2.s3.eu-west-1.amazonaws.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

Here we passed the following string: %1.1f%%. f indicates that the value should be treated as a floating-point number (d indicates an integer), and .1 that there should be exactly one digit after the decimal point.

The percent signs specify that it is a format string and that the number should be followed by the percent sign (%).

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

Task

  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.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Section 4. Chapter 3
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
some-alt