TutorialsPythonMatplotlib Basics

Matplotlib Basics

Create line charts, bar charts and scatter plots with Python's core plotting library

Matplotlib is the foundation of Python visualisation. Every other visualisation library (Seaborn, Pandas plot, Plotly) is built on top of it. For data analysts, Matplotlib is used to quickly visualise data while exploring it and to create clean charts for reports. You will use two styles: the plt shortcut style (quick, for notebooks) and the Figure/Axes style (for multiple charts and fine control). Learn both — the plt style for exploration, Figure/Axes for publication-ready charts.

Example

Line, bar, and scatter charts
import matplotlib.pyplot as plt
import pandas as pd

months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]
sales  = [420, 385, 510, 475, 540, 620]  # in thousands

# LINE CHART
plt.figure(figsize=(10, 5))
plt.plot(months, sales, marker="o", color="#c47f00", linewidth=2)
plt.title("Monthly Sales 2026", fontsize=14, fontweight="bold")
plt.xlabel("Month")
plt.ylabel("Sales (₹ thousands)")
plt.grid(True, alpha=0.3)
plt.tight_layout()
plt.savefig("sales_trend.png", dpi=150)
plt.show()

# BAR CHART
plt.figure(figsize=(8, 5))
plt.bar(months, sales, color="#0284c7", edgecolor="white")
plt.title("Sales by Month")
plt.ylabel("₹ (thousands)")
plt.show()

# SCATTER CHART
marketing = [50, 45, 65, 60, 70, 80]
plt.scatter(marketing, sales, color="#7c3aed", s=80, alpha=0.7)
plt.xlabel("Marketing Spend (₹ thousands)")
plt.ylabel("Sales (₹ thousands)")
plt.title("Marketing vs Sales Correlation")
plt.show()
💡 figsize=(width, height) in inches. dpi=150 gives a high-resolution image for presentations. tight_layout() prevents labels from being cut off.

Key Points

  • import matplotlib.pyplot as plt — universal convention
  • plt.figure(figsize=(width, height)) — always set size before plotting
  • plt.savefig("name.png", dpi=150, bbox_inches="tight") saves to file
  • plt.tight_layout() prevents title/label overlap — call before show() or savefig()
  • Pandas has built-in plot: df["Sales"].plot(kind="bar") wraps Matplotlib automatically

Practice Question

Which parameter controls the size of a Matplotlib figure?

Related Topics

Seaborn for Statistical ChartsCreate beautiful distribution, correlation and categorical charts with SeabornExploratory Data Analysis (EDA) WorkflowA systematic 6-step EDA process every data analyst should follow on any new datasetCapstone Project — Sales AnalysisEnd-to-end data analysis project: load, clean, analyse and visualise sales data