Loading...

Warning: Undefined array key "post_id" in /home/u795416191/domains/speqto.com/public_html/wp-content/themes/specto-fresh/single.php on line 22

Beginner’s Guide to Data Visualization with Matplotlib

By Sumit Pandey

27 Oct, 2025

matplotlib pie chart

Introduction

Data visualization plays an important role in presenting information in a clear and meaningful way.
Pie charts are commonly used to display data as percentages of a whole, making it easier to understand
how different categories compare. In Python, the Matplotlib library enables us to quickly create
visually appealing pie charts with various customization options. In this beginner-friendly guide,
you will learn how to create and customize pie charts step-by-step.

What You Will Learn

  • How to install Matplotlib
  • How to prepare data for pie charts
  • How to create and customize pie charts
  • How to add labels, percentages, and legends

Prerequisites

  • Basic understanding of Python
  • Python 3 installed on your machine
  • Matplotlib library installed

Step 1 — Create a Project Folder

First, create a folder where your code will be stored:

mkdir pie-chart
cd pie-chart

Step 2 — Install Matplotlib

Run the following command in your terminal to install Matplotlib:

pip install matplotlib

Step 3 — Write the Code

Create a file named pie_chart.py and add the following code:

import matplotlib.pyplot as plt

# Data to be plotted
languages = ['Python', 'JavaScript', 'Java', 'C++']
popularity = [40, 25, 20, 15]

# Plotting pie chart
plt.pie(popularity, labels=languages, autopct='%1.1f%%', startangle=90, shadow=True)

# Adding title
plt.title('Programming Language Popularity')

# Display the chart
plt.show()

Step-by-Step Code Explanation

  1. Import Matplotlib: We import Matplotlib’s pyplot module to create charts.
  2. Define data: We create two lists — one for languages and one for their popularity.
  3. plt.pie(): This function draws the pie chart.
  4. autopct: Displays percentage values on each slice.
  5. startangle: Rotates the chart for better orientation.
  6. shadow: Adds a subtle 3D effect.
  7. plt.title: Adds a title above the chart.
  8. plt.show: Displays the chart window.

Step 4 — Run the Script

Execute the script using the following command:

python pie_chart.py

A new window will open, displaying your pie chart.

Customizing Your Pie Chart

Matplotlib allows extensive customization. For example:

  • Custom colors to match your theme
  • Explode to highlight a particular slice
  • Legend to add additional context
colors = ['#ff9999','#66b3ff','#99ff99','#ffcc99']
explode = (0.1, 0, 0, 0)

plt.pie(popularity, labels=languages, colors=colors, explode=explode, autopct='%1.1f%%')
plt.legend()
plt.show()

Real-World Use Cases

Pie charts are useful in various scenarios, such as:

  • Market share analysis
  • Customer segmentation
  • Survey result visualization
  • Budget allocation
  • Election result distribution

Common Errors & Fixes

  • ModuleNotFoundError: Ensure Matplotlib is installed correctly.
  • Empty chart: Make sure your data lists contain values.
  • Wrong pip version: On Linux/Mac, try pip3 install matplotlib.

Conclusion

Pie charts are a simple yet effective way to visualize category-based data. With Matplotlib,
creating and customizing them becomes easy even for beginners. You can now use these skills
to present survey insights, business reports, analytics, and more in a visually appealing manner.

RECENT POSTS

Socket.IO Security Unveiled: Mastering Authentication & Authorization for Robust Real-time Applications

Socket.IO Security Unveiled: Mastering Authentication & Authorization for Robust Real-time Applications Divya Pal 4 February, 2026 In the dynamic landscape of modern web development, real-time applications have become indispensable, powering everything from chat platforms to collaborative editing tools. At the heart of many of these interactive experiences lies Socket.IO, a powerful library enabling low-latency, bidirectional […]

Prisma ORM in Production: Architecting for Elite Performance and Seamless Scalability

Prisma ORM in Production: Architecting for Elite Performance and Seamless Scalability Shubham Anand 16 February 2026 In the rapidly evolving landscape of web development, database interaction stands as a critical pillar. For many modern applications, Prisma ORM has emerged as a powerful, type-safe, and intuitive tool for interacting with databases. However, transitioning from development to […]

Streamlining DevOps: The Essential Guide to Gatling Integration in Your CI/CD Pipeline

Streamlining DevOps: The Essential Guide to Gatling Integration in Your CI/CD Pipeline Megha Srivastava 04 February 2026 In the dynamic landscape of modern software development, the quest for efficiency and reliability is paramount. DevOps practices have emerged as the cornerstone for achieving these goals, fostering seamless collaboration and rapid delivery. Yet, even the most robust […]

Fortifying Your Enterprise: Playwright Best Practices for Unbreakable Test Resilience

Fortifying Your Enterprise: Playwright Best Practices for Unbreakable Test Resilience Megha Srivastava 04 February 2026 In the dynamic landscape of enterprise software development, the quest for robust, reliable, and efficient testing is paramount. As systems grow in complexity, the challenge of maintaining an ironclad testing suite that withstands constant evolution becomes a critical differentiator. This […]

The TanStack Query Revolution: Elevating Your Data Fetching Paradigm from Basic to Brilliant

The TanStack Query Revolution: Elevating Your Data Fetching Paradigm from Basic to Brilliant GAURAV GARG 04 February 2026 In the dynamic landscape of web development, managing server state and data fetching often presents a labyrinth of challenges. From stale data and intricate caching mechanisms to race conditions and manual error handling, developers frequently grapple with […]

POPULAR TAG

POPULAR CATEGORIES