Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Yes, it is possible to traverse through different line styles in matplotlib. This can be done by passing a list of line styles to the linestyle parameter of the plot function. For example, to plot a line with alternating dash-dot, dotted and dashed styles, one can use the following code:

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]

fig, ax = plt.subplots()

line_styles = ['-.', ':', '--']

for i, style in enumerate(line_styles):
    ax.plot(x, y, linestyle=style, label=f'Line {i+1}')

ax.legend()
plt.show()

This will produce a plot with three lines, each with a different line style.