Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PC×
Skip to content
World desk3 min

How to Plot NumPy Arrays with Matplotlib in Python

Choose Matplotlib’s plot() for paired x-y arrays and imshow() for matrices or images. Learn how to label plots, interpret image coordinates, and compare arrays in panels.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use Axes.plot() for paired x-y values and Axes.imshow() for a matrix, image, or two-dimensional field. Start by creating a figure and axes with plt.subplots(), then choose the plotting method that matches what the array represents.

Plot paired NumPy values with plot()

When each x value corresponds to a y value, pass both arrays to ax.plot(x, y). This example samples a sine curve, labels its axes, and displays the figure:

import matplotlib.pyplot as plt
import numpy as np

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

fig, ax = plt.subplots()
ax.plot(x, y)
ax.set_xlabel("x")
ax.set_ylabel("sin(x)")
ax.set_title("Sine curve")
plt.show()

Here, x and y are paired coordinates, and the axis labels explain their meaning. Matplotlib’s Quick start guide describes a Figure as the container and an Axes as the region where data is plotted. It recommends the plt.subplots() pattern for creating them; using methods on ax keeps the plot explicit and makes it easier to add panels later.

For a sequence of y values whose x positions mean sample number, ax.plot(y) is a convenience form: the horizontal positions represent the samples rather than a separate measured x array. Use ax.plot(x, y) when you have meaningful x coordinates.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Show a matrix or image with imshow()

Use imshow when a two-dimensional array represents a raster image or a field arranged on a grid, rather than a single x-y series. A scalar matrix has shape (M, N), where rows and columns define its grid. For scalar values, Matplotlib normalizes the numbers and maps them to colors through a colormap; the colors are a visual encoding, not colors stored in the matrix.

fig, ax = plt.subplots()
image = ax.imshow(matrix, cmap="viridis")
fig.colorbar(image, ax=ax, label="value")
ax.set_title("Matrix values")
plt.show()

The colorbar helps readers relate displayed colors to values. For grayscale intensity data, choose a grayscale colormap and set vmin and vmax when those limits match the data scale. The imshow API documentation describes scalar, RGB, and RGBA input: RGB image arrays have shape (M, N, 3), while RGBA arrays have shape (M, N, 4). In those color arrays, the final dimension supplies the color channels directly.

Set image orientation and coordinate meaning

By default, imshow places pixel centers at integer coordinates, with the origin at the center of pixel (0, 0). That means the axes ordinarily indicate array indices, not necessarily physical or scientific coordinates.

  • Use origin to choose whether the first row appears at the top or bottom.
  • Use extent when the axes should show meaningful data bounds instead of pixel-index coordinates.
  • Choose interpolation deliberately. Resampling can change appearance through smoothing or aliasing when the displayed image size differs from the array dimensions.

These settings are documented in the imshow API and Matplotlib’s image interpolation guide. If a matrix represents measurements over real-world coordinates, configure the displayed bounds so readers do not mistake row and column indices for those measurements.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Compare multiple arrays in panels

For separate series or matrices that should be viewed together, create a grid of axes with plt.subplots(rows, columns) and draw each dataset on its corresponding axes. Shared scales can make comparisons easier when the data are comparable.

fig, axs = plt.subplots(2, 2, sharex="all", sharey="all")

axs[0, 0].plot(x, y1)
axs[0, 1].plot(x, y2)
axs[1, 0].plot(x, y3)
axs[1, 1].plot(x, y4)

plt.show()

With a multi-row, multi-column layout, axs is indexed by row and column, as in axs[0, 1]. For other layouts, the returned value can be a single Axes, a one-dimensional collection, or a two-dimensional grid depending on the requested layout and the squeeze setting. Matplotlib’s subplots documentation covers shared axes options: True or 'all', 'row', 'col', or independent axes.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Decide whether to call plt.show()

In a script or an environment that does not automatically render figures, plt.show() displays the plot. Some interactive environments display figures without an explicit call, so whether to include it depends on how the code is being run. Matplotlib’s Quick start guide demonstrates plt.show() and notes that it can be omitted in some environments.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.