Reputation: 3571
I have a set of points which can be "distinguished" in two parts, based on their coordinates. To make an example, imagine:
np.random.seed(1234) #I hope this is how you use random seed, not sure
### First part of data ###
xcoord1 = np.random.uniform(10,20,100).reshape(-1,1)
ycoord1 = np.random.uniform(20,30, 100).reshape(-1,1)
part1 = np.hstack((xcoord1, ycoord1))
### second part of data ###
xcoord2 = np.random.uniform(60,90,100).reshape(-1,1)
ycoord2 = np.ranodom.uniform(5,25,100).reshape(-1,1)
part2 = np.vstack((xcoord2, ycoord2))
Important: the two part of points are totally disjoint as you can see here
What I want to do
I have a set of values
values1 = np.random.uniform(0,1, size = 200) #for part1
values2 = np.random.uniform(0,1, size = 200) #for part2
each corresponding to the points in part1
and part2
. I can easily plot them based on the color (here)
colors1 = [cmap(tl) for tl in values1]
colors2 = [cmap(tl) for tl in values2]
colors = ['yellow', 'red', 'green']
cmap = matplotlib.colors.ListedColormap(colors)
plt.scatter(part1[:,0], part1[:,1], c = colors1)
plt.scatter(part2[:,0], part2[:,1], c=colors2)
Now I would like to do some interpolation on the colors, BUT I would like to have the color meshing only in the two areas where I have the points, not in between them! So I want to do two interpolations.
I managed to do the following:
xi = np.linspace(min(min(part1[:,0]), min(part2[:,0])), max(max(part1[:,0]), max(part2[:,0])), 200)
yi = np.linspace(min(min(part1[:,1]), min(part2[:,1])), max(max(part1[:,1]), max(part2[:,1])), 200)
xx, yy = np.meshgrid(xi, yi)
gridPoints = (xx,yy)
totalpart = np.vstack((part1, part2))
totalvalue = np.hstack((values1, values2))
zi = griddata(totalpart, totalvalue, gridPoints, method='linear')
plt.imshow(zi, origin = 'lower', cmap = cmap)
Which gives me
Now this is an interpolation, but as you see the colors occupy the part of the plot in between the two "clusters", that used to be white. I would like to have each one of the two "clusters" (i.e. part1
and part2
) to have their own interpolation (with the same colors if possible).
How can I do that?
WHAT I WANT, DONE IN PAINT
I tried to do in paint what I want to achieve, so you have a better idea:
Upvotes: 0
Views: 77
Reputation: 126
I would create a mask to set the values of zi
outside the two regions to np.nan
. That is
mask1 = (gridPoints[0] >= 10) & (gridPoints[0] <= 20) & \
(gridPoints[1] >= 20) & (gridPoints[1] <= 30)
mask2 = (gridPoints[0] >= 60) & (gridPoints[0] <= 90) & \
(gridPoints[1] >= 5) & (gridPoints[1] <= 25)
mask = mask1 | mask2
zi[~mask] = np.nan
plt.imshow(zi, origin = 'lower', cmap = cmap)
Upvotes: 1