Practice and reinforce the concepts from Lesson 13
:information_source: Total Activity Time: 45-60 minutes
- Setup: 5 minutes
- Question One: 20-25 minutes
- Question 2: 20-25 minutes
- Submission: 5 minutes
Copy and paste this code into the first cell of your notebook:
import numpy as np
import matplotlib.pyplot as plt
:bulb: :bulb: Remember: Always run the import cell first before starting any exercises!
Question One: Telebort Sports Day Graph
⏱️ Time: 20-25 minutes
Let's create a line graph showing the medals won by each Hogwarts house!
:computer: Step-by-Step Instructions
One. Create the data lists
Copy and paste this code into a new cell:
python
houses = ["Gryffindor", "Ravenclaw", "Hufflepuff", "Slytherin"] gold = [5, 7, 6, 2] silver = [4, 5, 5, 6] bronze = [5, 3, 4, 8]
2. Plot the graph lines
Add these three lines to plot each medal type:
pythonplt.plot(houses, gold) plt.plot(houses, silver) plt.plot(houses, bronze)
3. Add title and labels
python
plt.title("Telebort Sports Day: Number of Medals For Each House") plt.xlabel("Houses") plt.ylabel("Number of medals")
4. Add the legend
python
plt.legend(["Gold", "Silver", "Bronze"])
5. Display the graph
pythonplt.show()
tip :bulb: Pro tip: Run all the code in one cell to see your complete graph!
Try to recreate this styled version:
:bulb: :bulb: Hints for styling:
- Use
'r--'
for red dashed lines- Use
'g:'
for green dotted lines- Use
'b-.'
for blue dash-dot lines- Example:
plt.plot(houses, gold, 'r--')
⏱️ Time: 20-25 minutes
Let's create a graph showing the number of male and female students in each class!
One. Create the data lists
Copy and paste this code into a new cell:
classes = ["Class 1", "Class 2", "Class 3"]
male = [9, 10, 8]
female = [12, 6, 15]
2. Plot the graph lines
plt.plot(classes, male)
plt.plot(classes, female)
3. Add title and labels
plt.title("Number of Students in Each Class")
plt.xlabel("Classes")
plt.ylabel("Number of students")
4. Add the legend
plt.legend(["Male", "Female"])
5. Display the graph
plt.show()
Convert your line graph to a bar graph:
:bulb: :bulb: Hints for bar graphs:
- Replace
plt.plot()
withplt.bar()
- You'll need to adjust the x-positions for multiple bars
- Use
np.arange(len(classes))
to create x-positions- Add width parameter:
width = 0.35
- Offset one set of bars:
x - width/2
andx + width/2
:warning: Warning Before submitting:
- :white_check_mark: Make sure both graphs display correctly
- :white_check_mark: Check that all labels and titles are spelled correctly
- :white_check_mark: Save your Colab notebook (File -> Save)
- :white_check_mark: Take screenshots of your graphs if needed
:information_source: :link: Submit your exercise here Your submission helps us track your progress and provide feedback!
Common Issues:
plt.show()
at the end