Matplotlib accepts NumPy arrays for x/y data. Plotting is typically done locally; this lesson shows array patterns that feed charts.
Arrays as plot coordinates
import numpy as np
x = np.linspace(0, 2 * np.pi, 100)
y = np.sin(x)
print('x shape:', x.shape, 'y min/max:', y.min(), y.max())
Histogram data
np.histogram(data, bins=10) returns counts and bin edges—Matplotlib's hist can use same bins for consistency.
2D images
Grayscale images are 2D float or uint8 arrays. imshow maps array values to colormap—shape (height, width).
Important interview questions and answers
- Q: Why linspace for plots?
A: Smooth evenly spaced x avoids jagged line charts. - Q: histogram return?
A: Tuple of (counts, bin_edges)—document bins for reproducibility.
Self-check
- Generate 50 sin(x) samples from 0 to 2π.
- What NumPy function bins data for histograms?
Tip: Build x with linspace before plotting locally.
Interview prep
- linspace for plots?
Smooth evenly spaced x coordinates.
- histogram?
np.histogram returns counts and bin edges.