📖 Scenario: You are working on a simple data visualization project. You want to draw lines on a graph with different colors and thicknesses to make the chart easy to understand.
🎯 Goal: Create a line plot with three lines. Each line should have a specific color and line width as given.
📋 What You'll Learn
Create three lists of y-values for three lines
Create a variable for the x-values
Plot each line with a specific color and line width
Display the plot
💡 Why This Matters
🌍 Real World
Line colors and widths help make charts clearer and easier to read in reports, presentations, and dashboards.
💼 Career
Data scientists and analysts use line styling to highlight important trends and differences in data visualizations.
Progress0 / 4 steps
1
Create data for the lines
Create a list called x with values [1, 2, 3, 4, 5]. Create three lists called y1, y2, and y3 with values [1, 4, 9, 16, 25], [2, 3, 5, 7, 11], and [5, 7, 6, 8, 7] respectively.
Matplotlib
Hint
Use square brackets to create lists with the exact numbers given.
2
Import matplotlib and set up the plot
Import matplotlib.pyplot as plt. Create a variable called fig, ax by calling plt.subplots().
Matplotlib
Hint
Use import matplotlib.pyplot as plt and plt.subplots() to prepare the plot.
3
Plot the lines with colors and widths
Use ax.plot() to plot the three lines with x and y1, y2, y3. Set the line colors to 'red', 'green', and 'blue' respectively. Set the line widths to 2, 4, and 6 respectively.
Matplotlib
Hint
Use color='colorname' and linewidth=number inside ax.plot().
4
Display the plot
Use plt.show() to display the plot with the three colored lines of different widths.
Matplotlib
Hint
Use plt.show() to open the plot window.
Practice
(1/5)
1. Which parameter in matplotlib.pyplot.plot() changes the color of the line?
easy
A. linestyle
B. color
C. linewidth
D. marker
Solution
Step 1: Understand the purpose of parameters in plot()
The color parameter sets the line color, while linewidth changes thickness.
Step 2: Identify the parameter for color
Among the options, only color directly changes the line color.
Final Answer:
color -> Option B
Quick Check:
Line color = color [OK]
Hint: Remember: color changes color, linewidth changes thickness [OK]
Common Mistakes:
Confusing linewidth with color
Using linestyle to change color
Trying to use marker for color
2. Which of the following is the correct syntax to set a red line with thickness 3 in matplotlib.pyplot.plot()?
easy
A. plt.plot(x, y, color='red', linewidth=3)
B. plt.plot(x, y, linewidth='red', color=3)
C. plt.plot(x, y, color=3, linewidth='red')
D. plt.plot(x, y, width=3, color='red')
Solution
Step 1: Check parameter names and values
The correct parameters are color with a color string and linewidth with a number.
Step 2: Validate plt.plot(x, y, color='red', linewidth=3) syntax
plt.plot(x, y, color='red', linewidth=3) uses color='red' and linewidth=3, which is correct syntax.
Final Answer:
plt.plot(x, y, color='red', linewidth=3) -> Option A
Quick Check:
Correct parameter names and types = plt.plot(x, y, color='red', linewidth=3) [OK]
Hint: Use color='colorname' and linewidth=number [OK]
Common Mistakes:
Swapping color and linewidth values
Using wrong parameter names like width
Passing numbers as color
3. What will be the color and thickness of the line drawn by this code?
import matplotlib.pyplot as plt
x = [1, 2, 3]
y = [4, 5, 6]
plt.plot(x, y, color='green', linewidth=5)
plt.show()
medium
A. Blue line with thickness 5
B. Green line with thickness 1
C. Green line with thickness 5
D. Red line with thickness 5
Solution
Step 1: Analyze color parameter
The code sets color='green', so the line color is green.
Step 2: Analyze linewidth parameter
The code sets linewidth=5, so the line thickness is 5 (thicker than default).
Final Answer:
Green line with thickness 5 -> Option C
Quick Check:
color='green' and linewidth=5 = Green line with thickness 5 [OK]
Hint: Check color and linewidth values directly in plot() [OK]
Common Mistakes:
Ignoring linewidth value
Assuming default thickness
Confusing color names
4. Identify the error in this code snippet:
import matplotlib.pyplot as plt
plt.plot([1,2,3], [4,5,6], color=5, linewidth='thick')
plt.show()
medium
A. Missing plt.show() call
B. color should be a number, linewidth should be a string
C. color and linewidth parameters are correct
D. color should be a string, linewidth should be a number
Solution
Step 1: Check color parameter type
color must be a color name string or code, not a number like 5.
Step 2: Check linewidth parameter type
linewidth must be a number (int or float), not a string like 'thick'.
Final Answer:
color should be a string, linewidth should be a number -> Option D
Quick Check:
Parameter types must match expected types [OK]
Hint: color='name', linewidth=number; no numbers for color [OK]
Common Mistakes:
Passing numbers as color
Passing strings as linewidth
Ignoring type errors
5. You want to plot two lines on the same graph: one thick blue line and one thin orange line. Which code correctly does this?
hard
A. plt.plot(x, y1, color='blue', linewidth=6)
plt.plot(x, y2, color='orange', linewidth=1)
B. plt.plot(x, y1, color='orange', linewidth=6)
plt.plot(x, y2, color='blue', linewidth=1)
C. plt.plot(x, y1, linewidth='thick', color='blue')
plt.plot(x, y2, linewidth='thin', color='orange')
D. plt.plot(x, y1, color=6, linewidth='blue')
plt.plot(x, y2, color=1, linewidth='orange')
Solution
Step 1: Match colors to lines
The thick line should be blue, so color='blue' with large linewidth for y1.
Step 2: Match thickness and color for second line
The thin line should be orange, so color='orange' with small linewidth for y2.
Step 3: Validate parameter types
plt.plot(x, y1, color='blue', linewidth=6)
plt.plot(x, y2, color='orange', linewidth=1) uses correct parameter names and types for both lines.