Data Analysis with Python and pandas
Charts and Insights
In this lesson you will
- Draw a bar chart from a grouped result
- Choose a suitable chart type
- Write a clear, honest insight
Introduction
Numbers in a table are hard to scan. A chart shows the pattern at a glance. pandas works with a plotting library called matplotlib (install it with pip install matplotlib).
A bar chart in three lines
Python
import matplotlib.pyplot as plt
totals = df.groupby("category")["revenue"].sum().sort_values(ascending=False)
totals.plot(kind="bar", title="Revenue by category")
plt.ylabel("Revenue")
plt.tight_layout()
plt.savefig("revenue.png") # or plt.show() in a notebookThis draws one bar per category, tallest first. The bars show that Bags earns the most, then Stationery, then Kitchen.
Choosing a chart
- Bar chart: compare amounts across categories.
- Line chart: show change over time.
- Histogram: show how values are spread out, such as all prices.
- Scatter plot: check whether two number columns move together.
Avoid pie charts with many slices, because they are hard to compare.
Quick quiz
Which chart is best for showing how monthly sales changed over a year?
Make charts honest
- Give every chart a title and label the axes, with units.
- Start bar charts at zero. Cutting the axis makes small differences look huge.
- Do not show more detail than the data supports.
From numbers to insight
An insight is a short, useful statement backed by data. Compare:
- Weak: "Here is a chart of revenue."
- Better: "Bags brought in 149.50 of the 304.50 total revenue in this small sample, nearly half."
Always add the limits. Our table has only six rows, so it is a practice sample and not proof of anything about a real shop. Say how many rows you used, and what you would check next.
Try it yourself
Load or build a small table of your own. Group it by one column, draw a bar chart, and write two sentences: what the chart shows and one limitation.
Summary
- Chart a grouped result with
.plot(kind="bar")and save or show it. - Match the chart type to the question, and label everything clearly.
- Finish with an honest insight that states its limits.
Lesson complete
+20 XP
Nice work. Keep going while it is fresh.
Sources and further reading