BBampaa

Lessons › Python

Python lesson

Charts

pandas draws charts with matplotlib, which is plt here. Bars compare categories; lines show change over time.

Try this lesson free →
About 5–10 minutes · no sign-up needed

Example

Group the revenue by store, then plot it:

totals = df.groupby("Store")["Revenue"].sum()
totals.plot(kind="bar")
plt.title("Revenue by store")
plt.show()

Output:

CHART [{"k":"bar","x":["Abuja","Lagos"],"y":[195,264]}] T:Revenue by store

Key points

plt.bar(x, y)Bar chart: compare categories.
plt.plot(x, y)Line chart: change over time.
plt.title("...")Add a title. plt.show() draws the chart.

Or straight from pandas: totals.plot(kind="bar").

Practice

Try each one in your head first, then open the answer.

1. Build a line that draws a bar chart of sales for each day.
plt.bar(days, sales)
2. Build a line that draws a line chart of sales across the week.
plt.plot(days, sales)
3. Draw a bar chart of total revenue for each product.
df.groupby("Product")["Revenue"].sum().plot(kind="bar")
plt.show()
4. Draw a line chart of sales for each day, using the days and sales lists.
plt.plot(days, sales)
plt.show()
5. Draw a bar chart of total revenue for each store, with the title "Revenue by store".
df.groupby("Store")["Revenue"].sum().plot(kind="bar")
plt.title("Revenue by store")
plt.show()

Learn it by doing. This lesson is a short hands-on level on Bampaa: see it work, then build and write it yourself, with instant feedback.

Try this lesson free →
About 5–10 minutes · no sign-up needed