Plotting numpy array using Seaborn
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Seaborn works very well with NumPy arrays, even though many examples online use pandas DataFrames. The main thing to remember is that the shape of the array should match the plot type you choose. A one-dimensional array fits distribution or line plots, while a two-dimensional array is a natural fit for heatmaps or paired x and y values.
Plot a One-Dimensional Array
For a one-dimensional array, a histogram is usually the simplest starting point. Seaborn accepts the array directly.
This is useful when you want to see how values are distributed. The kde=True option adds a smoothed density curve, which can help reveal the general shape of the data.
Plot Array Values Against Their Index
If the array represents an ordered sequence, use the index as the x axis and the values as the y axis.
This pattern is common for time steps, experiment runs, or any series where order matters.
Plot a Two-Dimensional Array as a Heatmap
A two-dimensional NumPy array maps naturally onto sns.heatmap, which is one of the most useful Seaborn plots for matrix-like data.
annot=True prints the actual values inside the cells, which is helpful for small matrices. For larger arrays, turn annotations off so the plot stays readable.
Plot Two Columns as x and y
Not every two-dimensional array is a matrix. Sometimes each row is a point with two coordinates. In that case, slice the array into x and y columns and use a scatter plot:
When a DataFrame Helps
You do not need pandas for most basic plots, but converting a NumPy array to a DataFrame can help when you want meaningful row and column labels.
The plotting data is still the same, but labels make the chart much easier to interpret.
Style and Figure Size Still Matter
Even when the plotting call is correct, a chart can look cramped or noisy if the figure size and theme are left at defaults. In practice, setting a simple theme and a readable figure size often makes the difference between a technically correct plot and a chart that is actually useful.
Match Plot Type to Array Shape
A practical mental model is simple:
- One-dimensional arrays fit histograms, scatter plots, and line plots.
- Two-dimensional arrays fit heatmaps and clustered matrix views.
- If your array has more than two dimensions, reshape or slice it before plotting.
Once you think about the structure first, choosing the Seaborn API becomes much easier.
Common Pitfalls
- Passing a two-dimensional array into a plot that expects one-dimensional input.
- Forgetting to create an
xaxis when plotting a sequence withlineplot. - Using annotations on a large heatmap, which quickly becomes unreadable.
- Assuming Seaborn requires pandas. It often works directly with NumPy arrays.
Summary
- Seaborn can plot NumPy arrays directly without much setup.
- Use
histplotfor one-dimensional distributions andlineplotfor ordered sequences. - Use
heatmapfor two-dimensional arrays and matrices. - Convert to a DataFrame only when labels or tabular semantics improve readability.
- Always match the plot type to the array shape before debugging the code.
Related reading
- plotting spectrogram in audio analysis
- Plotting time on the independent axis
- Poisson Regression in statsmodels and R
- Popularity decay algorithm for popular website posts
- Poetry fails to install tensorflow
- Pool.apply_async nested function is not executed
- Predict classes or class probabilities?
- Predict NA missing values with machine learning
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.