Skip to content
Matplotlib

Line Plot

Plot lines with markers, colors, and styles.

By EZ4Code Team
lineplot

Code

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 2 * np.pi, 100)

plt.plot(x, np.sin(x), label="sin", color="steelblue", linewidth=2)
plt.plot(x, np.cos(x), label="cos", color="coral", linestyle="--",
         marker="o", markevery=10)

plt.title("Sine vs Cosine")
plt.xlabel("x")
plt.ylabel("amplitude")
plt.legend()
plt.grid(True, alpha=0.3)
plt.tight_layout()
plt.show()

Explanation

plot() draws one or more lines with style arguments for color, linestyle, marker, and width. Calling xlabel, ylabel, title, and legend annotates the figure before show() renders it. tight_layout prevents label overlap when the figure is displayed or saved.

More Matplotlib Snippets