numpy
seaborn
data visualization
python
plotting

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.

Practice ML system design

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.

python
1import numpy as np
2import seaborn as sns
3import matplotlib.pyplot as plt
4
5values = np.array([3, 4, 4, 5, 7, 8, 8, 9, 10, 12])
6
7sns.histplot(values, bins=5, kde=True)
8plt.title("Value Distribution")
9plt.show()

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.

python
1import numpy as np
2import seaborn as sns
3import matplotlib.pyplot as plt
4
5values = np.array([2.1, 2.4, 2.8, 3.5, 3.7, 4.0])
6positions = np.arange(values.size)
7
8sns.lineplot(x=positions, y=values, marker="o")
9plt.xlabel("Index")
10plt.ylabel("Value")
11plt.title("Sequence Stored in a NumPy Array")
12plt.show()

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.

python
1import numpy as np
2import seaborn as sns
3import matplotlib.pyplot as plt
4
5matrix = np.array([
6    [1.2, 2.4, 3.1],
7    [0.8, 1.9, 2.7],
8    [3.5, 2.1, 1.4]
9])
10
11sns.heatmap(matrix, annot=True, cmap="viridis", fmt=".1f")
12plt.title("Matrix Heatmap")
13plt.show()

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:

python
1points = np.array([
2    [1.0, 2.5],
3    [2.0, 3.1],
4    [3.0, 3.8],
5    [4.0, 5.2],
6])
7
8sns.scatterplot(x=points[:, 0], y=points[:, 1])
9plt.xlabel("x")
10plt.ylabel("y")
11plt.title("Point Data Stored in a NumPy Array")
12plt.show()

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.

python
1import numpy as np
2import pandas as pd
3import seaborn as sns
4import matplotlib.pyplot as plt
5
6matrix = np.array([
7    [23, 17, 9],
8    [15, 19, 12],
9    [8, 11, 21]
10])
11
12frame = pd.DataFrame(matrix, index=["A", "B", "C"], columns=["X", "Y", "Z"])
13sns.heatmap(frame, annot=True, cmap="mako")
14plt.show()

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 x axis when plotting a sequence with lineplot.
  • 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 histplot for one-dimensional distributions and lineplot for ordered sequences.
  • Use heatmap for 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
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Practice ML system design

All Rights Reserved.