AI & ML
0/6 sections
0%

Python: Matplotlib

Matplotlib is Python's foundational plotting library the backbone behind pandas, seaborn, and countless scientific visualizations. Mastering it gives full control over every pixel of a chart.

1Basic Plotting

python
import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 2 * np.pi, 100) y = np.sin(x)

# Simple line plot plt.plot(x, y, color='steelblue', linewidth=2, linestyle='--', label='sin(x)') plt.plot(x, np.cos(x), color='tomato', label='cos(x)') plt.xlabel('x') plt.ylabel('y') plt.title('Trigonometric Functions') plt.legend() plt.grid(True, alpha=0.3) plt.tight_layout() plt.savefig('trig.png', dpi=150, bbox_inches='tight') plt.show() ```

2Chart Types

python
fig, axes = plt.subplots(2, 3, figsize=(15, 10))

# Scatter axes[0, 0].scatter(x, y, c=y, cmap='viridis', alpha=0.6, s=10) axes[0, 0].set_title('Scatter Plot')

# Bar chart categories = ['A', 'B', 'C', 'D'] values = [23, 45, 12, 67] axes[0, 1].bar(categories, values, color='steelblue', edgecolor='black') axes[0, 1].set_title('Bar Chart')

# Histogram data = np.random.normal(0, 1, 1000) axes[0, 2].hist(data, bins=30, color='orange', edgecolor='black', density=True) axes[0, 2].set_title('Histogram')

# Box plot axes[1, 0].boxplot([np.random.normal(0, 1, 100), np.random.normal(2, 1.5, 100)], labels=['Group A', 'Group B'])

# Heatmap matrix = np.random.rand(5, 5) im = axes[1, 1].imshow(matrix, cmap='Blues', aspect='auto') plt.colorbar(im, ax=axes[1, 1])

# Pie chart axes[1, 2].pie(values, labels=categories, autopct='%1.1f%%', startangle=90)

plt.tight_layout() plt.show() ```

3Figure & Axes Architecture

python
# OOP interface full control
fig = plt.figure(figsize=(12, 8))

# Add subplots manually ax1 = fig.add_subplot(2, 2, 1) # 2x2 grid, position 1 ax2 = fig.add_axes([0.1, 0.1, 0.8, 0.35]) # [left, bottom, width, height]

# GridSpec for complex layouts from matplotlib.gridspec import GridSpec gs = GridSpec(2, 3, figure=fig, hspace=0.4, wspace=0.3) ax_main = fig.add_subplot(gs[0, :]) # span full first row ax_left = fig.add_subplot(gs[1, 0]) ax_right = fig.add_subplot(gs[1, 1:]) # span last two cols

ax_main.plot(x, y) ax_main.set_title('Main Plot', fontsize=14, fontweight='bold') ax_main.tick_params(labelsize=10) ax_main.spines['top'].set_visible(False) # clean look ax_main.spines['right'].set_visible(False) ```

4Styling & Customization

python
# Use built-in styles
plt.style.use('seaborn-v0_8-whitegrid')  # clean, publication-ready
plt.style.use('dark_background')          # dark theme

# Custom colors colors = ['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728']

# Annotations fig, ax = plt.subplots() ax.plot(x, y) ax.annotate('Peak', xy=(np.pi/2, 1), xytext=(2, 0.7), arrowprops=dict(arrowstyle='->', color='red'), fontsize=12, color='red')

# Add text ax.text(0.05, 0.95, 'R² = 0.97', transform=ax.transAxes, fontsize=12, verticalalignment='top', bbox=dict(boxstyle='round', facecolor='wheat', alpha=0.5))

# Twin axes (dual y-axis) ax2 = ax.twinx() ax2.plot(x, np.cos(x), color='red', linestyle='--') ax2.set_ylabel('cos(x)', color='red') ```

5Animations

python
from matplotlib.animation import FuncAnimation

fig, ax = plt.subplots() ax.set_xlim(0, 2 * np.pi) ax.set_ylim(-1, 1) line, = ax.plot([], [], lw=2)

def init(): line.set_data([], []) return (line,)

def animate(frame): x = np.linspace(0, 2 * np.pi, 100) y = np.sin(x + frame / 10) line.set_data(x, y) return (line,)

ani = FuncAnimation(fig, animate, frames=100, init_func=init, interval=50, blit=True)

# Save as GIF or MP4 ani.save('wave.gif', writer='pillow', fps=30) ani.save('wave.mp4', writer='ffmpeg', fps=30) plt.show() ```

6Publication-Quality Figures

python
# Configure global rcParams
plt.rcParams.update({
    'font.family': 'sans-serif',
    'font.size': 11,
    'axes.labelsize': 12,
    'axes.titlesize': 13,
    'xtick.labelsize': 10,
    'ytick.labelsize': 10,
    'legend.fontsize': 10,
    'figure.dpi': 150,
    'axes.spines.top': False,
    'axes.spines.right': False,
    'axes.grid': True,
    'grid.alpha': 0.3,
})

# Use context manager for local changes with plt.rc_context({'axes.facecolor': 'lightyellow'}): fig, ax = plt.subplots() ax.plot(x, y)

# Export high-resolution fig.savefig('figure.pdf', dpi=300, bbox_inches='tight', format='pdf') fig.savefig('figure.svg', format='svg', bbox_inches='tight') # vector ```

Explore the Full Portfolio

This is the complete portfolio of Sufi Khan Sulaiman, a technology leader specialising in B2B commerce and digital automation. Start from the Home page for the overview, then move through two decades of career experience across FLIR Systems, Lorex Technology, and 1c Platform, and the full catalogue of project case studies spanning headless commerce migrations, AI recommendation engines, and multi-channel fulfilment systems.

The skills and certifications page maps the technical and leadership capabilities behind the work, while the articles and the knowledge base break down the thinking into actionable frameworks. For hands-on learning, the tutorials and applications sections cover practical builds from front-end fundamentals to full-stack web apps.

For consulting engagement, the expertise page outlines service offerings, the ecommerce hub covers platform architecture and automation strategy, and the ecommerce guide (PDF) is a downloadable 55-page field manual. When you are ready to talk, the contact page is the direct line.