0
0
Matplotlibdata~5 mins

Line styles (solid, dashed, dotted) in Matplotlib

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