0% found this document useful (0 votes)
6 views10 pages

Python TP04

This document is a practical guide for beginners on using Matplotlib for plotting data in Python. It covers the importance of plotting, how to install Matplotlib, and provides examples of creating bar and scatter plots with detailed instructions on labeling and styling. Additionally, it includes a student exercise to create their own plots and submission requirements.

Uploaded by

ayaadjabbar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views10 pages

Python TP04

This document is a practical guide for beginners on using Matplotlib for plotting data in Python. It covers the importance of plotting, how to install Matplotlib, and provides examples of creating bar and scatter plots with detailed instructions on labeling and styling. Additionally, it includes a student exercise to create their own plots and submission requirements.

Uploaded by

ayaadjabbar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

TP 04 — Plotting with Matplotlib

Practical Guide for Beginners: from data to clear visuals

Why plotting matters (in one minute)


Plotting is a way to see data instead of only reading numbers. A good plot can reveal patterns instantly: trends, comparisons, errors,
and outliers. In architecture and design work, plots are useful for quick checks such as areas by room, budget by component,
comfort ranges, or options comparison.

The library
We will use Matplotlib, the most common plotting library in Python. You will learn:
• how a plot is built (figure → axes → plot / bar / scatter),
• how to label and style plots (title, axes, grid, annotations),
• how to save a plot as an image.
Installation (choose one method)
Option A — Anaconda (recommended for beginners)
Matplotlib is usually already installed. If not:
conda install matplotlib
Option B — Standard Python (pip)

python -m pip install matplotlib


Quick check (inside Python)

import matplotlib
print(matplotlib.__version__)

2
How Matplotlib works (mental model)
Think of a plot as:
• Figure = the canvas (the full page),
• Axes = the plotting area (where data appears),
• Artists = lines, bars, points, text, labels.
A clean beginner structure:
import [Link] as plt

fig, ax = [Link]()
[Link]([1, 2, 3], [10, 20, 15])
ax.set_title("My first plot")
ax.set_xlabel("x")
ax.set_ylabel("y")
[Link](True)
[Link]()

3
Example 1 — Room areas (bar chart that looks great)
Goal: compare room areas quickly (a classic architecture dataset).
Plot type: bar chart with labels + a highlighted threshold line.

import [Link] as plt

# Simple dataset (areas in m^2)


rooms = ["Living", "Bedroom", "Kitchen", "Bath", "Hall"]
areas = [28, 14, 10, 4, 6]

threshold = 12 # minimum target area (example)

fig, ax = [Link](figsize=(10, 5))

bars = [Link](rooms, areas, edgecolor="black")

# Title + labels
ax.set_title("Room Areas (m^2)")
ax.set_xlabel("Room")

4
ax.set_ylabel("Area (m^2)")

# Grid for readability


[Link](axis="y", linestyle="--", alpha=0.4)

# Threshold line
[Link](threshold, linestyle="--", linewidth=2)
[Link](len(rooms)-1, threshold + 0.4, "Target", ha="right")

# Value labels on top of bars


for b in bars:
height = b.get_height()
[Link](b.get_x() + b.get_width()/2, height + 0.3,
f"{height}", ha="center", va="bottom")

plt.tight_layout()
[Link]()

5
What students should notice
This plot becomes professional by adding: title, axes labels, grid, and value annotations. These are small details that make the
plot immediately readable.

6
Example 2 — Daylight idea (scatter plot with labels)
Goal: visualize a relationship between window area and daylight score.
Plot type: scatter plot with point labels (simple, visually engaging).

import [Link] as plt

# Toy dataset (beginner-friendly)


rooms = ["Living", "Bedroom", "Kitchen", "Study", "Hall"]
window_area = [6.0, 3.2, 2.5, 2.8, 1.2] # m^2 of windows
daylight = [8.5, 6.2, 5.8, 7.0, 4.5] # score out of 10 (example)

fig, ax = [Link](figsize=(10, 5))

[Link](window_area, daylight, s=140, edgecolor="black")

ax.set_title("Window Area vs Daylight Score")


ax.set_xlabel("Window area (m^2)")
ax.set_ylabel("Daylight score (/10)")
[Link](True, linestyle="--", alpha=0.4)

7
# Label each point with the room name
for i in range(len(rooms)):
[Link](window_area[i] + 0.08, daylight[i] + 0.08, rooms[i])

plt.tight_layout()
[Link]()

Interpretation
Students should read this as: more window area often increases daylight, but not perfectly. A scatter plot is the easiest way to
show relationships between two measurements.

8
Student Work — Your turn (simple, similar to the examples)
Write a Python script that creates two plots using Matplotlib.
Task A — Bar chart (comparison)
You are given a list of building components and their costs (in arbitrary units):
• components = ["Walls","Windows","Doors","Roof","Floor"]
• costs = [420, 180, 90, 260, 310]
Create a bar chart with:
• a clear title,
• axis labels,
• a grid on the y-axis,
• a value label on top of each bar.
Task B — Scatter plot (relationship)
You are given two lists:
• floor_area = [30, 45, 55, 70, 90]
• estimated_occupancy = [2, 3, 4, 5, 7]
Create a scatter plot with:
• a clear title,
• axis labels,
• grid,
• one short annotation near the highest occupancy point.
Bonus (optional): Save one of your plots to a file using:
[Link]("my_plot.png", dpi=200)

9
Submission format
Submit:
• one Python file: tp04_plotting.py
• screenshots or saved images of your two plots (.png).

10

You might also like