Skip to main contentSkip to solution

How can you create a dashed line in matplotlib?

Solution

✅ Correct Option: 1

The linestyle parameter (short form ls) of plt.plot() sets the line pattern. It accepts 'dashed' or the shorthand '--', for example plt.plot(x, y, linestyle='dashed').


Option 2: line_style is not a valid keyword, because matplotlib spells it without an underscore.

Option 3: style is not a line property of plt.plot(). The nearest match is plt.style.use(), which sets a whole plot theme.

Other linestyle values: 'solid' or '-', 'dotted' or ':', 'dashdot' or '-.'.

Keyboard Shortcuts

  • Left arrow: Previous question
  • Right arrow: Next question
  • S key: Jump to solution
  • Q key: Jump to question