Slicing an array using a polygon in Matplotlib

This seems like a pretty simple problem, but I'm new to Python, and I'm struggling to solve it. I have a scatterplot / heatmap created from two numpy arrays (about 25,000 pieces of information). The y axis is taken directly from the array, and the x axis is generated from a simple subtraction operation on two arrays.

Now I need to take a snapshot of the data so that I can work with a choice that falls into certain parameters on the graph. For example, I need to extract all the points that fall into a parallelogram: enter image description here

I can cut a rectangle using simple inequalities (see indexing idx_c , idx_h and idx , below), but I really need a way to select points using more complex geometry. It seems that this slicing can be done by specifying the vertices of the polygon. This is approximately what I can find for the solution, but I cannot figure out how to implement it:

http://matplotlib.org/api/nxutils_api.html#matplotlib.nxutils.points_inside_poly

Ideally, I really need something similar to the indexing below, i.e. something like colorjh[idx] . In the end, I will have to build different values ​​(for example, colorjh[idx] vs colorhk[idx] ), so the index must be passed to all arrays in the data set (many arrays). Perhaps this is obvious, but I would suggest that there are solutions that may not be as flexible. In other words, I will use this graph to select points of interest to me, and then I will need these indexes to work with other arrays from the same table.

Here is the code I'm working with:

 import numpy as np from numpy import ndarray import matplotlib.pyplot as plt import matplotlib import atpy from pylab import * twomass = atpy.Table() twomass.read('/IRSA_downloads/2MASS_GCbox1.tbl') hmag = list([twomass['h_m']]) jmag = list([twomass['j_m']]) kmag = list([twomass['k_m']]) hmag = np.array(hmag) jmag = np.array(jmag) kmag = np.array(kmag) colorjh = np.array(jmag - hmag) colorhk = np.array(hmag - kmag) idx_c = (colorjh > -1.01) & (colorjh < 6) #manipulate x-axis slicing here here idx_h = (hmag > 0) & (hmag < 17.01) #manipulate y-axis slicing here idx = idx_c & idx_h # heatmap below heatmap, xedges, yedges = np.histogram2d(hmag[idx], colorjh[idx], bins=200) extent = [yedges[0], yedges[-1], xedges[-1], xedges[0]] plt.clf() plt.imshow(heatmap, extent=extent, aspect=0.65) plt.xlabel('Color(JH)', fontsize=15) #adjust axis labels here plt.ylabel('Magnitude (H)', fontsize=15) plt.gca().invert_yaxis() #I put this in to recover familiar axis orientation plt.legend(loc=2) plt.title('CMD for Galactic Center (2MASS)', fontsize=20) plt.grid(True) colorbar() plt.show() 

As I said, I'm new to Python, so the fewer the explanations, the more likely I will be able to implement it. Thanks for any help you can provide.

+4
source share
1 answer
 a = np.random.randint(0,10,(100,100)) x = np.linspace(-1,5.5,100) # tried to mimic your data boundaries y = np.linspace(8,16,100) xx, yy = np.meshgrid(x,y) m = np.all([yy > xx**2, yy < 10* xx, xx < 4, yy > 9], axis = 0) 

enter image description here

+2
source

All Articles