Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

2D color plot with irregularly spaced samples (matplotlib.mlab.griddata)

cI previously posted this over at code review, but moved it over here as I was told it is more fitting.

Basically, I want to create a colorplot of some irregularly sampled data. I've had some success with the interpolation using matplotlib.mlab.griddata. When I plot the interpolated data (using matplotlib.pyplot.imshow) however, the edges of the domain appear to be left blank. This gets better if I increase the grid density (increase N in the code) but doesn't solve the problem.

I've attached my code and would like to upload an image of the plot I can generate, but am still lacking the reputation to post an image ;)

edit: That has changed now, uploaded the plot after the changes proposed by Ajean:enter image description here

. Can someone help me out as to what is going wrong?

import numpy as np
from matplotlib import pyplot as plt
from matplotlib.mlab import griddata

# Generate Data
X=np.random.random(100)
Y=2*np.random.random(100)-1
Z=X*Y

# Interpolation
N=100j       
extent=(0,1,-1,1)
xs,ys = np.mgrid[extent[0]:extent[1]:N, extent[2]:extent[3]:N]
resampled=griddata(X,Y,Z,xs,ys,interp='nn')

#Plot

fig = plt.figure()
ax = fig.add_subplot(111)

ax.set_xlabel('X')
ax.set_ylabel('Y')

cplot=ax.imshow(resampled.T,extent=extent) 
ticks=np.linspace(-1,1,11)
cbar=fig.colorbar(magplot,ticks=ticks,orientation='vertical')
cbar.set_label('Value', labelpad=20,rotation=270,size=16)

ax.scatter(X,Y,c='r')
like image 437
Raphael Kleindienst Avatar asked May 08 '26 09:05

Raphael Kleindienst


1 Answers

It is because your calls to random don't provide you with any values at the boundary corners, therefore there is nothing to interpolate with. If you change X and Y definitions to

# Just include the four corners
X=np.concatenate([np.random.random(100),[0,0,1,1]])
Y=np.concatenate([2*np.random.random(100)-1,[-1,1,1,-1]])

You'll fill in the whole thing.

like image 93
Ajean Avatar answered May 11 '26 02:05

Ajean



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!