Python DATA VISUALIZATION: Matplotlib & Seaborn: 13 practice problems
UNIT 3 of the free Python for AI course. Read the short theory, then solve the problems below with instant tests. Python runs in your browser.
▶ Practise DATA VISUALIZATION: Matplotlib & Seaborn nowAll topicsTheory: DATA VISUALIZATION: Matplotlib & Seaborn in Python
THEORY
------
* ANATOMY of a Matplotlib plot: FIGURE (the whole window/image) contains one or more
AXES (an individual plot area). Each Axes has a title, x/y AXIS with labels & ticks,
a legend, and ARTISTS (lines, bars, patches, text).
* TWO STYLES
pyplot (state machine): plt.plot(x, y); plt.title("t"); plt.xlabel("x"); plt.savefig("a.png")
object-oriented (better): fig, ax = plt.subplots(); ax.plot(x, y); ax.set_title("t")
ax.set_xlabel("x"); ax.legend(); fig.savefig("a.png")
Grids: fig, axes = plt.subplots(2, 2, figsize=(8, 6)); axes[0, 1].bar(...); fig.tight_layout()
* PLOT TYPES & WHEN TO USE THEM (viva!)
line ax.plot trend over time / continuous x
bar ax.bar/barh compare categories
histogram ax.hist(bins) distribution of ONE numeric variable
scatter ax.scatter relationship between TWO numeric variables (correlation)
pie ax.pie parts of a whole (few categories)
box ax.boxplot median, quartiles (IQR), outliers; compare groups
heatmap imshow / sns.heatmap matrix values, e.g. correlation matrix
* CUSTOMIZE: color=, linestyle="--", marker="o", label= (+ legend), alpha=, figsize=, grid(True),
xlim/ylim, xticks rotation, annotate, text.
* SAVE: fig.savefig("x.png", dpi=150, bbox_inches="tight") BEFORE plt.show(); plt.close(fig) frees memory.
* SEABORN = high-level statistical plotting built on Matplotlib; works directly with DataFrames.
import seaborn as sns; sns.set_theme(style="whitegrid")
distribution sns.histplot(data=df, x="col", kde=True), kdeplot
categorical sns.boxplot / violinplot / barplot (mean + CI) / countplot(x="cat")
relational sns.scatterplot(x=, y=, hue="group"), lineplot, regplot (trend line)
matrix sns.heatmap(df.corr(), annot=True, cmap="coolwarm")
multi sns.pairplot(df, hue="species")
Every axes-level Seaborn function accepts ax=... and RETURNS the Axes.
* Reading a BOX PLOT: box = Q1..Q3 (IQR, middle 50%), line = median, whiskers ≈ 1.5×IQR,
dots beyond = outliers. Correlation: +1 strong positive, 0 none, -1 strong negative.
Every problem below returns a Matplotlib FIGURE; the judge inspects it.
13 DATA VISUALIZATION: Matplotlib & Seaborn practice problems
1. Website Traffic Line Chart
Easy2 ways to solve📤 OUTPUT: return the matplotlib Figure (fig). Use `return fig`, don't plt.show() or print(). One line, the given title, x-label "Day", y-label "Visitors", grid on. Return the figure.Solve “Website Traffic Line Chart” in your browser →
2. Sales by Product Bar Chart
Easy2 ways to solve📤 OUTPUT: return the matplotlib Figure (fig). Use `return fig`, don't plt.show() or print(). One bar per category, title "Sales", y-label "Units". Return the figure.Solve “Sales by Product Bar Chart” in your browser →
3. Exam Score Distribution — hist with `bins` bins, x-label "Score". Return the figure.
Easy1 way to solve📤 OUTPUT: return the matplotlib Figure (fig). Use `return fig`, don't plt.show() or print().Solve “Exam Score Distribution — hist with `bins` bins, x-label "Score". Return the figure.” in your browser →
4. Compare Cities' Temperatures — several lines + legend
Medium1 way to solve📤 OUTPUT: return the matplotlib Figure (fig). Use `return fig`, don't plt.show() or print().
series: dict {label: list of y values}. One line per entry with label=key; show a legend.
Return the figure.
Solve “Compare Cities' Temperatures — several lines + legend” in your browser →5. Mini Dashboard — subplots
Medium1 way to solve📤 OUTPUT: return the matplotlib Figure (fig). Use `return fig`, don't plt.show() or print().
datasets: dict {name: list of numbers}. Create ONE ROW of subplots (one per dataset), plot each
as a line, and set each subplot's title to its name. Return the figure.
Solve “Mini Dashboard — subplots” in your browser →7. Salary by Department — Seaborn boxplot
Medium1 way to solve📤 OUTPUT: return the matplotlib Figure (fig). Use `return fig`, don't plt.show() or print(). df has columns "dept" and "salary". sns.boxplot(x="dept", y="salary") on an Axes; title "Salary by department". Return the figure.Solve “Salary by Department — Seaborn boxplot” in your browser →
8. Correlation Heatmap — sns.heatmap(df.corr(), annot=True)
Medium1 way to solve📤 OUTPUT: return the matplotlib Figure (fig). Use `return fig`, don't plt.show() or print(). Return the figure. (The judge checks there is one number printed per matrix cell.)Solve “Correlation Heatmap — sns.heatmap(df.corr(), annot=True)” in your browser →
9. Ad Spend vs Sales — scatter + best-fit line
Medium1 way to solve📤 OUTPUT: return the matplotlib Figure (fig). Use `return fig`, don't plt.show() or print(). Draw the points with ax.scatter AND a straight trend line from np.polyfit(xs, ys, 1). Labels "Ad spend" / "Sales". Return the figure.Solve “Ad Spend vs Sales — scatter + best-fit line” in your browser →
10. Save a Figure — fig.savefig(path), then close it. Return path.
Easy1 way to solve📤 OUTPUT: return the matplotlib Figure (fig). Use `return fig`, don't plt.show() or print().Solve “Save a Figure — fig.savefig(path), then close it. Return path.” in your browser →
11. Which Chart? — return a list of chart names for each scenario, choosing from
Easy1 way to solve📤 OUTPUT: return a list of chart names, e.g. ['line', 'bar', ...]. Use `return`, not print().
"line", "bar", "histogram", "scatter", "pie", "box", "heatmap":
1. Monthly revenue over 3 years
2. Number of students in each department
3. How exam marks are spread (most common range)
4. Does study time relate to marks?
5. Share of each browser in total users
6. Compare salary spread and outliers across 4 departments
7. Correlation between 6 numeric columns
Solve “Which Chart? — return a list of chart names for each scenario, choosing from” in your browser →12. CEO Dashboard — interview at a retail company (BI team)
Project1 way to solve📤 OUTPUT: return the matplotlib Figure (fig). Use `return fig`, don't plt.show() or print().
Interviewer: "The CEO wants ONE image with the 4 numbers that matter."
Return ONE figure with a 2 x 2 grid (plt.subplots(2, 2)):
top-left : line of monthly revenue (dict month -> value), title "Revenue by month"
top-right : bar chart of regions (dict), title "Revenue by region"
bottom-left : pie of categories (dict) with autopct="%1.0f%%", title "Category share"
bottom-right: histogram of order_values with 10 bins, title "Order value distribution"
and fig.suptitle("CEO dashboard").
Solve “CEO Dashboard — interview at a retail company (BI team)” in your browser →13. Stage 17 — Visualise the Profiler Output
Compiler1 way to solve📤 OUTPUT: return the matplotlib Figure (fig). Use `return fig`, don't plt.show() or print(). times: dict stage -> ms. Draw a HORIZONTAL bar chart (ax.barh) sorted so the SLOWEST stage is at the TOP, title "Compiler stage timings", x-label "ms". Return the figure.Solve “Stage 17 — Visualise the Profiler Output” in your browser →
← PANDAS (DataFrames & structured data manipulation) Introduction to artificial intelligence →