Bird
Raised Fist0
Matplotlibdata~20 mins

Line colors and width in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Line Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the color of the line in this plot?
Look at the code below that plots a line. What color will the line be?
Matplotlib
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [4, 5, 6], color='green')
plt.show()
ABlue
BRed
CGreen
DBlack
Attempts:
2 left
💡 Hint
The color is set by the 'color' parameter in plt.plot.
❓ Predict Output
intermediate
2:00remaining
What is the width of the line plotted?
Check the code below. What is the width of the line shown in the plot?
Matplotlib
import matplotlib.pyplot as plt
plt.plot([0, 1, 2], [2, 3, 4], linewidth=5)
plt.show()
A5
B10
C0.5
D1 (default width)
Attempts:
2 left
💡 Hint
The linewidth parameter controls the thickness of the line.
🔧 Debug
advanced
2:00remaining
Why does this code raise an error?
This code tries to plot a red line with width 3 but raises an error. What is the cause?
Matplotlib
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [3, 2, 1], color='red', linewidth=3)
plt.show()
Alinewidth should be line_width, so it's an invalid argument.
BThe color value red is not in quotes, causing a NameError.
Cplt.plot does not accept color or linewidth arguments.
DThe lists for x and y have different lengths.
Attempts:
2 left
💡 Hint
String values must be in quotes in Python.
❓ data_output
advanced
2:00remaining
What is the RGB color code for the line?
The code below plots a line with color='cyan'. What is the RGB tuple for this color?
Matplotlib
import matplotlib.pyplot as plt
line = plt.plot([0, 1], [1, 0], color='cyan')
plt.show()
A(0.0, 1.0, 1.0)
B(1.0, 0.0, 0.0)
C(0.0, 0.0, 1.0)
D(1.0, 1.0, 0.0)
Attempts:
2 left
💡 Hint
Cyan is a mix of green and blue at full intensity.
🚀 Application
expert
3:00remaining
How to plot two lines with different colors and widths?
You want to plot two lines on the same graph: one red line with width 2, and one blue line with width 4. Which code does this correctly?
A
import matplotlib.pyplot as plt
plt.plot([1,2,3], [3,2,1], color='red', linewidth=2)
plt.plot([1,2,3], [1,2,3], color='blue', linewidth=4)
plt.show()
B
import matplotlib.pyplot as plt
plt.plot([1,2,3], [3,2,1], 'red', 2)
plt.plot([1,2,3], [1,2,3], 'blue', 4)
plt.show()
C
import matplotlib.pyplot as plt
plt.plot([1,2,3], [3,2,1], color='red', width=2)
plt.plot([1,2,3], [1,2,3], color='blue', width=4)
plt.show()
D
import matplotlib.pyplot as plt
plt.plot([1,2,3], [3,2,1], c='red', linewidth=2)
plt.plot([1,2,3], [1,2,3], c='blue', linewidth=4)
plt.show()
Attempts:
2 left
💡 Hint
Use color= and linewidth= parameters correctly.

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

  1. Step 1: Understand the purpose of parameters in plot()

    The color parameter sets the line color, while linewidth changes thickness.
  2. Step 2: Identify the parameter for color

    Among the options, only color directly changes the line color.
  3. Final Answer:

    color -> Option B
  4. 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

  1. Step 1: Check parameter names and values

    The correct parameters are color with a color string and linewidth with a number.
  2. 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.
  3. Final Answer:

    plt.plot(x, y, color='red', linewidth=3) -> Option A
  4. 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

  1. Step 1: Analyze color parameter

    The code sets color='green', so the line color is green.
  2. Step 2: Analyze linewidth parameter

    The code sets linewidth=5, so the line thickness is 5 (thicker than default).
  3. Final Answer:

    Green line with thickness 5 -> Option C
  4. 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

  1. Step 1: Check color parameter type

    color must be a color name string or code, not a number like 5.
  2. Step 2: Check linewidth parameter type

    linewidth must be a number (int or float), not a string like 'thick'.
  3. Final Answer:

    color should be a string, linewidth should be a number -> Option D
  4. 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

  1. Step 1: Match colors to lines

    The thick line should be blue, so color='blue' with large linewidth for y1.
  2. Step 2: Match thickness and color for second line

    The thin line should be orange, so color='orange' with small linewidth for y2.
  3. 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.
  4. Final Answer:

    plt.plot(x, y1, color='blue', linewidth=6) plt.plot(x, y2, color='orange', linewidth=1) -> Option A
  5. Quick Check:

    Correct colors and linewidths = plt.plot(x, y1, color='blue', linewidth=6) plt.plot(x, y2, color='orange', linewidth=1) [OK]
Hint: Assign color and linewidth clearly for each line [OK]
Common Mistakes:
  • Swapping colors and linewidths
  • Using strings for linewidth
  • Passing numbers as colors
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard