
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How to display pie charts in Matplotlib Python?
Matplotlib is a popular Python package that is used for data visualization. Visualizing data is a key step since it helps understand what is going on in the data without actually looking at the numbers and performing complicated computations. It helps in communicating the quantitative insights to the audience effectively.
Matplotlib is used to create 2 dimensional plots with the data. It comes with an object oriented API that helps in embedding the plots in Python applications. Matplotlib can be used with IPython shells, Jupyter notebook, Spyder IDE and so on.
It is written in Python. It is created using Numpy, which is the Numerical Python package in Python.
Python can be installed on Windows using the below command −
pip install matplotlib
The dependencies of Matplotlib are −
Python ( greater than or equal to version 3.4) NumPy Setuptools Pyparsing Libpng Pytz Free type Six Cycler Dateutil
Pie chart, the name is such due to the shape of the plot. It is in the form of circle, with parts separated like a ‘pie’. It shows data as percentages.
Let us understand how Matplotlib can be used to create a pie plot −
Example
import matplotlib.pyplot as plt labels = 'Label_1', 'Label_2', 'Label_3' sizes = [10, 34, 56] explode = (0, 0.1, 0) fig1, ax1 = plt.subplots() ax1.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%', shadow=True, startangle=90) ax1.axis('equal') plt.show()
Output
Explanation
The required packages are imported and its alias is defined for ease of use.
The labels for the pie chart and the size of every ‘pie’ in the pie chart is defined.
An empty figure is created using the ‘figure’ function.
The ‘subplot’ function is used to create an area to plot the graph.
The data is plotted using the ‘plot’ function.
The set_xlabel, set_ylabel and set_title functions are used to provide labels for ‘X’ axis, ‘Y’ axis and title.
The plot is defined as pie chart by specifying ‘pie’.
It is shown on the console using the ‘show’ function.
- Related Articles
- Plotting Pandas DataFrames in Pie Charts using Matplotlib
- How to create waffle charts in Python Matplotlib?
- How to plot pie charts as subplots with custom size in Python Plotly?
- Python - Plotting Pie charts in excel sheet using XlsxWriter module
- How to plot pie-chart with a single pie highlighted with Python Matplotlib?
- How to remove the label on the left side in matplotlib.pyplot pie charts?
- How to plot a nested pie chart in Matplotlib?
- How to have actual values in Matplotlib Pie Chart displayed?
- How to add group labels for bar charts in Matplotlib?
- How to add a legend to a Matplotlib pie chart?
- How to display stacked bar chart using matplotlib in Python?
- How to Add Legends to charts in Python?
- How can bubble charts be created using Matplotlib?
- Python - Plot a Pie Chart for Pandas Dataframe with Matplotlib?
- How to avoid overlapping of labels & autopct in a Matplotlib pie chart?
