Mada za sehemu hiiVisualising dataMada 2
- Creating basic plots using libraries
- Creating basic interactive visualisations
Creating Basic Plots Using Libraries
Data visualisation is the process of representing data graphically so that patterns, trends, and relationships become easier to understand. In programming, we use specialised libraries — collections of pre-written code — to create charts and plots quickly. The two most common plotting libraries in Python are matplotlib and seaborn. These libraries take numerical data and convert it into visual forms like lines, bars, and dots.
When you have a list of numbers, it can be hard to see what the data is telling you. A plot turns raw numbers into a picture. For example, if you have monthly sales figures for a shop in Dar es Salaam, a plot can instantly show you which months had the highest and lowest sales.
1. Line Plot
- Shows data points connected by straight lines.
- Best for tracking changes over time (e.g., temperature changes over a week, monthly revenue).
2. Bar Plot
- Uses rectangular bars to compare categories.
- Best for comparing quantities across different groups (e.g., sales of different crops: maize, rice, beans).
3. Scatter Plot
- Displays individual data points as dots.
- Best for showing the relationship between two variables (e.g., hours studied versus exam scores).
4. Histogram
- Shows how data is distributed across ranges.
- Best for seeing the frequency of values (e.g., distribution of daily profit amounts in a market).
Before creating plots, you must import the library. In Python, we write an import statement at the top of the program:
import matplotlib.pyplot as plt
The plt is a short nickname we choose so we can type plt instead of the full name each time.
Step 1: Prepare Your Data
Data is stored in lists. For example, here are the monthly profits (in thousands of Tanzanian shillings) for a small dukas in Arusha:
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]
profits = [150, 180, 120, 200, 220, 190] # in thousands TSh
Step 2: Create the Plot
To make a simple line plot:
plt.plot(months, profits)
plt.title("Monthly Profits of Duka in Arusha")
plt.xlabel("Month")
plt.ylabel("Profit (Thousands TSh)")
plt.show()
The plt.plot() function creates the line. The plt.title(), plt.xlabel(), and plt.ylabel() functions add labels so the reader understands what the plot shows. The plt.show() function displays the finished plot.
A farmer in Morogoro recorded the harvest in bags for three crops:
- Maize: 120 bags
- Rice: 85 bags
- Beans: 60 bags
The following code creates a bar plot:
import matplotlib.pyplot as plt
crops = ["Maize", "Rice", "Beans"]
bags = [120, 85, 60]
plt.bar(crops, bags)
plt.title("Crop Harvest in Morogoro (Bags)")
plt.xlabel("Crop Type")
plt.ylabel("Number of Bags")
plt.show()
This bar plot makes it easy to see that maize had the highest harvest, while beans had the lowest.
Seaborn is another library built on top of matplotlib. It produces more attractive plots with less code. First, import it:
import seaborn as sns
To create the same bar plot with seaborn:
import matplotlib.pyplot as plt
import seaborn as sns
crops = ["Maize", "Rice", "Beans"]
bags = [120, 85, 60]
sns.barplot(x=crops, y=bags)
plt.title("Crop Harvest in Morogoro")
plt.show()
Seaborn automatically adds colours and improves the style.
| Data Situation | Recommended Plot |
|---|---|
| Changes over time (e.g., daily sales) | Line plot |
| Comparing categories (e.g., exam scores by class) | Bar plot |
| Finding relationships between two numbers (e.g., age vs. height) | Scatter plot |
| Showing how data is spread out (e.g., distribution of prices) | Histogram |
Choosing the correct plot helps your audience understand the data quickly and accurately.
A shop owner in Mwanza can use basic plots to track daily sales and decide which items to stock more. For example, plotting the weekly sales of different sodas shows which flavour sells best, helping the owner order the right amount and avoid running out of popular drinks or wasting money on slow-selling stock.
Jinsi utakavyotathminiwaTathmini ya mchakato · Tathmini ya matokeo · Tathmini ya maarifa
Tathmini ya mchakato
- Use data analysis libraries to generate plots.
- Label and style plots appropriately.
Tathmini ya matokeo
Visualizations (e.g., bar charts, scatter plots) that effectively communicate data insights.
Tathmini ya maarifa
Detailed knowledge of:
Method used: The student should explain how to:
- Demonstrate plotting techniques and customization.
Principles: The student should explain the principles of:
- Data visualization.
Theories: The student should explain:
- Axes, scales, and color schemes.
Circumstantial knowledge:
Detailed knowledge about:
- Selecting visualizations that align with the data type and target audience.
Swali
What is the correct import statement to use matplotlib for creating plots?
Ingia ili kuwasilisha jibu lako na lihesabiwe katika umahiri wako.
Ingia ili kufanya mazoeziMwalimu
Umekwama? Niulize chochote kuhusu mada hii.
Ingia ili kumuuliza Mwalimu wa AI wa Sonza kuhusu swali hili.
Ingia ili kuulizaMajadiliano
Hakuna maswali bado