Line and scatter
import matplotlib.pyplot as plt
days = [1, 2, 3, 4, 5]
score = [40, 55, 60, 72, 85]
plt.plot(days, score, marker="o", label="Practice score")
plt.scatter([2, 4], [70, 50], color="red", label="Mock tests")
plt.xlabel("Day")
plt.ylabel("Score")
plt.title("Learning over a week")
plt.legend()
plt.grid(True)
plt.show()
print("Chart drawn")Output
Chart drawn
Bars and histograms
import matplotlib.pyplot as plt
import numpy as np
fig, (left, right) = plt.subplots(1, 2, figsize=(9, 3.5))
left.bar(["Maths", "Science", "English"], [78, 85, 90])
left.set_title("Marks by subject")
heights = np.random.default_rng(1).normal(160, 8, 500)
right.hist(heights, bins=20)
right.set_title("Heights of 500 students")
plt.show()
print("Charts drawn")Output
Charts drawn
| Chart | Use it to show |
|---|---|
| Line | Change over time |
| Scatter | How two values relate |
| Bar | Comparing categories |
| Histogram | How values are spread |
You're ready for machine learning
You now know enough Python for every lesson in the Machine Learning module. Good next steps: the spam detector, then the KNN guide.
Finished reading?