{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "10c909e5",
   "metadata": {},
   "source": "# Week 3 — Wednesday: Choosing the Right Plot\n\n**DATA 202 · Calvin University**\n\nSame dataset as Monday — people experiencing homelessness — now cleaned and ready to visualize. A table of numbers tells you very little on its own; the right chart can reveal a pattern instantly, and the wrong chart can hide or distort it just as fast.\n\n**Today's plan (~50 min of content — class is 65 min, ~15 min goes to the retrieval quiz and announcements):**\n\n| Time | Section |\n|---|---|\n| ~5 min | Quick clean, reload, matching a question to a plot type |\n| ~10 min | Histograms |\n| ~10 min | Scatter plots |\n| ~10 min | Line plots |\n| ~10 min | Bar charts |\n| ~5 min | When charts deceive |\n\nSame two stop-and-check cues as Monday: **🎯 Predict First** (guess before we run the code) and **🙋 Quick Check** (a quick verbal question — no code)."
  },
  {
   "cell_type": "markdown",
   "id": "2deee9c3",
   "metadata": {},
   "source": "---\n## Quick Clean, Then Load\n\nWe'll redo Monday's cleaning in one cell so this notebook stands on its own."
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cd857044",
   "metadata": {},
   "outputs": [],
   "source": "import pandas as pd\nimport plotly.express as px\n\nhomeless = pd.read_csv(\"../../datasets/homeless.csv\")\n\nhomeless[\"city\"] = (\n    homeless[\"city\"].str.strip().str.replace(\"-\", \" \", regex=False)\n    .str.replace(r\"[^a-zA-Z\\s]\", \"\", regex=True).str.title()\n    .replace({\"Sf\": \"San Francisco\", \"La\": \"Los Angeles\"})\n)\nhomeless[\"shelter_status\"] = (\n    homeless[\"shelter_status\"].str.strip().str.lower()\n    .str.replace(r\"^sheltered$\", \"shelter\", regex=True)\n    .str.replace(r\"shelter\\s*,\\s*pending\", \"shelter pending\", regex=True)\n    .str.replace(r\"temporary shelter\", \"shelter temporary\", regex=True)\n)\nhomeless[\"education_level\"] = (\n    homeless[\"education_level\"].str.strip().str.lower()\n    .replace({\"none\": \"None\", \"primary\": \"Primary\", \"secondary\": \"Secondary\", \"higher\": \"Higher\"})\n)\n\nhomeless.head()"
  },
  {
   "cell_type": "markdown",
   "id": "2152a8b3",
   "metadata": {},
   "source": "---\n## Matching a Question to a Plot Type (SLO 03C)\n\n| Question | Variable types | Plot |\n|---|---|---|\n| What's the distribution of one number? | one numeric | **Histogram** |\n| How do two numbers relate to each other? | two numeric | **Scatter** |\n| How does something change across an order? | numeric, ordered (time, rank, duration...) | **Line** |\n| How do groups compare? | numeric + categorical | **Bar** |\n\nSame underlying idea every time: a column maps to a **visual channel** (x, y, color...), and the mapping you choose decides what question the chart can answer.\n\n🙋 **Quick Check:** you want to compare the *average* `monthly_support_usd` across the five cities. Which plot type from the table fits, and why?"
  },
  {
   "cell_type": "markdown",
   "id": "ec4f37d3",
   "metadata": {},
   "source": "---\n## Histograms · ~10 min\n\nA **histogram** divides a numeric variable into **bins** and counts how many rows fall into each. It's the tool for seeing a variable's **center, spread, and shape** — symmetric? skewed? multiple peaks? — and for spotting outliers.\n\nA histogram is *not* the same as a bar chart: a bar chart's bars are categories; a histogram's bars are *ranges of a number*.\n\n🎯 **Predict First:** before we plot it — do you expect `monthly_support_usd` to be roughly symmetric, or skewed toward one side? Take a guess."
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "64a5c310",
   "metadata": {},
   "outputs": [],
   "source": "px.histogram(homeless, x=\"monthly_support_usd\", nbins=15,\n             title=\"Distribution of Monthly Support ($)\",\n             labels={\"monthly_support_usd\": \"Monthly Support (USD)\"})"
  },
  {
   "cell_type": "markdown",
   "id": "28db7f02",
   "metadata": {},
   "source": "Try changing `nbins` to `5`, then to `40`. What do you gain, and what do you lose, at each? Whose interests might be served by a smoothed-out distribution instead of a spiky one — or the other way around?\n\n---\n### 🔨 Task 1 — Read a Histogram (~4 min)\n\nPlot a histogram of `years_homeless`. Is it roughly symmetric, or skewed? What would that shape mean for a program planning shelter capacity?"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7c47c89b",
   "metadata": {},
   "outputs": [],
   "source": "# Your code here\n"
  },
  {
   "cell_type": "markdown",
   "id": "ecc04471",
   "metadata": {},
   "source": "---\n## Scatter Plots · ~10 min\n\nA **scatter plot** shows the relationship between two numeric variables — one point per row. Useful for spotting trends, clusters, and outliers, and for asking whether one variable seems to predict another.\n\n🎯 **Predict First:** do you expect `years_homeless` and `monthly_support_usd` to trend together (more time homeless → more support), trend apart, or show no clear relationship at all?"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6a0c8dd2",
   "metadata": {},
   "outputs": [],
   "source": "px.scatter(homeless, x=\"years_homeless\", y=\"monthly_support_usd\", color=\"shelter_status\",\n           title=\"Years Homeless vs. Monthly Support\",\n           labels={\"years_homeless\": \"Years Homeless\", \"monthly_support_usd\": \"Monthly Support (USD)\"})"
  },
  {
   "cell_type": "markdown",
   "id": "2d764bfb",
   "metadata": {},
   "source": "Was your prediction right? Does the pattern look different depending on `shelter_status`?\n\n🙋 **Quick Check:** what does mapping `shelter_status` to `color=` add here that a plain black-and-white scatter plot couldn't show?\n\n---\n### 🔨 Task 2 — Build Your Own Scatter Plot (~4 min)\n\nPlot `family_size` against `monthly_support_usd`, colored by `education_level`. Describe one pattern you see — or the lack of one."
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b035ba5c",
   "metadata": {},
   "outputs": [],
   "source": "# Your code here\n"
  },
  {
   "cell_type": "markdown",
   "id": "14f72421",
   "metadata": {},
   "source": "---\n## Line Plots · ~10 min\n\nLine plots connect points **in order** — almost always across time, but any meaningfully ordered variable works. We don't have dates here, so we'll order by `years_homeless` itself: as time homeless increases, how does *average* support change?\n\nNotice we have to `groupby()` first — one line point per value of `years_homeless`, not one per person. Monday's skill feeds directly into today's chart.\n\n🎯 **Predict First:** do you expect average support to rise, fall, or stay flat as years homeless increases?"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "19ded360",
   "metadata": {},
   "outputs": [],
   "source": "by_years = homeless.groupby(\"years_homeless\")[\"monthly_support_usd\"].mean().reset_index()\n\npx.line(by_years, x=\"years_homeless\", y=\"monthly_support_usd\",\n        title=\"Average Monthly Support by Years Homeless\",\n        labels={\"years_homeless\": \"Years Homeless\", \"monthly_support_usd\": \"Average Monthly Support (USD)\"})"
  },
  {
   "cell_type": "markdown",
   "id": "6c683548",
   "metadata": {},
   "source": "🙋 **Quick Check:** this line jumps around a lot rather than following a smooth trend. What does that tell you about how much data we have *per* value of `years_homeless`? (Hint: think back to Monday's `groupby().count()`.)\n\n---\n### 🔨 Task 3 — Build Your Own Line Plot (~4 min)\n\nGroup by `years_homeless` again, but this time plot the average `family_size`. Does family size trend up, down, or stay flat as years homeless increases?"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5bcb284c",
   "metadata": {},
   "outputs": [],
   "source": "# Your code here\n"
  },
  {
   "cell_type": "markdown",
   "id": "d33c1aca",
   "metadata": {},
   "source": "---\n## Bar Charts · ~10 min\n\n**Bar charts** compare a number across categories. Vertical bars work well for a few categories; horizontal bars work better for many categories or long labels. `color=` turns one bar chart into a **grouped** or **stacked** comparison.\n\n🎯 **Predict First:** which `education_level` group do you guess receives the highest *average* monthly support? Guess before running the cell."
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "efc4e179",
   "metadata": {},
   "outputs": [],
   "source": "avg_support = homeless.groupby(\"education_level\")[\"monthly_support_usd\"].mean().reset_index()\n\npx.bar(avg_support, x=\"education_level\", y=\"monthly_support_usd\",\n       title=\"Average Monthly Support by Education Level\",\n       labels={\"education_level\": \"Education Level\", \"monthly_support_usd\": \"Average Monthly Support (USD)\"})"
  },
  {
   "cell_type": "markdown",
   "id": "cf0380c7",
   "metadata": {},
   "source": "---\n### 🔨 Task 4 — Build Your Own Bar Chart (~4 min)\n\nMake a bar chart of the **count** of people per `city`, sorted from most to fewest. (*Hint:* `groupby(\"city\").size()`, then `sort_values()`, then `reset_index()` before plotting — or pass `orientation=\"h\"` if the city labels get cramped.)"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5bb10488",
   "metadata": {},
   "outputs": [],
   "source": "# Your code here\n"
  },
  {
   "cell_type": "markdown",
   "id": "77ac9ce9",
   "metadata": {},
   "source": "---\n## When Charts Deceive · ~5 min\n\nCharts can mislead in three broad ways:\n\n* **Misrepresentation** — the chart is simply wrong: cherry-picked data, distorted proportions, a truncated axis that exaggerates a difference.\n* **False impressions** — technically accurate, but a visual choice (3D effects, inconsistent colors, an unlabeled log scale) suggests a pattern that isn't really there.\n* **Ambiguity** — missing labels, units, or context leave the chart open to multiple readings.\n\n**Try it:** plot the *total* (not average) `monthly_support_usd` per `city`. Why might that number alone be misleading if you don't also show how many people are in each city?"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "05131501",
   "metadata": {},
   "outputs": [],
   "source": "# Your code here — total monthly_support_usd per city\n"
  },
  {
   "cell_type": "markdown",
   "id": "667293e5",
   "metadata": {},
   "source": "📎 **In class:** we'll pull up the **Graphics Principles cheat sheet** (the Novartis one) and go through it together — a one-page reference for exactly these misrepresentation/false-impression/ambiguity traps, and how to avoid them in your own charts. Keep it handy for the Final Project.\n\nThe **\"Thumper Principle\"** (borrowed from *Bambi*): if a chart can't say something useful, don't make it. A visualization exists to help people **see** the data more clearly — if it isn't doing that, it isn't finished yet."
  },
  {
   "cell_type": "markdown",
   "id": "56f40ed5",
   "metadata": {},
   "source": "---\n## Coming Up\n\n| Day | Topic | Builds on today |\n|---|---|---|\n| Fri | Forum 1 — *Counting*, Ch. 1 | Every chart today made a choice about what to show — this week's reading asks who makes that choice, and for whom |\n| Week 4 | Joining tables | Combining datasets *before* you can plot them together |\n| Week 5 | Clustering | Finding groups the data suggests, instead of ones we chose (like `city` or `education_level`) in advance |"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python",
   "version": "3.11.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
