By the end of this lesson, you will be able to:
:information_source: Data visualization is the process of turning data into pictures, graphs, and charts to make it easier to understand.
Data visualization helps us because:
Let's look at an example of a bar chart:
Many professionals use data visualization:
:bulb: Key Benefits:
- Quick understanding: See patterns in seconds instead of minutes
- Better decisions: Clear visuals help you make smart choices
- Keep attention: Pictures are more interesting than numbers!
:information_source: Matplotlib is a Python tool that helps you create graphs and charts from your data.
Matplotlib can do amazing things:
Every graph has different parts. Look at this diagram:
We'll learn about these important parts:
:memo: Want to learn more? Check out the official Matplotlib tutorials: https://matplotlib.org/tutorials/index.html
:memo: Steps to Create a Graph
Follow these simple steps:
- Import the package: Tell Python you want to use Matplotlib
- Plot your data: Create the basic graph
- Show the graph: Display it on screen
- Add title and labels: Make it clear what your graph shows
- Add a legend: Explain what each line means
- Customize: Change colors and styles
- Try different graphs: Explore bar charts, pie charts, and more
:computer: Let's Code with Matplotlib!
Step One: Import the Package
First, we need to import two packages. NumPy helps with numbers, and Matplotlib creates the graphs:
python
import numpy as np import matplotlib.pyplot as plt
tip Remember: We use
plt
as a short name formatplotlib.pyplot
to save typing!
Let's use real data about people not following safety rules in Malaysia during 2020:
month = ["Aug", "Sep", "Oct", "Nov"]
social_dict = [1973, 1530, 3787,3424]
wear_mask = [413, 617, 5051, 3155]
This data shows "Statistics of not following SOP in Malaysia 2020"
To create a graph, use plt.plot(x-axis, y-axis)
. Here's how to show social distancing violations by month:
plt.plot(month, social_dict)
You can add multiple lines to the same graph:
plt.plot(month, social_dict)
plt.plot(month, wear_mask)
:memo: Pro tip: Each
plt.plot()
adds a new line to your graph!
Step 3: Show Your Graph
To see your graph on screen, use
plt.show()
:pythonplt.show()
Step 4: Add Title and Labels
Your graph needs a title and labels so people understand what it shows:
python
plt.title("Statistics of not following SOP in Malaysia 2020") plt.xlabel("Month") plt.ylabel("Number of cases")
Here's what each command does:
plt.title()
- Adds a title at the topplt.xlabel()
- Names the x-axis (horizontal)plt.ylabel()
- Names the y-axis (vertical)
Step 5: Add a Legend
When you have multiple lines, you need a legend to tell them apart. A legend is like a key that explains what each line means:
python
plt.legend(["Social Distance", "Wear Mask"])
The legend appears in a box on your graph
tip Alternative method: You can add labels directly when plotting:
plt.plot(month, social_dict, label="Social Distance")
plt.plot(month, wear_mask, label="Wear Mask")
plt.legend()
This method is cleaner and easier to read!
Change how your lines look by adding style codes. For example:
"r--"
= red dashed line"k^"
= black triangles"g-"
= green solid line"b:"
= blue dotted lineplt.plot(month, social_dict, "r--")
plt.plot(month, wear_mask, "k^")
Here's how to use different styles:
:memo: Learn more styles:
Great job! You've learned how to:
Data visualization helps us understand complex information quickly. With Matplotlib, you can turn boring numbers into exciting visual stories!
Code with AI: Try using AI to create and customize charts.
Beginner Prompts:
Advanced Prompts:
:bulb: Tip Remember: The best way to learn is by doing. Start with simple graphs and gradually add more features!