⚡0 XP🔥0

Data Visualisation with matplotlib and pandas

Lesson 6 of 7 ⏱ 5 min read ✎ 2 exercises ❓ 5-question quiz ▶ Runs Python

Choose the right chart and build it in Python - line, bar, horizontal bar, histogram, scatter, box plot and pie - with titles, labels, annotations, subplots for dashboards, pandas .plot shortcuts, and saving charts.

Why visualise?#

A chart reveals patterns, trends and outliers in seconds that would take minutes to see in a table - and it's how most managers consume analysis.

Choosing the right chart#

Question Chart matplotlib
How does it change over time? Line plt.plot()
How do categories compare? Bar / horizontal bar plt.bar() / plt.barh()
How are values distributed? Histogram / box plot plt.hist() / plt.boxplot()
Are two variables related? Scatter plt.scatter()
What's the share of a whole (2-5 parts)? Pie / donut (or 100% bar) plt.pie()
हिंदी में समझें

सही chart चुनना आधा काम है:

  • समय के साथ बदलाव (महीने-दर-महीने) → Line chart
  • तुलना (region, product) → Bar chart (लंबे नाम हों तो horizontal bar)
  • फैलाव (distribution) → Histogram / Box plot
  • दो चीज़ों का संबंध (discount vs profit) → Scatter plot
  • हिस्सेदारी (कुल का कितना %) → Pie (सिर्फ 2-5 हिस्से हों तो)

और chart का title ऐसा लिखें जो insight बताए - "Sales" नहीं, बल्कि "East की बिक्री 2025 में दोगुनी हुई"।

matplotlib basics#

import matplotlib.pyplot as plt

plt.figure(figsize=(8, 4))        # width, height in inches
plt.plot(x, y)                    # draw
plt.title("Insight as a title")
plt.xlabel("X label")
plt.ylabel("Y label")
plt.show()                        # display

Line chart#

▶ Try it Yourselfsales.csvPython
Ctrl + Enter to run

Bar and horizontal bar#

▶ Try it Yourselfsales.csvPython
Ctrl + Enter to run

Histogram and box plot#

▶ Try it Yourselfsales.csvPython
Ctrl + Enter to run

Scatter plot#

▶ Try it Yourselfsales.csvPython
Ctrl + Enter to run

Pie / donut#

▶ Try it Yourselfsales.csvPython
Ctrl + Enter to run
Common Mistake

Avoid pies with many slices, 3D charts, and dual y-axes that confuse readers. When in doubt, use a sorted bar chart.

Subplots - a mini dashboard#

▶ Try it Yourselfsales.csvPython
Ctrl + Enter to run

pandas plotting shortcuts#

DataFrames and Series have a .plot method (built on matplotlib):

series.plot()                    # line
series.plot.bar() / .barh()
df.plot.scatter(x="discount", y="profit")
series.plot.hist(bins=30)
df.boxplot(column="sales", by="category")

Formatting tips#

  • Big numbers: divide by 1e5 (lakh) or 1e3 and say so in the label.
  • plt.xticks(rotation=45) for crowded labels; plt.tight_layout() to avoid overlap.
  • plt.bar_label(bars) adds data labels; plt.annotate() points to a specific value.
  • Keep colours consistent across charts (same category = same colour).

Saving charts#

plt.savefig("monthly_sales.png", dpi=150, bbox_inches="tight")   # call before plt.show()
Did you know?

seaborn (pip install seaborn) adds beautiful statistical charts on top of matplotlib - try it on your own computer: sns.barplot, sns.heatmap, sns.boxplot. Plotly creates interactive charts for web dashboards.

Summary#

  • Pick the chart for the question: line (time), bar (compare), histogram/box (distribution), scatter (relationship), pie (few-part share).
  • Always add an insight title, axis labels and units.
  • plt.subplots builds multi-chart figures; pandas .plot is a quick shortcut.
  • Save with savefig for reports and presentations.

Practice Exercises

Write your code, then press Check. Your program is tested automatically.

1

Monthly trend chart

Create a line chart of total sales per month (use resample("MS")) with a title, x-label and y-label. Store the monthly Series in monthly (24 values).

Dataset: sales.csv (already loaded - just use the file name)
2

Profit by sub-category bar chart

Create profit_by_sub = total profit per sub_category, sorted ascending, and draw it as a horizontal bar chart (barh) with a title.

Dataset: sales.csv (already loaded - just use the file name)

Quick Quiz

Q1.

Which chart best shows sales trend across 24 months?

Q2.

You want to compare profit across 20 products with long names. Best chart?

Q3.

What does plt.subplots(2, 2) create?

Q4.

Which is a good chart title?

Q5.

How do you save a chart as an image?