A pie plot is a circular chart that shows data as slices, with each slice representing a portion of the dataset. It is useful for visualizing the relative sizes of different data values. In a pie chart, the size of each slice, including its angle, area, and arc length, corresponds to the value or percentage it represents. The total of all percentages equals 100%, and the sum of all slice angles is 360.
For instance, consider a dataset that records the pass percentages of boys and girls in a classroom. A pie plot visually represents this data, where each slice indicates the proportion of boys and girls passing.
In this tutorial, we will learn how to create and customize pie plots in Python using the Pandas library.
Pie Plot in PandasIn Pandas, pie plot can be created using the plot.pie() method for both the Series and DataFrames objects. This method internally use Matplotlib and return either a matplotlib.axes.Axes object or NumPy array np.ndarray of Axes when subplots parameter is set to True.
DataFrame.plot.pie(): Creates pie plot for one or more columns in a DataFrame.
Series.plot.pie(): Creates a pie plot for a specific column or Series.
Following is the syntax of the plot.pie() method for both the Series and DataFrames objects −
Series.plot.pie(**kwargs)
Where,
y: Specify the column label or position to plot. Required when using DataFrame.plot.pie() without subplots=True.
**kwargs: Additional keyword arguments to customize the plot appearance.
This example demonstrates using the Series.plot.pie() method on a pandas Series object to generate the pie plot.
import pandas as pd import numpy as np import matplotlib.pyplot as plt # Create a Pandas Series series = pd.Series(3 * np.random.rand(4), index=["a", "b", "c", "d"]) # Generate a pie plot series.plot.pie(figsize=(7, 4)) # Set title and Display the plot plt.title('Basic Pie Plot') plt.show()
Following is the output of the above code −
Example: Creating Pie Plot for a DataFrameThis example uses the DataFrame.plot.pie() method for the creating the pie plot.
import pandas as pd import matplotlib.pyplot as plt # Create a DataFrame with pass percentages data = {"Category": ["Boys", "Girls"], "Pass Percentage": [75, 85]} df = pd.DataFrame(data) # Plotting the pie chart df.plot.pie(y="Pass Percentage", labels=df["Category"], figsize=(7, 4)) # Set title and Display the plot plt.title('Pie Plot for DataFrame') plt.show()
After executing the above code, we get the following output −
Customizing a Pie PlotPandas allows customization of pie plots through various parameters such as, labels, colors, autopct, fontsize, and more.
ExampleThis example demonstrates customizing the pie plot using the addition keyword arguments.
import pandas as pd import matplotlib.pyplot as plt # Create a DataFrame with pass percentages data = { "Category": ["Boys", "Girls"], "Pass Percentage": [75, 85] } df = pd.DataFrame(data) # Plotting the pie chart df.plot.pie(y="Pass Percentage", labels=df["Category"], autopct="%.1f%%", colors=["skyblue", "pink"], figsize=(7, 4)) # Set title and plt.title('Customizing a Pie Plot') plt.show()
Following is the output of the above code −
Pie Plots for All DataFrame ColumnsWhen plotting a pie plot from a DataFrame, you must specify a column using the y parameter or use subplots=True to plot multiple pie charts for all numerical columns.
ExampleThis example demonstrates creating pie plot for all DataFrame columns in a multiple subplots.
import pandas as pd import matplotlib.pyplot as plt import numpy as np # Create a DataFrame df = pd.DataFrame(3 * np.random.rand(4, 2), index=["a", "b", "c", "d"], columns=["Column1", "Column2"]) # Plotting the pie chart df.plot.pie(subplots=True, autopct="%.1f%%", figsize=(7, 4), title='Pie Plots for All Columns') # Display the Plot plt.show()
Following is the output of the above code −
Pie Plot Handling NaN ValuesPandas plot.pie() method easily handles the missing values (NaN), if your data contains any. The method automatically fills the missing data with 0 when creating a pie plot.
ExampleThe following example shows how the plot.pie() method handles the missing values.
import pandas as pd import numpy as np import matplotlib.pyplot as plt # Create a Pandas Series series = pd.Series([1, 2, np.nan, 4], index=["A", "B", "C", "D"]) # Generate a pie plot series.plot.pie(figsize=(7, 4)) # Set title and Display the plot plt.title('Pie Plot Handling Missing Values') plt.show()
On executing the above code we will get the following output −
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4