0
0
Matplotlibdata~20 mins

Line styles (solid, dashed, dotted) in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Line Style Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What line style is used in this plot code?
Look at the code below that draws a line using matplotlib. What line style will the line have?
Matplotlib
import matplotlib.pyplot as plt
plt.plot([0, 1, 2], [0, 1, 0], linestyle='--')
plt.show()
ADashed line
BSolid line
CDotted line
DDash-dot line
Attempts:
2 left
💡 Hint
The linestyle argument controls the pattern of the line.
data_output
intermediate
2:00remaining
How many lines are solid in this plot code?
The code below plots three lines with different line styles. How many lines will be solid?
Matplotlib
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [1, 2, 3], linestyle='-')
plt.plot([1, 2, 3], [3, 2, 1], linestyle=':')
plt.plot([1, 2, 3], [2, 2, 2], linestyle='--')
plt.show()
A1
B0
C2
D3
Attempts:
2 left
💡 Hint
Check the linestyle argument for each plot call.
visualization
advanced
2:00remaining
Identify the line style of the green line in this plot
This code plots three lines with colors and line styles. What is the line style of the green line?
Matplotlib
import matplotlib.pyplot as plt
plt.plot([0, 1, 2], [2, 1, 0], color='red', linestyle=':')
plt.plot([0, 1, 2], [0, 1, 2], color='green', linestyle='-.')
plt.plot([0, 1, 2], [1, 1, 1], color='blue', linestyle='--')
plt.show()
ADashed line
BDash-dot line
CSolid line
DDotted line
Attempts:
2 left
💡 Hint
Look at the linestyle argument for the green line.
🧠 Conceptual
advanced
2:00remaining
What error occurs with this invalid linestyle?
What error will this code produce when run?
Matplotlib
import matplotlib.pyplot as plt
plt.plot([0, 1], [1, 0], linestyle='---')
plt.show()
ANo error, plots a solid line
BSyntaxError: invalid syntax
CTypeError: linestyle must be a string
DValueError: Unrecognized linestyle string
Attempts:
2 left
💡 Hint
Check if '---' is a valid linestyle in matplotlib.
🔧 Debug
expert
2:00remaining
Why does this plot not show a dotted line?
The code below is intended to plot a dotted line but does not. Why?
Matplotlib
import matplotlib.pyplot as plt
plt.plot([0, 1, 2], [0, 1, 0], linestyle='..')
plt.show()
ABecause the plot function requires linewidth to show dotted lines
BBecause '..' is interpreted as two dots, producing a dotted line
CBecause '..' is not a valid linestyle, matplotlib defaults to solid line
DBecause linestyle must be a tuple, not a string
Attempts:
2 left
💡 Hint
Check the valid linestyle strings in matplotlib.