Pandas DataFrame Plotting Exercises and Solutions

Solidify your data visualization skills with hands-on practice problems. This exercise hub tests your ability to select the right chart format, transform data with value_counts() and groupby(), customize dimensions and labels, and render clean charts across all Pandas plot types.

Sample Dataset Setup 🔝

You can download the practice Excel file Download student.xlsx ↓ or generate the sample DataFrame directly in Python using the code below:

import pandas as pd

data = {
    "id": range(1, 11),
    "name": ["John", "Ravi", "Sara", "Alex", "Nina", "Mike", "Tara", "Leo", "Maya", "Omar"],
    "class1": ["Four", "Three", "Four", "Five", "Three", "Four", "Five", "Three", "Four", "Five"],
    "mark": [75, 82, 90, 65, 88, 70, 95, 60, 85, 78]
}
df = pd.DataFrame(data)
print(df)

Practice Problems Overview 🔝

Solve each of the following 5 core plotting challenges:

  1. Problem 1 (Line Chart): Count the number of students in each class using value_counts() and render an ordered line chart.
  2. Problem 2 (Bar Chart): Group records by class1 and plot the mean mark for each class.
  3. Problem 3 (Multi-Bar Comparison): Group by class1 and compute both the mean and maximum marks side-by-side.
  4. Problem 4 (Pie Chart): Calculate the percentage share of students across classes and render a pie chart with percentage callouts (autopct).
  5. Problem 5 (Density Plot): Plot the continuous probability density curve of student marks using Kernel Density Estimation (plot.density()).

Step-by-Step Exercise Solutions 🔝

Click below to reveal verified Python code solutions for each exercise:

Solution 1: Student Count Line Chart

counts = df["class1"].value_counts().sort_index()
counts.plot.line(marker="o", grid=True, title="Class Student Enrollment Counts")
plt.ylabel("Student Count")
plt.show()

Solution 2: Class Mean Mark Bar Chart

mean_df = df.groupby("class1")["mark"].mean()
mean_df.plot.bar(title="Mean Mark by Class", color="#2b5c8f")
plt.ylabel("Average Score")
plt.show()

Solution 3: Multi-Bar Mean & Max Comparison

agg_df = df.groupby("class1")["mark"].agg(["mean", "max"])
agg_df.plot.bar(title="Mean vs Maximum Mark by Class", figsize=(6.5, 4))
plt.ylabel("Marks")
plt.show()

Solution 4: Class Enrollment Pie Chart

counts = df["class1"].value_counts()
counts.plot.pie(autopct="%1.0f%%", title="Class Student Share")
plt.ylabel("")
plt.show()

Solution 5: Continuous Mark Density Estimation

df["mark"].plot.density(title="Student Mark Probability Density", bw_method=0.3)
plt.xlabel("Marks")
plt.show()

Pandas Plotting Cluster Reference Table 🔝

Explore dedicated tutorials for each individual chart format in this cluster:

TutorialMethodPrimary Analytical Purpose
Line Chartsdf.plot.line()Continuous series, time-series trends, and progression over ordered intervals.
Bar Chartsdf.plot.bar()Comparing discrete category metrics and multi-column grouped totals.
Horizontal Barsdf.plot.barh()Leaderboards, rankings, and categories with long descriptive text labels.
Histogramsdf.plot.hist()Frequency counts across continuous data intervals with custom binning.
Box Plotsdf.plot.box()Five-number statistical summary (IQR, median) and outlier flier detection.
Pie Chartsdf.plot.pie()Relative percentage shares and composition of categorical totals.
Density Plotsdf.plot.density()Smooth Kernel Density Estimation (KDE) curves for continuous distributions.
Area Plotsdf.plot.area()Cumulative volume trends and stacked proportional accumulation over time.
Scatter Plotsdf.plot.scatter()Bivariate correlations, point dispersion, and 3D bubble chart sizing.
Hexbin Plotsdf.plot.hexbin()2D hexagonal density binning for large datasets solving overplotting.
Plots from MySQLpd.read_sql()Extracting database records directly into analytical Pandas visualizations.

Practice in Google Colab 🔝

Work through all exercises with pre-populated datasets and verified assertions in Google Colab:

Open in Google Colab View on GitHub
Work through all plotting practice challenges interactively, test your data transformation solutions, and verify your code against automated assertions directly in Google Colab. All sample datasets are embedded directly in the notebook.

Reference: Pandas DataFrame.plot official documentation.




Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer