Data Visualization

Overview


At the end of Using Libraries, the study log had a trusted table-shaped view of its data. The DataFrame had three columns:

subject, minutes, date

You used that DataFrame to compute textual summaries: totals, averages, top sessions, and recent sessions. This part starts from the same place. The data is not changing. What changes is the way you communicate some of the summaries.

A visualization is a summary you can read with your eyes. A bar chart can make category comparisons easier to scan than a printed dictionary. A line chart can make a time pattern easier to see than a list of daily totals. A histogram can show whether session lengths are mostly short, mostly long, or spread out.

But a chart is not magic. It does not make bad data good. It does not explain causes by itself. It does not remove the need to understand the table behind it. The chart comes after the same careful routine you practiced in Using Libraries:

  1. inspect the DataFrame,
  2. name the question,
  3. prepare a small summary table if needed,
  4. make the chart,
  5. and check whether the chart really answers the question.

The libraries for this part are matplotlib and seaborn. Matplotlib is the basic plotting library. Seaborn is a higher-level library built on top of matplotlib that makes many common charts shorter to write. You will use both, but the most important skill is not memorizing every plotting function. The skill is matching a question, a table, and a readable visual form.

Install both libraries in the same virtual environment you use for your current study-log project. You originally created that project in Reading and Writing Files and extended it through Using Libraries. With that environment active, run:

Terminal window
python -m pip install matplotlib seaborn

Then verify both the interpreter and the installed versions:

Terminal window
python -c "import sys; print(sys.executable)"
python -c "import matplotlib, seaborn; print(matplotlib.__version__, seaborn.__version__)"

If an import fails even though installation succeeded, compare the interpreter path printed by the first command with the environment where you ran python -m pip. Installing and running with the same python avoids the most common environment mismatch.

The structure of this part is as follows:

The Data We Will Use

Most examples use tiny inline data so you can copy them into a local file and run them without downloading anything:

import pandas as pd
records = [
{"subject": "math", "minutes": 45, "date": "2026-05-10"},
{"subject": "history", "minutes": 30, "date": "2026-05-11"},
{"subject": "programming", "minutes": 60, "date": "2026-05-11"},
{"subject": "math", "minutes": 25, "date": "2026-05-12"},
]
df = pd.DataFrame(records, columns=["subject", "minutes", "date"])
df["date"] = pd.to_datetime(df["date"])

This is the same shape as the DataFrame from the Using Libraries project checkpoint. In your full study-log project, you may create the DataFrame from Session objects:

log = load_study_log("sessions.json")
df = sessions_dataframe(log.sessions)

Once you have df, the plotting code should not care whether the rows originally came from inline dictionaries, a CSV file, JSON, or Session objects. That boundary is important. Loading data, preparing a DataFrame, summarizing rows, and plotting results are separate jobs.

The Basic Workflow

For example, the question “Which subjects have the most total study time?” is not answered by plotting the raw DataFrame directly. The raw DataFrame has one row per session. The question asks for one total per subject.

Prepare that summary first:

subject_totals = df.groupby("subject")["minutes"].sum()
subject_totals = subject_totals.sort_values(ascending=False)
print(subject_totals)

Only then make the chart. The plot is a visual version of a summary you already know how to compute.

This is the main habit for the part: do not rush to the chart. Make the table meaning clear first, then draw it.

A useful rhythm for this part is:

  1. read each chapter in order,
  2. run the plotting examples locally and look at the figures, changing one small thing at a time,
  3. complete the exercises as they appear,
  4. add the required charts to your study log in the project checkpoint,
  5. and complete the recap and feedback chapter.

Work through this part locally, because plotting is a local-development skill, and close figures when you are done with them.

By the end, your study log should be able to produce a few useful charts:

  • total minutes by subject,
  • study minutes over time,
  • and the distribution of session lengths.

Those charts do not replace the textual dashboard from Using Libraries. They sit beside it. Sometimes a number is the clearest summary. Sometimes a table is. Sometimes a chart is. A working programmer learns to choose.