Python Matplotlib Plotting – A Complete Guide

Last updated 4 weeks, 1 day ago | 101 views 75     5

Matplotlib is the cornerstone of data visualization in Python. Its core plotting capabilities allow users to visualize data as line plots, bar charts, histograms, scatter plots, and more. This guide walks you through the fundamentals of plotting with Matplotlib, focusing on its most important interface: matplotlib.pyplot.


Why Use Matplotlib for Plotting?

  • Simple and consistent interface (pyplot)

  • Highly customizable

  • Supports multiple backends (interactive, static, PDF, PNG)

  • Integrated with NumPy and pandas

  • Can export to many file formats


Installation

You can install Matplotlib with pip:

pip install matplotlib

In a Jupyter notebook:

%matplotlib inline

✅ Getting Started with pyplot

Import the pyplot module:

import matplotlib.pyplot as plt

Line Plot – The Most Basic Plot

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

plt.plot(x, y)
plt.title("Basic Line Plot")
plt.xlabel("X Values")
plt.ylabel("Y Values")
plt.grid(True)
plt.show()

Explanation

  • plt.plot(x, y): Creates a line connecting the (x, y) points.

  • plt.title(), xlabel(), and ylabel(): Add annotations.

  • plt.grid(True): Adds grid lines for readability.


Types of Plots in Matplotlib

1. Scatter Plot

Used to show the relationship between two variables.

x = [1, 2, 3, 4]
y = [10, 20, 25, 30]

plt.scatter(x, y, color='red', marker='x')
plt.title("Scatter Plot")
plt.show()

2. Bar Chart

Great for comparing categories.

labels = ['A', 'B', 'C']
values = [5, 7, 3]

plt.bar(labels, values, color='skyblue')
plt.title("Bar Chart")
plt.show()

3. Histogram

Displays distribution of a dataset.

data = [1,1,2,2,2,3,3,4,4,4,5,5,5,5]

plt.hist(data, bins=5, color='orange', edgecolor='black')
plt.title("Histogram")
plt.xlabel("Value")
plt.ylabel("Frequency")
plt.show()

4. Pie Chart

Shows proportions of categories.

labels = ['Python', 'Java', 'C++', 'Ruby']
sizes = [45, 30, 15, 10]

plt.pie(sizes, labels=labels, autopct='%1.1f%%', startangle=90)
plt.axis('equal')  # Makes it a circle
plt.title("Language Popularity")
plt.show()

Customizing Plots

Changing Line Styles, Colors, and Markers

plt.plot(x, y, color='green', linestyle='--', marker='o')

Adding Legends

plt.plot(x, y, label='Growth')
plt.legend()

Adjusting Figure Size

plt.figure(figsize=(8, 4))
plt.plot(x, y)
plt.show()

Using Subplots

plt.subplot(1, 2, 1)
plt.plot(x, y)
plt.title("Plot 1")

plt.subplot(1, 2, 2)
plt.bar(labels, values)
plt.title("Plot 2")

plt.tight_layout()
plt.show()

Saving Plots

plt.savefig("my_plot.png", dpi=300, bbox_inches='tight')

Save before plt.show() to ensure the plot is not cleared before saving.


✅ Complete Multi-Plot Example

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y1 = [1, 4, 9, 16, 25]
y2 = [2, 4, 6, 8, 10]

plt.figure(figsize=(10, 5))

# First plot
plt.subplot(1, 2, 1)
plt.plot(x, y1, label='y = x^2', color='blue', marker='o')
plt.title("Quadratic")
plt.xlabel("x")
plt.ylabel("y")
plt.grid(True)
plt.legend()

# Second plot
plt.subplot(1, 2, 2)
plt.plot(x, y2, label='y = 2x', color='red', linestyle='--')
plt.title("Linear")
plt.xlabel("x")
plt.ylabel("y")
plt.grid(True)
plt.legend()

plt.tight_layout()
plt.savefig("multi_plot_example.png", dpi=300)
plt.show()

Tips for Effective Plotting

Tip Why It Matters
Use tight_layout() Avoid overlapping elements in subplots
Always label axes and title Increases clarity
Use grid lines Helps interpret values
Export in high DPI Useful for reports and publications
Use colors and markers sparingly Avoid clutter and confusion

⚠️ Common Pitfalls

Pitfall Fix
Forgetting plt.show() Call it at the end to render the plot
Overlapping plots in loops Use plt.clf() or plt.figure()
Saving plots after plt.show() Save before showing the plot
Hard-to-read ticks/labels Use plt.xticks() and plt.yticks() to customize
Ignoring aspect ratio Use plt.axis('equal') for accurate proportions (e.g., in pie charts)

Advanced Topics to Explore Next

  • Object-Oriented API (using Figure and Axes)

  • Interactive Plots with widgets or mpl_interactions

  • 3D Plots using mpl_toolkits.mplot3d

  • Animations with matplotlib.animation

  • Seaborn & Plotly for advanced statistical and interactive plots


Summary

Feature Function
Line plot plt.plot()
Bar chart plt.bar()
Scatter plot plt.scatter()
Histogram plt.hist()
Pie chart plt.pie()
Multiple plots plt.subplot()
Save figure plt.savefig()
Customize layout plt.figure(), plt.grid(), plt.legend()

Conclusion

Matplotlib’s plotting capabilities give you full control over how your data is visualized. From simple line plots to complex subplot layouts, understanding how to use Matplotlib's plotting functions effectively is a crucial skill for anyone working in data science, engineering, or research.