Visualization

Expert · 17 min read · ▶ live playground · ✦ checkpoint

One chart can make a quiet problem loud. A table says Ravi scored 91; a plot shows whether Ravi is improving, whether everyone else is stuck, and whether one odd value deserves a closer look. Matplotlib is Python's core plotting library: it turns lists of numbers into line charts, bar charts, scatter plots, and histograms you can inspect with your eyes.

Matplotlib fundamentals

Matplotlib is a plotting library, a third-party package for drawing charts from data. Most examples use matplotlib.pyplot as plt; pyplot is Matplotlib's command-style interface, a small set of functions for creating and labeling charts.

A figure is the whole drawing area. An axis is one measurement direction on the chart, such as the horizontal week axis or the vertical score axis. A line chart connects values in order, which makes it useful for trends over time.

A marker is the symbol drawn at each data point. A legend is the small chart key that explains which line belongs to which player. A grid is a set of guide lines behind the data.

import matplotlib.pyplot as plt
 
weeks = [1, 2, 3, 4]
mina_scores = [72, 78, 85, 88]
ravi_scores = [75, 79, 84, 91]
 
plt.plot(weeks, mina_scores, marker="o", label="Mina")
plt.plot(weeks, ravi_scores, marker="o", label="Ravi")
plt.title("Practice score by week")
plt.xlabel("Week")
plt.ylabel("Score")
plt.grid(True)
plt.legend()
plt.show()

The data is ordinary Python lists. Matplotlib handles the drawing. title, xlabel, and ylabel tell the reader what they are looking at.

Think of a chart as a labeled stage for your data. The lists are the actors, the axes are the floor markings, and the title tells the audience what scene they are watching.

Try it - change the story

The first time you run a Matplotlib playground in this lesson, the browser downloads Matplotlib once. After that, the package stays loaded for this page, and each Run still starts your own code fresh.

Change the scores, labels, or title. Run it again and watch the picture change.

python — playgroundlive
⌘/Ctrl + Enter to run

Choosing the right plot type

Different chart types answer different questions. A line chart asks, "How does this change in order?" A bar chart compares categories, such as players, teams, or snack choices.

import matplotlib.pyplot as plt
 
players = ["Mina", "Ravi", "Noor", "Isha"]
scores = [82, 91, 77, 88]
 
plt.bar(players, scores, color="steelblue")
plt.title("Latest practice score")
plt.xlabel("Player")
plt.ylabel("Score")
plt.show()

Bars work well when the x-axis is a set of names, not a smooth number line. You can quickly see the tallest bar without reading every value.

A scatter plot draws one dot per record to show how two measurements move together. Use it when you want to ask, "Do players who practice more minutes tend to score higher?"

import matplotlib.pyplot as plt
 
minutes = [31, 28, 24, 30, 36, 22]
scores = [82, 91, 77, 88, 93, 74]
 
plt.scatter(minutes, scores, color="purple")
plt.title("Practice time and score")
plt.xlabel("Minutes practiced")
plt.ylabel("Score")
plt.grid(True)
plt.show()

Scatter plots do not prove cause. They help you see a relationship worth testing. If the dots drift upward as minutes increase, you have a visual clue.

A histogram groups numeric values into ranges, called bins, to show a distribution, the shape of how values are spread out. Use it when you care less about who got each score and more about where the scores cluster.

import matplotlib.pyplot as plt
 
all_scores = [72, 78, 82, 85, 88, 91, 77, 84, 90, 86]
 
plt.hist(all_scores, bins=5, color="slateblue")
plt.title("Score distribution")
plt.xlabel("Score range")
plt.ylabel("Player count")
plt.show()

With a histogram, the exact bin choice changes the look. Fewer bins make a rough summary. More bins reveal more detail but can make a small data set look noisy.

Customizing without hiding the data

Customization should make the chart easier to read, not louder. Start with four habits:

  • Add a title that names the question.
  • Label both axes with the unit or meaning.
  • Use a legend when more than one line or group appears.
  • Use color to separate ideas, not to decorate every element.

Matplotlib has many knobs: colors, markers, line styles, annotations, and subplots. An annotation is a small note attached to a specific point in a chart. A subplot is one chart panel inside a larger figure. Those tools are useful, but labels and chart choice matter more than fancy styling.

Here is a small example with an annotation:

import matplotlib.pyplot as plt
 
weeks = [1, 2, 3, 4]
scores = [70, 73, 80, 92]
 
plt.plot(weeks, scores, marker="o", color="darkorange")
plt.title("Isha's score jump")
plt.xlabel("Week")
plt.ylabel("Score")
plt.annotate("new practice plan", xy=(4, 92), xytext=(2.4, 89))
plt.grid(True)
plt.show()

The annotation points attention at the jump without forcing the reader to guess what changed. Use this sparingly. If every point has a note, no point has focus.

A static look at seaborn and plotly

seaborn is a plotting package built on top of Matplotlib that makes statistical charts and polished defaults easier. plotly is a plotting package for interactive charts, where readers can hover, zoom, and explore in the browser.

They are not loaded in this course runtime, so treat this as workflow shape, not runnable code here:

# seaborn shape: a polished bar chart from table-shaped data
sns.barplot(data=practice, x="team", y="score")
 
# plotly shape: an interactive scatter plot
fig = px.scatter(practice, x="minutes", y="score", color="team")
fig.show()

Matplotlib is still the foundation worth learning first. Once you understand axes, labels, chart types, and honest customization, seaborn and plotly feel like helpful front doors, not magic.

Checkpoint

Answer all three to mark this lesson complete

Visualization helps you notice the shape of the data before you argue with the details. Next, the data workflow pulls the pieces together: cleaning messy inputs, exploring patterns, and keeping a notebook useful instead of chaotic.

+50 XP on completion