Recently, while working on a data visualization project, I needed to save a NumPy array as a PNG image in Python. At first, it seemed simple, but I quickly realized that there are a few details that can make or break the output quality.
As someone who’s been coding in Python, I’ve often used Matplotlib not just for plotting data but also for saving and exporting images. In this tutorial, I’ll walk you through how to save NumPy array as PNG file using Matplotlib, step-by-step.
I’ll also share a few different methods that I’ve personally used in real-world projects, from saving grayscale arrays to creating transparent PNGs.
Method 1 – Use matplotlib.pyplot.imsave()
This is the simplest and most direct method to save an array as a PNG file in Python. I often use this method when I’m working with scientific or image data that I want to visualize quickly.
Here’s the complete Python code example:
import numpy as np
import matplotlib.pyplot as plt
# Create a random NumPy array (for example, a 10x10 grayscale image)
data = np.random.rand(10, 10)
# Save the NumPy array as a PNG image
plt.imsave('random_array.png', data, cmap='viridis')
print("PNG image saved successfully as random_array.png")You can refer to the screenshot below to see the output.

In this code, I used plt.imsave() to save the array as an image. The cmap=’viridis’ argument applies a color map, which helps visualize data intensity beautifully.
If you’re working with grayscale data, you can replace ‘viridis’ with ‘gray’ to save it as a grayscale PNG.
Method 2 – Save a NumPy Array as PNG Using plt.imshow() and plt.savefig()
Sometimes, you may want more control over the figure, such as adding titles, colorbars, or adjusting layout before saving. In such cases, I prefer using plt.imshow() combined with plt.savefig().
Here’s how you can do it:
import numpy as np
import matplotlib.pyplot as plt
# Create a 2D NumPy array
data = np.random.rand(100, 100)
# Display the array using imshow()
plt.imshow(data, cmap='plasma')
plt.colorbar(label='Intensity')
# Save the figure as a PNG image
plt.savefig('array_plot.png', dpi=300, bbox_inches='tight')
print("Image saved successfully as array_plot.png")You can refer to the screenshot below to see the output.

Here, I used dpi=300 for high-resolution output, which is perfect for presentations or reports. The bbox_inches=’tight’ ensures that unnecessary white spaces around the image are removed.
Method 3 – Save a NumPy Array as a Transparent PNG in Python
In some projects (especially web-based or UI overlays), I needed to save images with transparent backgrounds. Matplotlib makes this incredibly easy with the transparent=True argument in savefig().
Here’s the example:
import numpy as np
import matplotlib.pyplot as plt
# Create a simple 2D array
data = np.random.rand(50, 50)
# Plot the array
plt.imshow(data, cmap='inferno')
plt.axis('off') # Hide axes for a clean image
# Save the image with a transparent background
plt.savefig('transparent_array.png', transparent=True, dpi=200, bbox_inches='tight')
print("Transparent PNG saved successfully as transparent_array.png")You can refer to the screenshot below to see the output.

This method is especially useful when you want to overlay your image on websites, dashboards, or PowerPoint slides. The transparent background ensures that the image blends seamlessly with any design.
Method 4 – Convert and Save RGB Arrays as PNG in Python
If you’re working with RGB images, such as those generated by image processing or deep learning models, you can save them directly using Matplotlib. The key is to ensure your array has the correct shape, (height, width, 3) for RGB.
Here’s a quick example:
import numpy as np
import matplotlib.pyplot as plt
# Create a random RGB array (100x100 pixels)
rgb_array = np.random.rand(100, 100, 3)
# Save the RGB array as a PNG image
plt.imsave('rgb_array.png', rgb_array)
print("RGB PNG image saved successfully as rgb_array.png")You can refer to the screenshot below to see the output.

This approach works great when you’re visualizing output from machine learning models like CNNs or GANs. It’s fast, simple, and doesn’t require converting the array to any other format.
Method 5 – Save a NumPy Array as PNG Using PIL and Matplotlib Together
Sometimes, I combine Matplotlib with Pillow (PIL) when I need more control over image formats or metadata. This method gives you the flexibility to handle pixel values, transparency, and even image compression.
Here’s the full code:
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
# Create a 2D NumPy array
array = np.random.rand(100, 100) * 255
array = array.astype(np.uint8)
# Convert NumPy array to an image using PIL
image = Image.fromarray(array)
# Save image using PIL
image.save('array_pil.png')
# Optionally display using Matplotlib
plt.imshow(image, cmap='gray')
plt.title('Array Saved as PNG using PIL + Matplotlib')
plt.show()
print("Image saved successfully as array_pil.png")Here, I first converted the NumPy array into an unsigned 8-bit integer array (0–255).
Then, I used Pillow’s Image.fromarray() to save it as a PNG file, which ensures pixel-perfect accuracy.
Additional Tips for Saving Arrays as PNG in Python
Here are a few useful tips I’ve learned over the years:
- Use DPI wisely: For web images, 100–150 DPI is fine; for print, go for 300+.
- Normalize data: If your array values are not between 0–1 or 0–255, normalize them before saving.
- Save colorbars separately: You can use plt.colorbar() and plt.savefig() independently to save legends.
- Batch save arrays: Use loops to save multiple arrays automatically in your Python scripts.
- Avoid white borders: Always use bbox_inches=’tight’ when saving with plt.savefig().
Here’s a quick snippet for batch saving multiple arrays:
import numpy as np
import matplotlib.pyplot as plt
for i in range(5):
data = np.random.rand(50, 50)
plt.imsave(f'array_{i}.png', data, cmap='viridis')
print(f"Saved array_{i}.png")This is especially handy when you’re working with datasets or simulation outputs that need to be exported as images.
Common Use Cases
Over the years, I’ve used this technique in several real-world Python projects, such as:
- Visualizing heatmaps of U.S. sales data using NumPy arrays.
- Saving satellite image patches from geospatial datasets.
- Exporting AI model outputs (like segmentation masks) as PNGs.
- Creating custom icons or textures for dashboards and games.
Each time, Matplotlib’s simplicity and flexibility made the process smooth and reliable.
Conclusion
So that’s how you can save a NumPy array as a PNG image using Matplotlib in Python.
We explored multiple methods, from plt.imsave() to plt.savefig() and even combining Pillow with Matplotlib for advanced control.
If you’re just getting started, I recommend beginning with the first method using plt.imsave(). It’s quick, clean, and perfect for most use cases.
Over time, as your projects grow, you can experiment with transparency, DPI, and color maps to create professional-quality visuals. Python and Matplotlib together make it incredibly easy to bridge the gap between raw data and stunning visual outputs.
You may also read:
- Add Text to Bar and Scatter Plots in Matplotlib
- Add Text to the Corner and Center of a Plot in Matplotlib
- Save Matplotlib Graph as PNG in Python
- Save a Matplotlib Plot as a Transparent PNG in Python

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.