0
0
Matplotlibdata~20 mins

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

Choose your learning style9 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.