Matplotlib Basics (5.
2) - Code & Test
Code Examples
Line Plot
import [Link] as plt
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
[Link](x, y, color='blue', marker='o', label='Line')
[Link]("Simple Line Plot")
[Link]("X Axis")
[Link]("Y Axis")
[Link]()
[Link](True)
[Link]()
Scatter Plot
[Link](x, y, color='red', label='Points')
[Link]("Simple Scatter Plot")
[Link]("X Axis")
[Link]("Y Axis")
[Link]()
[Link](True)
[Link]()
Bar Chart
categories = ['Apple', 'Banana', 'Mango', 'Orange']
values = [50, 40, 70, 60]
[Link](categories, values, color='green')
[Link]("Fruit Sales")
[Link]("Fruits")
[Link]("Quantity Sold")
[Link]()
Histogram
import numpy as np
data = [Link](100, 15, 1000)
[Link](data, bins=20, color='purple', edgecolor='black')
[Link]("Histogram Example")
[Link]("Value")
[Link]("Frequency")
[Link]()
Multiple Line Plots
x = [1, 2, 3, 4, 5]
y1 = [1, 4, 9, 16, 25]
y2 = [1, 8, 27, 64, 125]
[Link](x, y1, label='y = x^2', marker='o')
[Link](x, y2, label='y = x^3', marker='s')
[Link]("Multiple Line Plots")
[Link]("X Value")
[Link]("Y Value")
[Link]()
[Link](True)
[Link]()
Custom Labels, Title, Legend
x = [10, 20, 30, 40, 50]
y = [3, 7, 2, 8, 5]
[Link](x, y, label="Data Line", linestyle='--', color='brown', marker='^')
[Link]("Customized Plot Example", fontsize=14)
[Link]("Input X", fontsize=12)
[Link]("Output Y", fontsize=12)
[Link](loc='lower right')
[Link](True)
[Link]()
Practice Test
Part A: Multiple Choice Questions
Q1. Which function is used to create a line plot in Matplotlib?
A) [Link]()
B) [Link]()
C) [Link]()
D) [Link]()
Q2. What does [Link]("X Axis") do?
A) Draws a vertical line
B) Sets label of X-axis
C) Sets label of Y-axis
D) Plots a histogram
Q3. Which function is used to display the plot window?
A) [Link]()
B) [Link]()
C) [Link]()
D) [Link]()
Q4. What parameter controls the number of bins in a histogram?
A) bar
B) scatter
C) bins
D) ticks
Q5. Which method is used to create a scatter plot?
A) [Link]()
B) [Link]()
C) [Link]()
D) [Link]()
Part B: Code Output Prediction
Q6. What will this code plot?
x = [1, 2, 3]
y = [4, 1, 5]
[Link](x, y)
[Link]()
A) Line plot
B) Scatter plot
C) Bar chart
D) Pie chart
Q7. In this code, where will the legend appear?
[Link]([1, 2], [2, 4], label="Line")
[Link](loc='upper left')
[Link]()
A) No legend shown
B) Bottom right
C) Top left
D) Top right
Part C: Fill in the Blanks
Q8. _______ function is used to draw a histogram in matplotlib.
Q9. [Link]() is used to show _______ on the graph.
Bonus (Short Code)
Q10. Write code to plot a scatter plot of x = [1, 2, 3] and y = [3, 2, 1] with red color.