Python for Excel users · Lesson 13 of 14
Excel-style charts in Python with matplotlib
Make column, clustered column, line and ranked bar charts like Excel's with matplotlib and pandas, add titles and labels, and save them as images.
In Excel you select a range and click Insert → Chart. In Python you use matplotlib, the standard chart library, usually as plt. You tell it the categories and the values, add a title and axis labels, and plt.show() draws the chart.
Column chart
import matplotlib.pyplot as plt
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]
sales = [1200, 1350, 980, 1600, 1720, 1540]
plt.figure(figsize=(6, 3.5))
plt.bar(months, sales, color="#2f6f9f")
plt.title("Sales by month")
plt.ylabel("Sales (€)")
plt.tight_layout()
plt.show()
print("Best month:", months[sales.index(max(sales))])Best month: May

figsize is the width and height in inches; tight_layout() stops labels being cut off.
Line chart with two series
A line chart with a legend is two plt.plot calls, each with a label. marker="o" adds the dots Excel shows with "Line with Markers".
import matplotlib.pyplot as plt
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]
this_year = [1200, 1350, 980, 1600, 1720, 1540]
last_year = [1100, 1150, 1210, 1300, 1380, 1450]
plt.figure(figsize=(6, 3.5))
plt.plot(months, this_year, marker="o", label="2026")
plt.plot(months, last_year, marker="o", linestyle="--", label="2025")
plt.title("Sales: this year vs last year")
plt.ylabel("Sales (€)")
plt.legend()
plt.tight_layout()
plt.show()
print("Months above last year:", sum(a > b for a, b in zip(this_year, last_year)))Months above last year: 5

Chart straight from a pivot table
A DataFrame can draw itself with .plot(). A pivot table with products as columns gives a clustered column chart in one line, just like inserting a PivotChart.
import matplotlib.pyplot as plt
import pandas as pd
df = pd.DataFrame({
"Region": ["North", "North", "South", "South", "East", "East"],
"Product": ["Lamp", "Desk", "Lamp", "Desk", "Lamp", "Desk"],
"Sales": [420, 610, 380, 290, 150, 330],
})
pt = df.pivot_table(index="Region", columns="Product", values="Sales", aggfunc="sum")
print(pt)
pt.plot(kind="bar", figsize=(6, 3.5), rot=0, title="Sales by region and product")
plt.ylabel("Sales (€)")
plt.tight_layout()
plt.show()Product Desk Lamp Region East 330 150 North 610 420 South 290 380

rot=0 keeps the region names horizontal.
Ranked bar chart
For a ranking, sort first and use horizontal bars (barh), Excel's Bar chart. The largest value goes at the top when you sort from small to large, because the first bar is drawn at the bottom.
import matplotlib.pyplot as plt
import pandas as pd
reps = pd.Series({"Ana": 1450, "Ben": 820, "Chloe": 2300, "Dev": 990, "Eva": 1210})
ranked = reps.sort_values()
plt.figure(figsize=(6, 3.5))
plt.barh(ranked.index, ranked.values, color="#3a8f5c")
plt.title("Sales per rep")
plt.xlabel("Sales (€)")
plt.tight_layout()
plt.show()
print("Top rep:", ranked.index[-1])Top rep: Chloe

Saving the chart as an image
plt.savefig writes the chart to a PNG file that you can paste into slides or emails. Call it before plt.show().
plt.savefig("sales_by_month.png", dpi=150, bbox_inches="tight")
plt.show()✎ Exercise
Draw a column chart of the visitors per weekday with the title Visitors per day and the y-axis label Visitors, then print the total number of visitors.
Hint: Import matplotlib.pyplot as plt, then plt.bar(days, visitors), plt.title(...), plt.ylabel(...) and plt.show().
days = ["Mon", "Tue", "Wed", "Thu", "Fri"]
visitors = [120, 95, 140, 160, 210]
# your code here (start with the same import line as the examples)
Expected output:
Total visitors: 725
Show a solution
import matplotlib.pyplot as plt
days = ["Mon", "Tue", "Wed", "Thu", "Fri"]
visitors = [120, 95, 140, 160, 210]
plt.figure(figsize=(6, 3.5))
plt.bar(days, visitors)
plt.title("Visitors per day")
plt.ylabel("Visitors")
plt.tight_layout()
plt.show()
print("Total visitors:", sum(visitors))Total visitors: 725

Check yourself
1. Which function draws a column chart?
plt.bar draws vertical bars; plt.barh draws horizontal ones.
2. What does label="2026" in plt.plot do?
Labels appear when you call plt.legend().
3. How do you get a clustered column chart from a pivot table pt?
DataFrame.plot(kind="bar") draws one group of bars per row and one colour per column.