I have a 9x9 matrix in which I know 8 values, at the positions contained in array points, with corresponding values in array values. I want to interpolate the unknown values and paste the result on a 200x200 image.
I'm a beginner at python and I'm struggling with the following:
My subplots won't display together. Subplot(121) and subplot(122) should create a single image with both plots next to each other (horizontally), but instead I always get two separate images. What am I doing wrong, and why?
The code below is my attempt at using griddata() to interpolate the known values on a 200x200 grid. The result, grid_z, is however completely filled with nan values, and I don't understand why or how I can solve this.
import numpy as np
from scipy.interpolate import griddata
import matplotlib.pyplot as plt
X, Y = np.mgrid[0:1:200j, 0:1:200j]
points = np.array([(3, 8),(5, 8),(4, 6),(4, 4),(2, 4),(6, 4),(3, 0),(5, 0)])
values = [ 1.82907198, 1.69794981, 1.30089053, -0.00452952, 2.32777365, 0.69508469, 2.06540834, 2.1184028 ]
grid_z = griddata(points, values, (X, Y), method='cubic')
plt.figure
plt.subplot(121)
plt.plot(points[:,0], points[:,1], 'ro', markersize=10)
plt.xlim(0, 8)
plt.ylim(0, 8)
plt.gca().set_aspect('equal', adjustable='box')
plt.title('Original')
plt.subplots(122)
plt.imshow(grid_z.T, extent=(0,8,0,8), origin='lower')
plt.title('Cubic')
plt.gcf().set_size_inches(6, 6)
plt.show()
Any help would be greatly appreciated! I've consulted dozens of similar posts online but am unable to figure out what I'm doing wrong.
CodePudding user response:
To work with subplots you need to use Matplotlib's object-oriented approach (more info