Data Visualization โ matplotlib & seaborn
Create publication-quality charts with matplotlib and seaborn โ line plots, bar charts, heatmaps, subplots, and interactive Plotly figures.
Part 1: What You Will Learn
- Create line and bar charts with matplotlib.
- Use seaborn for statistical plots based on DataFrames.
- Add titles, labels, legends, and layout adjustments.
- Choose chart types that match the question being asked.
Part 2: Key Concepts
Data visualization turns numerical patterns into shapes that are easier to compare. matplotlib gives low-level control over a figure, while seaborn provides higher-level statistical plotting functions that work naturally with pandas DataFrames.
Part 3: Topic-Specific Code Examples
# Install once: pip install matplotlib pandas seaborn
import matplotlib.pyplot as plt
months = ["Jan", "Feb", "Mar", "Apr", "May"]
revenue = [12000, 14500, 13800, 16200, 18100]
plt.plot(months, revenue, marker="o")
plt.title("Monthly Revenue")
plt.xlabel("Month")
plt.ylabel("Revenue (RM)")
plt.grid(True, alpha=0.3)
plt.tight_layout()
plt.show()import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
data = pd.DataFrame({
"course": ["Python", "Python", "Java", "Java", "C#", "C#"],
"mark": [82, 74, 68, 79, 91, 85],
})
sns.barplot(data=data, x="course", y="mark", errorbar=None)
plt.title("Average Mark by Course")
plt.xlabel("Course")
plt.ylabel("Average Mark")
plt.tight_layout()
plt.show()Part 4: How the Example Works
A line chart is appropriate for values ordered over time. The seaborn bar plot receives a DataFrame and calculates the average mark for each course automatically. Always label axes clearly and avoid adding visual decoration that does not help the reader interpret the data.
Part 5: Hands-On Practice
Mini project โ Sales Dashboard Charts. Build a DataFrame with month, region, and revenue. Create one line chart for monthly revenue, one bar chart comparing regions, and one seaborn box plot showing the distribution of transaction values.
Part 6: Next Steps
Run and modify the examples in Visual Studio 2026, then continue to Lesson 24. Return to Python Tutorial Home to review the complete curriculum.