# 5 Best Ways to Display Pie Charts in Matplotlib Python

Rate this post

π‘ Problem Formulation: In analytics, representing data visually is as crucial as the analysis itself. Pie charts are a staple for showing proportions in a dataset. Imagine you have data on market share percentages for various tech companies and you want to communicate this information effectively. The desired output is a clear, informative pie chart that allows stakeholders to quickly understand the composition of the market.

## Method 1: Basic Pie Chart

Matplotlib’s `pyplot` module provides a `pie()` function that creates pie charts with a simple list of values. You can label each wedge, specify colors, and explode one or more wedges out from the center for emphasis. This method is straightforward and suitable for quick, basic visualizations.

Here’s an example:

```import matplotlib.pyplot as plt

sizes = [25, 35, 20, 20]
labels = ['Apple', 'Samsung', 'Google', 'Others']

plt.pie(sizes, labels=labels, autopct='%1.1f%%')
plt.axis('equal')  # Equal aspect ratio ensures the pie is drawn as a circle.
plt.show()```

This code generates a simple pie chart displaying the respective market share of different tech companies with their names and percentage values on the chart.

## Method 2: Custom Colors and Explode

Enhancing the basic pie chart, Matplotlib allows customization of colors, and using the explode parameter, some slices can be popped out. This is useful for highlighting specific portions of your data, such as the market leader or fastest-growing segment.

Here’s an example:

```import matplotlib.pyplot as plt

sizes = [25, 35, 20, 20]
labels = ['Apple', 'Samsung', 'Google', 'Others']
colors = ['gold', 'lightblue', 'lightgreen', 'lightcoral']
explode = (0.1, 0, 0, 0)  # Only "explode" the first slice (i.e., 'Apple').

plt.pie(sizes, explode=explode, labels=labels, colors=colors, autopct='%1.1f%%')
plt.axis('equal')
plt.show()```

The output is a more colorful chart with the ‘Apple’ slice detached from the rest, drawing attention to it instantly.

## Method 3: Shadow, Start Angle and Percentage Formatting

Adding a shadow to the pie chart can give it a three-dimensional look, enhancing visual appeal. Furthermore, the starting angle can be tweaked to rotate the chart for better positioning of slices. Custom percentage formatting allows you to control how the numeric values are displayed.

Here’s an example:

```import matplotlib.pyplot as plt

sizes = [25, 35, 20, 20]
labels = ['Apple', 'Samsung', 'Google', 'Others']

plt.pie(sizes, labels=labels, autopct=lambda p: '{:.0f}%'.format(p), startangle=140, shadow=True)
plt.axis('equal')
plt.show()```

This code snippet adjusts the start angle to 140 degrees and adds a shadow effect, making the chart look more engaging and easier to read with rounded percentage values.

## Method 4: Adding a Legend and Title

A legend and title can be added to a pie chart to provide context and information about the dataset, making it self-explanatory. Label placement and aesthetics can be further tweaked to make the chart more reader-friendly.

Here’s an example:

```import matplotlib.pyplot as plt

sizes = [25, 35, 20, 20]
labels = ['Apple', 'Samsung', 'Google', 'Others']

plt.pie(sizes, labels=labels, autopct='%1.1f%%')
plt.axis('equal')
plt.legend(labels, title="Companies:")
plt.title('Tech Company Market Share')
plt.show()```

By adding a title and a legend with a header, the pie chart created by this code snippet becomes a standalone presentation tool.

## Bonus One-Liner Method 5: Displaying a Pie Chart Inline

For quick inline displays, especially within Jupyter notebooks, a one-liner pie chart can be immensely useful. This simple method uses inline magic and a condensed pie function call.

Here’s an example:

```%matplotlib inline
plt.pie([25, 35, 20, 20], labels=['Apple', 'Samsung', 'Google', 'Others'], autopct='%1.1f%%')```

This concise one-liner generates a pie chart directly inline with a Jupyter notebook, perfect for analysis and demonstrations within an interactive environment.

## Summary/Discussion

• Method 1: Basic Pie Chart. Quick setup. Limited visual appeal.
• Method 2: Custom Colors and Explode. Visually attractive. Takes slightly more code.
• Method 3: Shadow, Start Angle, and Percentage Formatting. Enhanced appearance. Start angle and shadows might not be desirable in all contexts.
• Method 4: Adding a Legend and Title. Complete information on display. Requires additional space for title and legend.
• Method 5: Displaying Inline. Convenient for Jupyter notebooks. Limited to interactive Python environments.