How to have actual values in Matplotlib Pie Chart displayed?

To display actual values in a Matplotlib pie chart, you can use a custom autopct function that converts percentages back to the original values. This is useful when you want to show raw numbers instead of just percentages.

Basic Example with Actual Values

Here's how to display the actual values in your pie chart ?

import matplotlib.pyplot as plt

# Set figure size
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

# Data for the pie chart
labels = ('Read', 'Eat', 'Sleep', 'Repeat')
fracs = [5, 3, 4, 1]
total = sum(fracs)
explode = (0, 0.05, 0, 0)

# Create pie chart with actual values
plt.pie(fracs, explode=explode, labels=labels,
        autopct=lambda p: '{:.0f}'.format(p * total / 100),
        shadow=True, startangle=90)

plt.title('Daily Activities (Actual Values)')
plt.show()

Displaying Both Values and Percentages

You can also show both the actual values and percentages together ?

import matplotlib.pyplot as plt

labels = ('Study', 'Work', 'Exercise', 'Entertainment')
values = [8, 6, 2, 4]
total = sum(values)

# Function to display both value and percentage
def make_autopct(values):
    def my_autopct(pct):
        absolute = int(round(pct/100.*sum(values)))
        return '{:.1f}%\n({:d})'.format(pct, absolute)
    return my_autopct

plt.figure(figsize=(8, 6))
plt.pie(values, labels=labels, autopct=make_autopct(values),
        startangle=90, colors=['#ff9999', '#66b3ff', '#99ff99', '#ffcc99'])

plt.title('Time Distribution (Hours per Day)')
plt.show()

Custom Formatting Options

Here are different ways to format the displayed values ?

import matplotlib.pyplot as plt

data = [25, 15, 30, 20]
labels = ['Product A', 'Product B', 'Product C', 'Product D']

fig, axes = plt.subplots(1, 3, figsize=(15, 5))

# Method 1: Only actual values
axes[0].pie(data, labels=labels, 
           autopct=lambda pct: f'{pct * sum(data) / 100:.0f}')
axes[0].set_title('Actual Values Only')

# Method 2: Values with currency
axes[1].pie(data, labels=labels,
           autopct=lambda pct: f'${pct * sum(data) / 100:.0f}K')
axes[1].set_title('Values with Currency')

# Method 3: Percentage and values
axes[2].pie(data, labels=labels,
           autopct=lambda pct: f'{pct:.1f}%\n({pct * sum(data) / 100:.0f})')
axes[2].set_title('Both Percentage and Values')

plt.tight_layout()
plt.show()

Comparison of Display Methods

Method Code Display
Percentages only autopct='%1.1f%%' 25.0%
Actual values autopct=lambda p: f'{p * total / 100:.0f}' 25
Both autopct=lambda p: f'{p:.1f}%\n({p * total / 100:.0f})' 25.0%
(25)

Conclusion

Use lambda functions with autopct to display actual values in pie charts. The key is converting percentages back to original values using p * total / 100. You can customize the format to show currency, units, or combine with percentages.

Updated on: 2026-03-25T23:14:37+05:30

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements