Python Plotting Guide: Data Visualization with Matplotlib

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Getting Started with Plotting in Python

Data visualization is a key tool for analyzing information. Graphs help you quickly spot trends, detect anomalies, and make informed decisions. Python offers a wide range of libraries for creating visualizations.

Why Use Graphs in Python

Plotting graphs in Python is essential for tackling these tasks:

  • Rapid analysis of large datasets
  • Clear presentation of experiment and research results
  • Showcasing data to clients and colleagues
  • Making reports and dashboards more informative
  • Uncovering hidden patterns in data
  • Building interactive visualizations for web apps

Overview of Popular Visualization Libraries

Among the most in-demand tools for plotting in Python are:

Matplotlib - A foundational visualization library with a low learning curve. It gives you full control over the look of your graphs.

Pyplot - A module within Matplotlib for quickly creating standard plots. Known for its simplicity.

Seaborn - A library for statistical visualization that builds on Matplotlib. It has a medium learning curve.

Plotly - A tool for creating interactive graphs with a medium learning curve.

Pandas Plot - Built-in visualization capabilities from DataFrames with a low learning curve.

Working with Matplotlib and the Pyplot Module

Installing and Importing the Library

To get started with Matplotlib, install the library:

pip install matplotlib

Then import the necessary modules:

import matplotlib.pyplot as plt

Creating a Simple Line Plot

Here's a basic example of building a line plot:

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]

plt.plot(x, y)
plt.title("Simple Line Plot")
plt.xlabel("X Axis")
plt.ylabel("Y Axis")
plt.show()

The result shows a linear relationship between the X and Y variables.

Customizing Plot Style and Appearance

To improve your plot's look, use various parameters:

plt.plot(x, y, color='green', linestyle='--', marker='o')
plt.title("Customized Plot")
plt.grid(True)
plt.show()

Key customization parameters:

  • color - line color
  • linestyle - line style (-- dashed, -. dash-dot, : dotted)
  • marker - marker for data points (o, s, ^, v)
  • linewidth - line thickness
  • alpha - transparency

Creating Bar and Pie Charts

Building a Bar Chart

Bar charts are great for comparing values across categories:

categories = ['A', 'B', 'C', 'D']
values = [10, 24, 36, 40]

plt.bar(categories, values, color='skyblue')
plt.title("Bar Chart")
plt.xlabel("Categories")
plt.ylabel("Values")
plt.show()

Creating a Pie Chart

Pie charts show each category's share of the whole:

sizes = [40, 30, 20, 10]
labels = ['Python', 'Java', 'C++', 'Ruby']

plt.pie(sizes, labels=labels, autopct='%1.1f%%', startangle=140)
plt.title("Pie Chart")
plt.axis('equal')  # ensures a circular shape
plt.show()

Pie chart parameters:

  • autopct - format for displaying percentages
  • startangle - starting angle for the first slice

Blogs

Book Recommendations