Showing posts with label Python Tutorials. Show all posts
Showing posts with label Python Tutorials. Show all posts

Sunday, August 17

Matplotlib — Beginner to Advanced (Free Google Colab Notebook)

August 17, 2025 0

Matplotlib — Beginner to Advanced (Free Google Colab Notebook)

Tool: Matplotlib (Python plotting) · Level: Beginner → Advanced · Format: Interactive Google Colab

Updated: August 17, 2025

This hands-on Google Colab notebook teaches you Matplotlib from zero to pro—starting with the basics of figures and axes, and moving up to advanced styling, annotations, subplots, 3D charts, date/time plots, animations, and export best practices. Perfect for students, freshers, and working professionals who want to make publication-ready charts.

What You’ll Learn

  • Basics: Figures, axes, line, scatter, bar, histogram, box/violin plots
  • Layout: Subplots, GridSpec, tight_layout, sharex/sharey, twin axes
  • Styling: Titles, labels, ticks, legends, fonts, colors, markers, line styles
  • Advanced: Annotations, arrows, text boxes, secondary axis, broken axes
  • Time Series: Plotting dates/times, formatters, locators, rotating labels
  • Images & 3D: imshow/heatmaps, colorbars, 3D surface/wireframe/scatter
  • Animation: FuncAnimation for GIF/MP4, interactive updates
  • Export: Save high-DPI PNG, SVG, PDF; size control; transparent backgrounds
  • Performance: Large datasets (rasterization, path simplification)
  • Best Practices: Stylesheets, rcParams, reproducible figure factories

Who Is This For?

  • Beginners learning Python who want clean, readable plots
  • Data/ML learners building reports, notebooks, and dashboards
  • Researchers & engineers needing publication-quality figures
  • Freshers polishing portfolio projects with professional visuals

How to Use This Notebook (3 Steps)

  1. Click the blue button above (opens in a new tab).
  2. Go to File → Save a copy in Drive to keep your changes.
  3. Run cells from top to bottom, then tweak parameters to experiment.

Quick Start Code (from the Notebook)

# Quick start: basic line chart
import matplotlib.pyplot as plt

x = [0, 1, 2, 3, 4]
y = [0, 1, 4, 9, 16]

fig, ax = plt.subplots(figsize=(6, 4))
ax.plot(x, y, marker="o", linewidth=2)
ax.set_title("Squares")
ax.set_xlabel("x")
ax.set_ylabel("x^2")
ax.grid(True, alpha=0.3)
plt.show()

Sample: Subplots & Shared Axes

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 2*np.pi, 200)
y1, y2 = np.sin(x), np.cos(x)

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(8, 3), sharex=True, sharey=True)
ax1.plot(x, y1, label="sin")
ax2.plot(x, y2, label="cos", linestyle="--")
ax1.legend(); ax2.legend()
fig.suptitle("Shared Axes Demo")
fig.tight_layout()
plt.show()

Sample: Annotating a Chart

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(-3, 3, 300)
y = np.exp(-x**2)

fig, ax = plt.subplots()
ax.plot(x, y)
peak_x = x[np.argmax(y)]
ax.annotate("Peak",
            xy=(peak_x, y.max()),
            xytext=(peak_x+0.5, y.max()-0.2),
            arrowprops=dict(arrowstyle="->", lw=1.5))
plt.show()

What You’ll Build by the End

  • A reusable figure factory template for consistent styling
  • Publication-ready time-series plots with custom formatters
  • Beautiful multi-panel dashboards with legends and colorbars
  • Animated charts (MP4/GIF) to explain changes over time

Troubleshooting Tips

  • Call plt.tight_layout() (or constrained_layout=True) to fix overlaps.
  • Use fig, ax = plt.subplots() consistently; avoid mixing stateful and OO APIs.
  • For large scatter plots, try rasterized=True and save as PDF/SVG.
  • Use ax.xaxis.set_major_formatter(...) for readable date ticks.

Quick Summary

Topic Matplotlib (Python plotting) — Beginner to Advanced
Format Interactive Google Colab Notebook
Prerequisites Basic Python; Google account
Deliverables Code snippets, templates, animations, export-ready figures
Access Link Open in Google Colab

Have a topic you want added (e.g., ternary plots, polar charts, custom color maps)? Comment below—I’ll include it in the next update.

For more hands-on Python guides and fresher-friendly resources, bookmark this site and follow us on LinkedIn.