Loading...

Matplotlib

Comprehensive library for creating static, animated, and interactive visualizations

Core Scientific Python Stack Essential Core Library
Quick Info
  • Category: Core Scientific Python Stack
  • Level: Essential
  • Type: Core Library
  • Requires:

Why We Recommend Matplotlib

Matplotlib is the foundational plotting library for Python, offering complete control over every aspect of your figures. It's publication-ready out of the box and integrates seamlessly with NumPy and Pandas, making it essential for visualizing research data.

Common Use Cases

  • Create publication-quality figures for papers
  • Visualize neural signals (spike rasters, LFPs, calcium traces)
  • Plot behavioral data and experimental results
  • Generate time series, histograms, heatmaps, and more

Getting Started

Matplotlib is a comprehensive library for creating static, animated, and interactive visualizations in Python. It provides MATLAB-like plotting while giving you complete control over every element of your figures.

Why Matplotlib?

  • Publication Ready: High-quality figures suitable for papers
  • Complete Control: Fine-tune every aspect of your plots
  • Versatile: Line plots, scatter plots, bar charts, heatmaps, 3D plots, and more
  • Integration: Works seamlessly with NumPy and Pandas
  • Standard: The foundation for many other plotting libraries

Key Features

Basic Plotting

import matplotlib.pyplot as plt
import numpy as np

# Simple line plot
x = np.linspace(0, 10, 100)
y = np.sin(x)

plt.figure(figsize=(10, 4))
plt.plot(x, y)
plt.xlabel('Time (s)')
plt.ylabel('Amplitude')
plt.title('Sine Wave')
plt.show()

Multiple Subplots

fig, axes = plt.subplots(2, 2, figsize=(10, 8))

axes[0, 0].plot(x, np.sin(x))
axes[0, 1].plot(x, np.cos(x))
axes[1, 0].hist(np.random.randn(1000))
axes[1, 1].scatter(x, np.random.randn(100))

Common Plot Types

  • Line plots: Time series, signals
  • Scatter plots: Correlations, relationships
  • Histograms: Distributions
  • Bar charts: Comparisons across conditions
  • Heatmaps: Matrix data, correlation matrices
  • Raster plots: Spike trains
  • Error bars: Show variability

Common Use Cases in Neuroscience

  • Electrophysiology: Plot LFPs, spike rasters, PSTHs
  • Calcium Imaging: Visualize fluorescence traces
  • Behavioral Analysis: Show reaction times, accuracy across conditions
  • Spectrograms: Time-frequency representations
  • Connectivity: Visualize correlation matrices

Getting Started

Install Matplotlib:

conda install matplotlib
# or
pip install matplotlib

Basic example:

import matplotlib.pyplot as plt
import numpy as np

# Generate sample neural data
time = np.linspace(0, 2, 1000)
signal = np.sin(2 * np.pi * 10 * time) + 0.5 * np.random.randn(1000)

# Create figure
plt.figure(figsize=(12, 4))
plt.plot(time, signal, linewidth=0.5)
plt.xlabel('Time (s)')
plt.ylabel('Voltage (mV)')
plt.title('Simulated LFP Signal')
plt.grid(alpha=0.3)
plt.tight_layout()
plt.savefig('lfp_signal.png', dpi=300)
plt.show()

Tips

  • Use plt.style.use('seaborn') for nicer default styles
  • Always label axes and add titles for clarity
  • Use plt.tight_layout() to prevent overlapping labels
  • Save high-resolution figures with plt.savefig('figure.png', dpi=300)
  • Create reusable figure templates for consistency
  • Learn to use fig and ax objects for more control
  • Use colormaps thoughtfully (consider colorblind-friendly options)

Prerequisites

Top