11

I use matpotlib's imshow() to interpolate and plot some data. Here is a 2D array.

im = ax.imshow(data2D,interpolation='sinc')

I would like to get the 'sinc' interpolated data after imshow. I haven't been able to find any method to do so. Any idea? Thanks, Nicolas

Art
  • 2,836
  • 4
  • 17
  • 34
Begbi
  • 148
  • 1
  • 5

2 Answers2

0

The matplotlib C++ resampling code is interfaced in the matplotlib.image.resample function, which you can import and call on your own.

  • Please give an example of using this method to interpolate an array. Currently it is unclear how the function should be utilized. – Lupilum May 05 '23 at 14:01
0

As an addendum to Yannick Copin's answer which reads

The matplotlib C++ resampling code is interfaced in the matplotlib.image.resample function, which you can import and call on your own.

Here is a visual demo of interpolation using the private function matplotlib.image._resample

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.image import _resample
from matplotlib.transforms import Affine2D

np.random.seed(19680801)

grid = np.random.rand(4, 4)
scale_factor = 10
out_dimensions = (grid.shape[0]*scale_factor, grid.shape[1]*scale_factor)

fig, axs = plt.subplots(nrows=3)

transform = Affine2D().scale(scale_factor, scale_factor)
# Have to get an image to be able to resample
# Resample takes an _ImageBase or subclass, which require an Axes
img = axs[0].imshow(grid, interpolation='spline36', cmap='viridis')
interpolated = _resample(img, grid, out_dimensions, transform=transform)

axs[0].imshow(grid, cmap='viridis')
axs[1].imshow(grid, interpolation='spline36', cmap='viridis')
axs[2].imshow(interpolated, cmap='viridis')

original data, imshow interpolation, interpolation and then plotting

Lupilum
  • 363
  • 2
  • 11