Bird
Raised Fist0
Matplotlibdata~5 mins

Line styles (solid, dashed, dotted) in Matplotlib

Choose your learning style10 modes available
Introduction

Line styles help you show different types of lines in your charts. They make graphs easier to understand by using solid, dashed, or dotted lines.

When you want to compare multiple data series in one chart and need to tell them apart.
When you want to highlight a trend with a solid line and show predictions with a dashed line.
When you want to mark thresholds or limits with dotted lines on a plot.
When you want to make your graph clearer for people who see it in black and white.
When you want to add style or emphasis to certain parts of your chart.
Syntax
Matplotlib
plt.plot(x, y, linestyle='style')

# style can be 'solid', 'dashed', 'dotted', or shortcuts like '-', '--', ':'

You can use full names like 'solid' or shortcuts like '-' for solid.

Line style is set using the linestyle parameter in plt.plot().

Examples
This draws a solid line.
Matplotlib
plt.plot(x, y, linestyle='solid')
This draws a dashed line.
Matplotlib
plt.plot(x, y, linestyle='dashed')
This draws a dotted line.
Matplotlib
plt.plot(x, y, linestyle='dotted')
This is a shortcut for a dashed line.
Matplotlib
plt.plot(x, y, linestyle='--')
Sample Program

This program draws three lines with different styles: solid, dashed, and dotted. It also adds a legend to explain each line.

Matplotlib
import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y1 = [1, 4, 9, 16, 25]
y2 = [1, 3, 6, 10, 15]
y3 = [2, 3, 5, 7, 11]

plt.plot(x, y1, linestyle='solid', label='Solid line')
plt.plot(x, y2, linestyle='dashed', label='Dashed line')
plt.plot(x, y3, linestyle='dotted', label='Dotted line')

plt.legend()
plt.title('Line Styles Example')
plt.xlabel('X axis')
plt.ylabel('Y axis')
plt.show()
OutputSuccess
Important Notes

You can combine line styles with colors and markers for more clarity.

Using different line styles helps people who cannot see colors well.

Summary

Line styles change how lines look in a plot: solid, dashed, or dotted.

Use the linestyle parameter in plt.plot() to set the style.

Different styles help make your charts clearer and easier to read.

Practice

(1/5)
1. Which linestyle value in matplotlib creates a solid line?
easy
A. "-"
B. "--"
C. ":"
D. "-."

Solution

  1. 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.
  2. Step 2: Match symbol to solid line

    The symbol "-" represents a solid line style.
  3. Final Answer:

    "-" -> Option A
  4. 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

  1. Step 1: Recall linestyle values for dashed lines

    Dashed lines in matplotlib are specified by the string "--" or the word "dashed".
  2. Step 2: Identify correct syntax

    Among the options, only linestyle='--' correctly sets a dashed line.
  3. Final Answer:

    plt.plot(x, y, linestyle='--') -> Option C
  4. 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

  1. Step 1: Identify linestyle parameter value

    The code uses linestyle=':', which in matplotlib means a dotted line.
  2. Step 2: Understand line style mapping

    "-" is solid, "--" is dashed, ":" is dotted, and "-." is dash-dot.
  3. Final Answer:

    Dotted line -> Option A
  4. 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

  1. Step 1: Check valid linestyle values in matplotlib

    Matplotlib accepts 'solid', 'dashed', 'dashdot', 'dotted' as strings or symbols like '--', '-.', ':'. The string 'dash' is invalid.
  2. Step 2: Identify the error

    The code uses linestyle='dash', which is not a valid linestyle value. Use '--' for dashed lines.
  3. Final Answer:

    The linestyle value 'dash' is invalid; use '--' instead -> Option B
  4. 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

  1. Step 1: Identify line styles for solid and dotted

    Solid line uses "-" and dotted line uses ":" in matplotlib.
  2. 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.
  3. Final Answer:

    plt.plot(x, y1, linestyle='-') and plt.plot(x, y2, linestyle=':') -> Option D
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard