Matplotlib

Create professional data visualizations with automated Matplotlib plotting workflows

Matplotlib is a community skill for creating scientific and data visualizations using the matplotlib library, covering plot types, styling configuration, subplot layouts, annotation placement, and export formatting for publication-quality figure generation.

What Is This?

Overview

Matplotlib provides tools for generating static, animated, and interactive data visualizations in Python. It covers plot types that create line charts, scatter plots, bar charts, histograms, heatmaps, and specialized scientific visualizations, styling configuration that sets colors, fonts, line widths, and visual themes for consistent figure appearance, subplot layouts that arrange multiple plots in grid formations with shared axes and spacing control, annotation placement that adds text labels, arrows, and highlighted regions to direct reader attention, and export formatting that saves figures in PNG, PDF, SVG, and EPS formats at specified resolutions for publication and presentation use. The skill enables researchers and analysts to produce publication-quality figures from data.

Who Should Use This

This skill serves data scientists creating visualization dashboards, researchers generating figures for academic publications, and analysts building automated reporting pipelines with chart generation.

Why Use It?

Problems It Solves

Default matplotlib plots lack the formatting quality needed for publications and presentations. Multi-panel figure layouts require manual spacing and alignment configuration. Annotation positioning on plots needs coordinate system knowledge to place text and arrows correctly. Export settings for different target media require resolution and format configuration.

Core Highlights

Chart builder creates diverse plot types from structured data arrays. Style manager applies consistent visual themes across figure collections. Layout engine arranges subplot grids with flexible axis sharing. Exporter saves figures in multiple formats with configurable resolution.

How to Use It?

Basic Usage

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot\
  as plt
import numpy as np

class FigureBuilder:
  def __init__(
    self,
    style: str
      = 'seaborn-v0_8'
  ):
    plt.style.use(style)

  def line_plot(
    self,
    x, y,
    title: str = '',
    xlabel: str = '',
    ylabel: str = ''
  ) -> plt.Figure:
    fig, ax = (
      plt.subplots(
        figsize=(8, 5)))
    ax.plot(x, y,
      linewidth=2)
    ax.set_title(
      title, fontsize=14)
    ax.set_xlabel(
      xlabel, fontsize=12)
    ax.set_ylabel(
      ylabel, fontsize=12)
    fig.tight_layout()
    return fig

  def save(
    self,
    fig: plt.Figure,
    path: str,
    dpi: int = 300
  ):
    fig.savefig(
      path, dpi=dpi,
      bbox_inches=(
        'tight'))
    plt.close(fig)

Real-World Examples

class PanelFigure:
  def __init__(
    self,
    rows: int = 1,
    cols: int = 2,
    figsize: tuple
      = (12, 5)
  ):
    self.fig, self.axes = (
      plt.subplots(
        rows, cols,
        figsize=figsize))
    if rows == 1\
        and cols == 1:
      self.axes = (
        np.array([
          self.axes]))
    self.axes = (
      self.axes.flatten())

  def add_scatter(
    self,
    panel: int,
    x, y,
    color: str = None,
    label: str = None
  ):
    ax = self.axes[panel]
    ax.scatter(
      x, y, c=color,
      label=label,
      alpha=0.7)
    if label:
      ax.legend()

  def add_bar(
    self,
    panel: int,
    categories:
      list[str],
    values: list[float],
    color: str = None
  ):
    ax = self.axes[panel]
    ax.bar(
      categories,
      values,
      color=color)

  def annotate(
    self,
    panel: int,
    text: str,
    xy: tuple,
    xytext: tuple
  ):
    self.axes[panel]\
      .annotate(
        text, xy=xy,
        xytext=xytext,
        arrowprops=dict(
          arrowstyle='->'))

  def save(
    self,
    path: str,
    dpi: int = 300
  ):
    self.fig\
      .tight_layout()
    self.fig.savefig(
      path, dpi=dpi,
      bbox_inches=(
        'tight'))
    plt.close(self.fig)

Advanced Tips

Use GridSpec for complex subplot layouts that need panels of different sizes spanning multiple rows or columns. Set rcParams at the start of scripts to define default fonts and sizes for consistent styling across all figures. Use the Agg backend for server-side rendering when no display is available.

When to Use It?

Use Cases

Generate a multi-panel figure with scatter plots and bar charts for a research paper. Build an automated report pipeline that produces weekly data visualizations. Create annotated time series plots highlighting significant events in the data.

Related Topics

Data visualization, matplotlib, scientific plotting, Python charts, publication figures, subplot layouts, and statistical graphics.

Important Notes

Requirements

Matplotlib Python package installed. NumPy for data array operations. Display backend or Agg backend for headless rendering.

Usage Recommendations

Do: use tight_layout or constrained_layout to prevent label clipping and overlap in multi-panel figures. Set figure size and DPI appropriate for the target output medium. Close figures after saving to release memory in batch generation.

Don't: use default styling for publication figures without adjusting fonts, colors, and line widths. Create overly complex single figures when multiple focused panels are clearer. Render figures interactively on headless servers without switching to the Agg backend.

Limitations

Interactive features require display backends not available in all deployment environments. Complex animations may have performance limitations with large datasets. Default styling requires significant customization to match specific journal or brand style requirements.