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
Bar Chart
Draw vertical, horizontal, and grouped bars.
Scatter Plot
Visualize relationships with size and color encodings.
Subplots
Arrange multiple axes with subplots and GridSpec.
Labels and Legend
Annotate plots with text, arrows, and legend options.
Styles and Themes
Apply built-in styles and customize rcParams.
Save Figure
Export figures to PNG, PDF, and SVG with DPI control.