By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
Basic plotting with Matplotlib is a fundamental skill for data visualization in Python. It allows you to create clear, informative visuals that communicate data insights effectively. This topic is crucial for professionals and exam candidates because it forms the backbone of data analysis and reporting. Misunderstanding or misusing Matplotlib can lead to misinterpreted data, poor decision-making, and ineffective communication. For instance, a poorly designed scatter plot might obscure a critical correlation, leading to incorrect business strategies.
pip install matplotlib
⚠️: Verify the installation by importing Matplotlib in a Python script.
Import Matplotlib
import matplotlib.pyplot as plt
⚠️: Always use the alias plt for consistency.
plt
Create a Line Plot
plt.plot()
python x = [0, 1, 2, 3, 4] y = [0, 1, 4, 9, 16] plt.plot(x, y) plt.show()
⚠️: Always call plt.show() to display the plot.
plt.show()
Create a Bar Plot
plt.bar()
python categories = ['A', 'B', 'C', 'D'] values = [4, 7, 1, 8] plt.bar(categories, values) plt.show()
⚠️: Ensure categories and values are correctly paired.
Create a Scatter Plot
plt.scatter()
python x = [5, 7, 8, 7, 2, 17, 2, 9, 4, 11, 12, 9, 6] y = [99, 86, 87, 88, 111, 86, 103, 87, 94, 78, 77, 85, 86] plt.scatter(x, y) plt.show()
⚠️: Check for outliers that might distort the plot.
Customize Plots
python plt.plot(x, y) plt.title('Sample Line Plot') plt.xlabel('X-axis Label') plt.ylabel('Y-axis Label') plt.legend(['Data']) plt.grid(True) plt.show()
Experts view Matplotlib as a versatile toolkit for data storytelling. They focus on the narrative behind the data, using plots to highlight key insights rather than just displaying raw information. They think in terms of layers, building plots step-by-step to create a cohesive visual story.
Exam trap: Missing this step in a coding question.
The mistake: Using incorrect data types for plotting.
Exam trap: Questions involving mixed data types.
The mistake: Overlooking plot customization.
Exam trap: Questions asking for plot interpretation.
The mistake: Ignoring outliers in scatter plots.
Scenario: You have sales data for different products over a month.Question: Create a bar plot to compare the sales of each product.Solution:1. Import Matplotlib.2. Define the product names and sales values.3. Use plt.bar() to create the bar plot.4. Add titles and labels.5. Call plt.show().Answer:
import matplotlib.pyplot as plt products = ['Product A', 'Product B', 'Product C', 'Product D'] sales = [120, 150, 130, 170] plt.bar(products, sales) plt.title('Monthly Sales Comparison') plt.xlabel('Products') plt.ylabel('Sales') plt.show()
Why it works: Clearly compares sales data for different products.
Scenario: You have temperature data recorded every hour for a day.Question: Create a line plot to show the temperature trend.Solution:1. Import Matplotlib.2. Define the time and temperature data.3. Use plt.plot() to create the line plot.4. Add titles and labels.5. Call plt.show().Answer:
import matplotlib.pyplot as plt hours = list(range(24)) temperatures = [15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12] plt.plot(hours, temperatures) plt.title('Daily Temperature Trend') plt.xlabel('Hours') plt.ylabel('Temperature (°C)') plt.show()
Why it works: Shows the temperature trend over time.
Scenario: You have data on the relationship between study hours and exam scores.Question: Create a scatter plot to visualize the relationship.Solution:1. Import Matplotlib.2. Define the study hours and exam scores.3. Use plt.scatter() to create the scatter plot.4. Add titles and labels.5. Call plt.show().Answer:
import matplotlib.pyplot as plt study_hours = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] exam_scores = [50, 60, 70, 80, 90, 95, 98, 99, 100, 100] plt.scatter(study_hours, exam_scores) plt.title('Study Hours vs Exam Scores') plt.xlabel('Study Hours') plt.ylabel('Exam Scores') plt.show()
Why it works: Helps identify the correlation between study hours and exam scores.
plt.plot(x, y)
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.