What are the three basic line styles in matplotlib?
The three basic line styles are solid, dashed, and dotted lines.
Click to reveal answer
beginner
How do you specify a dashed line style in matplotlib?
Use the argument linestyle='--' in the plot function to create a dashed line.
Click to reveal answer
beginner
What does linestyle=':' do in a matplotlib plot?
It creates a dotted line style for the plotted line.
Click to reveal answer
intermediate
How can you combine color and line style in a matplotlib plot?
You can specify both using color='colorname' and linestyle='style' in the plot function, for example plt.plot(x, y, color='red', linestyle='--').
Click to reveal answer
beginner
What is the default line style in matplotlib if none is specified?
The default line style is a solid line, which is linestyle='-'.
Click to reveal answer
Which linestyle code creates a solid line in matplotlib?
A-
B--
C:
D-.
✗ Incorrect
The code '-' specifies a solid line style in matplotlib.
How do you make a dotted line in matplotlib?
Alinestyle='--'
Blinestyle=':'
Clinestyle='-'
Dlinestyle='-.'
✗ Incorrect
The linestyle ':' creates a dotted line in matplotlib.
What argument changes the line style in matplotlib's plot function?
Acolor
Blinewidth
Clinestyle
Dmarker
✗ Incorrect
The 'linestyle' argument controls the style of the line.
Which line style code creates a dashed line?
A-.
B-
C:
D--
✗ Incorrect
The code '--' creates a dashed line style.
If you want a red dashed line, which is the correct way to write it?
Aplt.plot(x, y, color='red', linestyle='--')
Bplt.plot(x, y, linestyle='-', color='red')
Cplt.plot(x, y, color='red', linestyle=':')
Dplt.plot(x, y, linestyle='--')
✗ Incorrect
You specify both color and linestyle explicitly: color='red' and linestyle='--'.
Explain how to change line styles in matplotlib plots and give examples of solid, dashed, and dotted lines.
Think about the linestyle codes and how you add them to the plot function.
You got /3 concepts.
Describe how you can customize a line's appearance using both color and line style in matplotlib.
Remember you can add multiple style arguments in plt.plot()
You got /3 concepts.
Practice
(1/5)
1. Which linestyle value in matplotlib creates a solid line?
easy
A. "-"
B. "--"
C. ":"
D. "-."
Solution
Step 1: Understand line style symbols in matplotlib
In matplotlib, the linestyle parameter uses symbols: "-" for solid, "--" for dashed, ":" for dotted, and "-." for dash-dot.
Step 2: Match symbol to solid line
The symbol "-" represents a solid line style.
Final Answer:
"-" -> Option A
Quick Check:
Solid line = "-" [OK]
Hint: Solid line uses a single dash "-" [OK]
Common Mistakes:
Confusing dashed "--" with solid
Using ":" for solid line
Mixing dash-dot "-." with solid
2. Which of the following is the correct syntax to plot a dashed line using matplotlib.pyplot?
easy
A. plt.plot(x, y, linestyle='-')
B. plt.plot(x, y, linestyle=':')
C. plt.plot(x, y, linestyle='--')
D. plt.plot(x, y, linestyle='solid')
Solution
Step 1: Recall linestyle values for dashed lines
Dashed lines in matplotlib are specified by the string "--" or the word "dashed".
Step 2: Identify correct syntax
Among the options, only linestyle='--' correctly sets a dashed line.
Final Answer:
plt.plot(x, y, linestyle='--') -> Option C
Quick Check:
Dashed line = "--" [OK]
Hint: Dashed line uses double dash "--" [OK]
Common Mistakes:
Using single dash '-' for dashed line
Using ':' which is dotted line
Using 'solid' string incorrectly
3. What will be the line style of the plot generated by this code?
import matplotlib.pyplot as plt
x = [1, 2, 3]
y = [2, 3, 5]
plt.plot(x, y, linestyle=':')
plt.show()
medium
A. Dotted line
B. Dashed line
C. Solid line
D. Dash-dot line
Solution
Step 1: Identify linestyle parameter value
The code uses linestyle=':', which in matplotlib means a dotted line.
Step 2: Understand line style mapping
"-" is solid, "--" is dashed, ":" is dotted, and "-." is dash-dot.
Final Answer:
Dotted line -> Option A
Quick Check:
Colon ":" means dotted line [OK]
Hint: Colon ":" means dotted line style [OK]
Common Mistakes:
Thinking ':' is dashed line
Confusing dash-dot '-.' with dotted
Assuming default solid line
4. The following code is intended to plot a dashed line but does not work as expected. What is the error?
import matplotlib.pyplot as plt
x = [0, 1, 2]
y = [0, 1, 4]
plt.plot(x, y, linestyle='dash')
plt.show()
medium
A. The list y has wrong values
B. The linestyle value 'dash' is invalid; use '--' instead
C. Missing plt.show() call
D. The variable names x and y are incorrect
Solution
Step 1: Check valid linestyle values in matplotlib
Matplotlib accepts 'solid', 'dashed', 'dashdot', 'dotted' as strings or symbols like '--', '-.', ':'. The string 'dash' is invalid.
Step 2: Identify the error
The code uses linestyle='dash', which is not a valid linestyle value. Use '--' for dashed lines.
Final Answer:
The linestyle value 'dash' is invalid; use '--' instead -> Option B
Quick Check:
Invalid 'dash'; use '--' [OK]
Hint: 'dash' invalid for dashed; use '--' [OK]
Common Mistakes:
Assuming 'dash' string works
Thinking plt.show() is missing
Blaming variable names or data values
5. You want to plot two lines on the same graph: the first one solid and the second one dotted. Which code correctly sets these line styles?
hard
A. plt.plot(x, y1, linestyle='-.')
plt.plot(x, y2, linestyle=':')
B. plt.plot(x, y1, linestyle=':')
plt.plot(x, y2, linestyle='-')
C. plt.plot(x, y1, linestyle='-')
plt.plot(x, y2, linestyle='--')
D. plt.plot(x, y1, linestyle='-')
plt.plot(x, y2, linestyle=':')
Solution
Step 1: Identify line styles for solid and dotted
Solid line uses "-" and dotted line uses ":" in matplotlib.
Step 2: Match code to required styles
plt.plot(x, y1, linestyle='-')
plt.plot(x, y2, linestyle=':') uses linestyle='-' for the first line (solid) and linestyle=':' for the second line (dotted), which is correct.
Final Answer:
plt.plot(x, y1, linestyle='-') and plt.plot(x, y2, linestyle=':') -> Option D
Quick Check:
Solid = '-', Dotted = ':' [OK]
Hint: Solid '-' and dotted ':' line styles together [OK]
Common Mistakes:
Mixing dashed '--' with dotted ':'
Using dash-dot '-.' instead of dotted
Swapping line styles order
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.