🎉 Welcome to PyVerse! Start Learning Today

Basic Data Visualization using Matplotlib

What you'll learn

  • What Matplotlib is and how to install it
  • The "Figure and Axes" mental model
  • How to make line, scatter, bar, and histogram charts
  • How to style charts (titles, labels, colors, legends)
  • How to show multiple plots and save your chart
  • A hands-on mini-project

1) What is Matplotlib?

Matplotlib is a Python library that turns your data into pictures (charts). Pictures help you notice patterns much faster than looking at raw numbers.

Install it (run this in your terminal or command prompt):

pip install matplotlib

Then in Python:

import matplotlib.pyplot as plt

2) The mental model: Figure and Axes

  • Figure: the whole window or image.
  • Axes: the area inside the figure where the actual chart appears (with x/y axes).

Two common ways to plot:

  • Quick (pyplot) style: plt.plot(...)plt.bar(...), etc.
  • Object-Oriented (OO) style: fig, ax = plt.subplots(); ax.plot(...)

For beginners, start with pyplot. We'll show both.

3) Quick start: your first plot

Example: temperatures across a week.

import matplotlib.pyplot as plt days = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"] temps = [18, 20, 21, 19, 22, 24, 23] plt.figure(figsize=(8, 4)) # width, height in inches plt.plot(days, temps, color="tab:blue", marker="o", linewidth=2) plt.title("Daily High Temperature (°C)") plt.xlabel("Day") plt.ylabel("Temperature (°C)") plt.grid(True, alpha=0.3) plt.tight_layout() # fixes layout spacing plt.show()

4) Common chart types

A) Line plot (great for trends over time)

import matplotlib.pyplot as plt months = ["Jan","Feb","Mar","Apr","May","Jun"] subscribers = [120, 150, 180, 220, 260, 300] plt.plot(months, subscribers, marker="o", linestyle="-", color="purple") plt.title("Channel Subscribers Over Time") plt.xlabel("Month") plt.ylabel("Subscribers") plt.show()

B) Scatter plot (great for relationships between two variables)

import matplotlib.pyplot as plt import random random.seed(0) # so results are the same each run hours_studied = list(range(0, 11)) # 0..10 # Fake scores: roughly rise with hours, with some randomness scores = [min(100, max(0, 5*h + 55 + random.randint(-7, 7))) for h in hours_studied] plt.scatter(hours_studied, scores, c="teal", s=60, alpha=0.8) plt.title("Study Hours vs Test Score") plt.xlabel("Hours Studied") plt.ylabel("Score (%)") plt.grid(True, alpha=0.2) plt.show()

C) Bar chart (great for comparing categories)

import matplotlib.pyplot as plt subjects = ["Math", "English", "Science", "History"] grades = [88, 92, 83, 90] bars = plt.bar(subjects, grades, color="tab:orange") plt.title("Grades by Subject") plt.xlabel("Subject") plt.ylabel("Grade") # Add value labels on top of bars for i, bar in enumerate(bars): y = bar.get_height() plt.text(bar.get_x() + bar.get_width()/2, y + 1, str(grades[i]), ha="center") plt.ylim(0, 100) plt.show()

D) Histogram (great for seeing the distribution of values)

import matplotlib.pyplot as plt scores = [65, 70, 68, 72, 75, 80, 85, 88, 90, 92, 94, 95, 78, 83, 87, 89, 66, 74, 77, 81] mean_score = sum(scores) / len(scores) plt.hist(scores, bins=8, color="tab:green", edgecolor="white") plt.axvline(mean_score, color="red", linestyle="--", linewidth=2, label=f"Mean = {mean_score:.1f}") plt.title("Test Score Distribution") plt.xlabel("Score") plt.ylabel("Count") plt.legend() plt.show()

5) Styling tips that make charts clearer

  • Titles and labels: always add plt.titleplt.xlabel, and plt.ylabel.
  • Grid: plt.grid(True, alpha=0.3) helps read values.
  • Colors: use names like "tab:blue", "tab:orange", or hex like "#1f77b4".
  • Markers/lines: marker="o"linestyle="--"linewidth=2.
  • Legends: add label="..." in your plot function, then plt.legend().
  • Axis limits: plt.xlim(min, max)plt.ylim(min, max).
  • Styles: try plt.style.use("ggplot") or plt.style.use("seaborn-v0_8").

Example with legend and style:

import matplotlib.pyplot as plt plt.style.use("ggplot") x = [1, 2, 3, 4, 5] y1 = [2, 3, 2, 5, 4] y2 = [1, 2, 4, 4, 5] plt.plot(x, y1, marker="o", label="Group A") plt.plot(x, y2, marker="s", label="Group B") plt.title("Two Groups Over Time") plt.xlabel("Time") plt.ylabel("Value") plt.legend() plt.tight_layout() plt.show()

6) Multiple plots in one figure (subplots) + saving to file

import matplotlib.pyplot as plt days = ["Mon","Tue","Wed","Thu","Fri","Sat","Sun"] steps = [4200, 6300, 8000, 5600, 9000, 10000, 7600] sleep = [7.5, 6.8, 8.2, 7.0, 8.5, 9.0, 7.8] fig, axes = plt.subplots(1, 2, figsize=(10, 4)) # 1 row, 2 columns # Left: line plot axes[0].plot(days, steps, marker="o", color="tab:blue") axes[0].set_title("Daily Steps") axes[0].set_ylabel("Steps") axes[0].grid(True, alpha=0.3) # Right: bar chart axes[1].bar(days, sleep, color="tab:purple") axes[1].set_title("Sleep Hours") axes[1].set_ylabel("Hours") fig.suptitle("Weekly Health Snapshot", fontsize=14) fig.tight_layout() # Save to file (PNG) with good quality fig.savefig("weekly_snapshot.png", dpi=200) plt.show()

7) The OO pattern in one glance (nice habit as you advance)

import matplotlib.pyplot as plt fig, ax = plt.subplots() ax.plot([1,2,3], [3,1,4], marker="o") ax.set_title("OO API Example") ax.set_xlabel("X") ax.set_ylabel("Y") plt.show()

Practical Project: Build a "Weekly Life Dashboard"

Goal: Visualize a week of your life: steps, sleep, and how you spent your time.

What you'll build

  • Subplot 1: Line chart of steps per day with average line and max-day annotation
  • Subplot 2: Bar chart of sleep hours with an 8-hour goal line
  • Subplot 3: Pie chart of time spent (study, sports, screen, other)

Use your own real data for the past 7 days if possible. If not, start with the sample below.

import matplotlib.pyplot as plt # 1) Data (replace with your real week) days = ["Mon","Tue","Wed","Thu","Fri","Sat","Sun"] steps = [4200, 6300, 8000, 5600, 9000, 10000, 7600] sleep = [7.5, 6.8, 8.2, 7.0, 8.5, 9.0, 7.8] # Pie data: total hours in the week by activity (make sure they sum to ~112 awake hours: 16h/day * 7 = 112) time_spent = { "Study": 20, "Sports": 10, "Screen": 30, "Hobbies": 18, "Other": 34 } # 2) Prepare some stats avg_steps = sum(steps) / len(steps) max_steps = max(steps) max_day_index = steps.index(max_steps) max_day = days[max_day_index] # 3) Create the figure with 3 subplots fig, axes = plt.subplots(1, 3, figsize=(14, 4)) # Subplot 1: Steps line with average line and annotation axes[0].plot(days, steps, marker="o", color="tab:blue", label="Steps") axes[0].axhline(avg_steps, color="orange", linestyle="--", label=f"Avg = {avg_steps:.0f}") axes[0].set_title("Steps Per Day") axes[0].set_ylabel("Steps") axes[0].grid(True, alpha=0.3) # Annotate the max day axes[0].annotate( f"Max: {max_steps}", xy=(max_day_index, max_steps), xytext=(max_day_index, max_steps + 1200), arrowprops=dict(arrowstyle="->", color="gray"), ha="center" ) axes[0].legend() # Subplot 2: Sleep bar with goal line bars = axes[1].bar(days, sleep, color="tab:purple", label="Sleep") axes[1].axhline(8, color="green", linestyle="--", label="Goal: 8h") axes[1].set_title("Sleep Hours") axes[1].set_ylabel("Hours") # Add labels on bars for bar in bars: y = bar.get_height() axes[1].text(bar.get_x() + bar.get_width()/2, y + 0.1, f"{y:.1f}", ha="center", fontsize=8) axes[1].legend() # Subplot 3: Time spent pie chart labels = list(time_spent.keys()) values = list(time_spent.values()) axes[2].pie(values, labels=labels, autopct="%1.0f%%", startangle=90) axes[2].set_title("Weekly Time Spent (Awake)") fig.suptitle("My Weekly Life Dashboard", fontsize=16) fig.tight_layout() fig.savefig("weekly_life_dashboard.png", dpi=220) plt.show()

Ideas to extend

  • Add a fourth subplot: histogram of steps to see distribution.
  • Color any sleep day below 7 hours in red.
  • Turn on a global style: plt.style.use("ggplot") at the top.
  • Read real data from a CSV using Python's csv module and plot it.

Summary

  • Matplotlib turns lists of numbers into charts you can understand quickly.
  • Remember: Figure (whole image) and Axes (plot area).
  • Start with simple chart types: line, scatter, bar, histogram.
  • Always add labels, titles, and grid; use legends for multiple series.
  • Use subplots to compare different views of your data in one figure.
  • Save your work with fig.savefig("name.png", dpi=300) for high quality.
  • Practice by visualizing data from your own life—patterns will jump out!

Loading quizzes...