Numpy Array使用Matplotlib中的多边形切片

Tea*_*hey 5 numpy polygon matplotlib astronomy slice

这似乎是一个相当简单的问题,但我是Python的新手,我正在努力解决它.我有一个从两个numpy数组生成的散点图/热图(大约25,000条信息).y轴直接取自阵列,x轴是通过两个阵列上的简单减法运算生成的.

我现在需要做的是切片数据,以便我可以使用属于绘图中某些参数的选择.例如,我需要提取落在平行四边形内的所有点: 在此输入图像描述

我可以使用简单的不等式剪切矩形(参见索引idx_c,idx_h以及idx下面),但我真的需要一种方法来使用更复杂的几何体来选择点.看起来这种切片可以通过指定多边形的顶点来完成.这是我能找到的最接近解决方案,但我无法弄清楚如何实现它:

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

理想情况下,我真的需要类似于下面的索引,即类似的东西colorjh[idx].最终,我将不得不绘制不同的数量(例如,colorjh[idx]vs colorhk[idx]),因此索引需要可以转移到数据集中的所有数组(大量数组).也许这很明显,但我认为有些解决方案可能不那么灵活.换句话说,我将使用此图选择我感兴趣的点,然后我将需要这些索引适用于同一个表中的其他数组.

这是我正在使用的代码:

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(J-H)', 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()
Run Code Online (Sandbox Code Playgroud)

就像我说的,我是Python的新手,所以解释的行为越少,我就越有可能实现它.感谢您提供的任何帮助.

ask*_*han 5

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)
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明