GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.tutorialspoint.com — Python Pandas - Pie Plot
Selected Reading
Python Pandas - Pie PlotA 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.
SyntaxFollowing is the syntax of the plot.pie() method for both the Series and DataFrames objects − Series.plot.pie(**kwargs) Where,
Example: Creating Pie Plot for Series DataThis 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()
OutputFollowing 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()
OutputAfter 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()
OutputFollowing 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() OutputFollowing 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()
OutputOn executing the above code we will get the following output −
python_pandas_visualization.htm Advertisements
|
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |