انتقل للمحتوى
Robo EgyptRobo Egypt
☰ Course contents
Robo Python: home
0. Lab tour1
1. The start5
2. Variables and input5
3. Conditions5
4. Loops5
5. Lists4
6. Functions4
7. Drawing with the turtle3
8. The project: the guess-the-number game3
9. Strings in depth1
10. Dictionaries (dict)3
11. Randomness and errors3
12. Classes and objects (class)2
13. Pro drawing and charts3
14. The final project: the quiz game2

Charts with matplotlib

Section 13: Pro drawing and chartsLesson 13.3 · ⏱ 12 min

Pros turn numbers into pictures. The matplotlib library works in Robo Python, and the chart appears in the result panel.

A line chart: the temperature during the week

import matplotlib.pyplot as plt

days = ["Sat", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri"]
temps = [24, 26, 29, 31, 30, 27, 25]

plt.plot(days, temps, marker="o", color="tomato")
plt.title("Temperature this week")
plt.ylabel("°C")
plt.grid(True)
plt.show()
print("Highest:", max(temps), "on", days[temps.index(max(temps))])
🖥️ The output in Robo Python:
Highest: 31 on Tue
🐍 Open the code in Robo Python ↗
The line chart in Robo Python
The line chart in Robo Python

A bar chart: the class's results

The bars are green for those who got 85 or more. Notice the line that makes the list of colours in one line.

import matplotlib.pyplot as plt

names = ["Ali", "Mona", "Omar", "Sara"]
marks = [80, 93, 75, 88]
colors = ["green" if m >= 85 else "orange" for m in marks]

plt.bar(names, marks, color=colors)
plt.title("Class results")
plt.ylim(0, 100)
plt.show()
print("Average:", sum(marks) / len(marks))
🖥️ The output in Robo Python:
Average: 84.0
🐍 Open the code in Robo Python ↗
The bar chart: green for the top students
The bar chart: green for the top students
  1. Draw how many hours you slept on seven days.
  2. Draw the result of the dice project (11-2) as bars with plt.bar.

📌 Another example: the temperature line

A line chart: a week's temperature.

🐍 The temperature line
import matplotlib.pyplot as plt

days = ["Sat", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri"]
temps = [24, 26, 25, 29, 31, 30, 27]
plt.plot(days, temps, marker="o", color="orange")
plt.title("Temperature this week")
plt.ylabel("°C")
plt.show()
🐍 Open the code in Robo Python ↗
In Robo Python
In Robo Python
✍️ Practise
Try the lesson's examples here: copy the code, change it, and click ▶ Run.
✍️ Practise here in the lessonor open the lab in a new page ↗

🎯 Challenge yourself

Easy

Bars: the number of students in each class.

👀 Show the solution
🐍 Solution
import matplotlib.pyplot as plt

classes = ["A", "B", "C", "D"]
students = [28, 32, 25, 30]
plt.bar(classes, students, color="teal")
plt.title("Students")
plt.show()
🐍 Open the code in Robo Python ↗
In Robo Python
In Robo Python

Result: four bars.

Medium

A pie: your favourite colours.

👀 Show the solution
🐍 Solution
import matplotlib.pyplot as plt

colors = ["red", "blue", "green"]
votes = [5, 9, 6]
plt.pie(votes, labels=colors, colors=colors)
plt.title("Favourite colour")
plt.show()
🐍 Open the code in Robo Python ↗
In Robo Python
In Robo Python

Result: a pie with three parts.

Hard

Bars for the results of 600 dice rolls.

👀 Show the solution
🐍 Solution
import matplotlib.pyplot as plt

import random
counts = [0] * 6
for i in range(600):
    counts[random.randint(1, 6) - 1] += 1
plt.bar([1, 2, 3, 4, 5, 6], counts, color="purple")
plt.title("600 dice rolls")
plt.show()
🐍 Open the code in Robo Python ↗
In Robo Python
In Robo Python

Result: six bars close to each other.

Finished the lesson? Mark it so your progress counts.