Fundamentals 12 min read

Master Matplotlib: From Installation to Advanced Plotting Techniques

This comprehensive Matplotlib tutorial walks you through installing the library, importing pyplot, creating basic and advanced plot types—including line, contour, histogram, bar, and stream plots—customizing fonts, axes, and labels, saving figures, and using subplots to compose multi‑panel visualizations, all with clear code examples.

ITPUB
ITPUB
ITPUB
Master Matplotlib: From Installation to Advanced Plotting Techniques

Introduction

Matplotlib is Python's most popular plotting library, offering a MATLAB‑like API that is ideal for interactive data visualization. The pyplot module is the most commonly used entry point for creating two‑dimensional charts, helping users analyze data and make better decisions.

Installation and Basic Setup

Assuming Python is in your PATH, install Matplotlib with: $ pip install matplotlib Import the library in your script:

import matplotlib.pyplot as plt

Core Plot Types

Line Plot : Simple 2‑D line using plt.plot().

Contour & Pseudocolor : Use pcolormesh() or contour() to display a 2‑D array with colors.

Histogram : plt.hist() returns bin counts and probabilities.

Path : Advanced shapes via matplotlib.path.

Streamplot : Visualize vector fields with plt.streamplot(), customizing color and width.

Bar Chart : Create customizable bar graphs with plt.bar().

Additional examples include ellipses, pie charts, tables, scatter plots, GUI widgets, filled curves, date handling, log plots, legends, TeX notations, native TeX rendering, EEG UI, and XKCD‑style sketches.

Vertical and Horizontal Lines

Draw a vertical line: plt.axvline(x=0, ymin=0, ymax=1, **kwargs) Draw a horizontal line: plt.axhline(y=0, xmin=0, xmax=1, **kwargs) Both functions accept keyword arguments for color, label, line style, etc.

Multiple Lines

Plot several vertical lines by iterating over an array of x‑coordinates:

import matplotlib.pyplot as plt
xpoints = [0.2, 0.4, 0.6]
for p in xpoints:
    plt.axvline(p, label='line: {}'.format(p))
plt.legend()
plt.show()

Similarly, use zip() to pair coordinates with colors for colored lines.

Saving Figures

Save the current figure with plt.savefig(fname, **kwargs), where fname is the filename (path optional) and kwargs can control format, DPI, background, etc.

Subplots

Create multiple plots in one figure using plt.subplot(nrows, ncols, index, **kwargs). Example for a 2×2 grid:

import matplotlib.pyplot as plt
plt.subplot(2,2,1)
plt.plot(x1, y1, color='c')
plt.subplot(2,2,2)
plt.plot(x2, y2, color='m')
plt.subplot(2,2,3)
plt.plot(x3, y3, color='g')
plt.subplot(2,2,4)
plt.plot(x4, y4, color='r')
plt.show()

Customizing Font Size

Adjust the default font size with plt.rc('font', size=30) or matplotlib.pyplot.rc('fontname', **font). The change affects all subsequent text elements.

Axis Limits and Labels

Set axis ranges with plt.xlim([start, end]) and plt.ylim([start, end]). Add axis labels using plt.xlabel('X axis label') and plt.ylabel('Y axis label'), where the second argument can be a font dictionary for size, weight, etc.

Clearing a Plot

Remove all current axes and figures with plt.clf() before starting a new plot.

Conclusion

This tutorial provides a step‑by‑step guide to installing Matplotlib, generating a variety of plot types, customizing appearance, saving outputs, and arranging multiple subplots, equipping readers with the essential tools to create professional visualizations in Python.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonTutorialData visualizationMatplotlibplottingpyplot
ITPUB
Written by

ITPUB

Official ITPUB account sharing technical insights, community news, and exciting events.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.