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.

About 16 minutes · code runs in your browser, nothing to install

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

Pythoneditable
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))])
Output
Best month: May
Chart drawn by this code

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".

Pythoneditable
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)))
Output
Months above last year: 5
Chart drawn by this code

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.

Pythoneditable
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()
Output
Product  Desk  Lamp
Region
East      330   150
North     610   420
South     290   380
Chart drawn by this code

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.

Pythoneditable
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])
Output
Top rep: Chloe
Chart drawn by this code
Tip: Keep charts as plain as Excel's best ones: one message per chart, a clear title, the unit on the axis, and no 3D effects.

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().

On your computer: save a chart
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().

Your codewrite your answer, then Run
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
Solutioneditable
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))
Output
Total visitors: 725
Chart drawn by this code

Check yourself

1. Which function draws a column chart?

2. What does label="2026" in plt.plot do?

3. How do you get a clustered column chart from a pivot table pt?

Spotted a mistake on this page? Tell us