Show contents
Chapter 5 of 7 · 20 min
An honest bar chart
By the end of this chapter, you'll be able to
- Draw a bar chart with characters
- Scale values so they fit on one line
- Recognise a misleading chart
- Choose a chart's title and labels
A bar is a repeated string
In Python, multiplying a text by a number repeats it: "█" * 5 gives █████. A bar whose length is proportional to a value fits on one line. ljust(9) pads the name with spaces up to 9 characters, so that all the bars start at the same place.
sales = {"Monday": 12, "Tuesday": 18, "Wednesday": 9, "Thursday": 15, "Friday": 24}
print("Meals sold per day (school canteen)")
for day in sales:
print(day.ljust(10), "█" * sales[day], sales[day])
Meals sold per day (school canteen)
Monday ████████████ 12
Tuesday ██████████████████ 18
Wednesday █████████ 9
Thursday ███████████████ 15
Friday ████████████████████████ 24
At a glance: Friday dominates, Wednesday is the quietest day. Notice the title and the number at the end of each bar: without them, the reader would have to guess what is being measured.
Scaling
With revenues of several hundred thousand francs, a bar of one character per franc would fit on no screen. We scale: the largest value gets the maximum length (here 30 characters), and every other value a proportional length.
revenue = {"Tecno": 275_000, "Samsung": 240_000, "Itel": 45_000}
largest = max(revenue.values())
print("Revenue for the month, in CFA francs")
for brand in revenue:
length = round(revenue[brand] / largest * 30)
print(brand.ljust(8), "█" * length, revenue[brand])
Revenue for the month, in CFA francs
Tecno ██████████████████████████████ 275000
Samsung ██████████████████████████ 240000
Itel █████ 45000
revenue.values() gives all the values of the dictionary, and max the largest one. The calculation value / largest gives a number between 0 and 1, which we multiply by the maximum length, then round with round: you cannot draw half a cell.
The bar is 26 cells long: its length keeps the proportion with Tecno.
Text description of the animation
- We want to draw Samsung's bar.
- Samsung is about 87% of the best brand.
- We take 87% of the 30 available cells, rounded to a whole number.
- The bar is 26 cells long: its length keeps the proportion with Tecno.
Mini-lab: your first chartPython 3.13
Output
Click “Run”: Python will load in your browser (about 10 MB the first time).Your code runs in your browser, isolated from the page. · Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Your turn
Worked exercise
Worked exercise: the sign-up chart
A computer club records the number of members per neighbourhood. Write a program that prints a title, then one bar per neighbourhood, scaled to 20 cells at most, with the number of members at the end.
members = {"Badalabougou": 34, "Hamdallaye": 51, "Lafiabougou": 17, "Magnambougou": 42}
Hint 1
largest = max(members.values()).Hint 2
round(members[n] / largest * 20) cells, and n.ljust(13) to line up the bars (the longest name has 12 characters).Show the solution and common mistakes
members = {"Badalabougou": 34, "Hamdallaye": 51, "Lafiabougou": 17, "Magnambougou": 42}
largest = max(members.values())
print("Computer club members, by neighbourhood")
for n in members:
length = round(members[n] / largest * 20)
print(n.ljust(13), "█" * length, members[n])
Computer club members, by neighbourhood
Badalabougou █████████████ 34
Hamdallaye ████████████████████ 51
Lafiabougou ███████ 17
Magnambougou ████████████████ 42
Common mistakes:
- Dividing by the sum instead of the maximum: the bars would be correct relative to each other, but the longest would not fill the space planned for it.
- Forgetting
round():"█" * 13.3raises aTypeError, because a text can only be repeated a whole number of times. - Forgetting the title: a chart with no title or unit forces the reader to guess what they are looking at.
Summary
- 1A bar chart makes a comparison visible at a glance.
- 2"█" * n draws a bar of n cells; ljust lines up the labels.
- 3To scale: value / maximum × available length, rounded with round.
- 4Bars must start at zero: cutting off their base inflates the gaps and misleads the reader.
- 5A good chart has a title, labels and the unit of the values.
Your progress is saved on this device. Create a free account to keep your progress on all your devices.