Ask Your Question
1

Is it possible to traverse through different line styles in matplotlib?

asked 2023-06-28 21:46:27 +0000

plato gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2023-06-28 21:58:01 +0000

nofretete gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-06-28 21:46:27 +0000

Seen: 12 times

Last updated: Jun 28 '23