如何删除圆内的一组网格网格点? [英] How to delete a set of meshgrid points inside a circle?
问题描述
我正在尝试创建一个网格网格,而网格中的某些点没有指定的坐标和半径.我无法减去落入圆内的网格点. 这是我的代码
I am trying to create a meshgrid without some of the points that falls within the circle having specified coordinates and a radius. I am not able to subtract the grid points falling inside the circle. Here is my code
import math
import numpy
import matplotlib.pyplot as plt
N = 50
x_start, x_end = -2.0, 2.0
y_start, y_end = -1.0, 1.0
x = numpy.linspace(x_start, x_end, N)
y = numpy.linspace(y_start, y_end, N)
circle_x, circle_y, r= 0.0, 0.0, 0.4
#x.remove((r-circle_x)**2) #need some help with these two lines
#y.remove((r-circle_y)**2)
X, Y = numpy.meshgrid(x, y)
size = 10
fig = plt.figure()
plt.xlabel('x', fontsize = 16)
plt.ylabel('y', fontsize = 16)
plt.scatter(X, Y)
plt.show()
显示网格点的网格##标题
meshgrid showing the mesh points## Heading
推荐答案
如果您希望散点图仅包含圆外的点,请使用布尔索引来从2D网格划分"数组中仅选择那些点:
If you'd like a scatter plot with just the points outside the circle, use boolean indexing to select only those points from your 2D "meshgridded" array:
import numpy as np
import matplotlib.pyplot as plt
N = 50
x_start, x_end = -2.0, 2.0
y_start, y_end = -1.0, 1.0
x = np.linspace(x_start, x_end, N)
y = np.linspace(y_start, y_end, N)
x0, y0, radius = 0.0, 0.0, 0.4
x, y = np.meshgrid(x, y)
r = np.sqrt((x - x0)**2 + (y - y0)**2)
outside = r > radius
fig, ax = plt.subplots()
ax.set(xlabel='X', ylabel='Y', aspect=1.0)
ax.scatter(x[outside], y[outside])
plt.show()
另一方面,如果使用的是类似imshow
的东西,需要2D输入,则需要掩盖内部的值(如@JulienSpronck所述,尽管最好将它们设置为np.nan
或使用遮罩数组而不是将其设置为0)或在图像上设置剪切路径.
On the other hand, if you were using something like imshow
that requires 2D input, you'd need to either mask the values inside (as @JulienSpronck mentions, though it would be better to set them to np.nan
or use a masked array than to set them to 0) or set a clip path on the image.
但是,为了分散,您不需要2D输入.
For scatter, however, you don't need 2D input.
在nD数组上的布尔索引将返回1d结果.例如:
Boolean indexing on a nD array will return a 1d result. For example:
In [9]: x = np.arange(100).reshape(10, 10)
In [10]: x
Out[10]:
array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19],
[20, 21, 22, 23, 24, 25, 26, 27, 28, 29],
[30, 31, 32, 33, 34, 35, 36, 37, 38, 39],
[40, 41, 42, 43, 44, 45, 46, 47, 48, 49],
[50, 51, 52, 53, 54, 55, 56, 57, 58, 59],
[60, 61, 62, 63, 64, 65, 66, 67, 68, 69],
[70, 71, 72, 73, 74, 75, 76, 77, 78, 79],
[80, 81, 82, 83, 84, 85, 86, 87, 88, 89],
[90, 91, 92, 93, 94, 95, 96, 97, 98, 99]])
In [11]: x[x > 75]
Out[11]:
array([76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92,
93, 94, 95, 96, 97, 98, 99])
因为scatter
只是绘制点,所以不关心它们的连接方式,因此我们可以轻松地使用布尔索引的一维结果.
Because scatter
is only plotting points, it doesn't care how they're connected, and we can easily use the 1D result of boolean indexing.
另一方面,如果要绘制图像,则需要2D网格.在这种情况下,您需要屏蔽这些值:
On the other hand, if you wanted to plot an image, you'd need a 2D grid. In that case, you'd want to mask the values instead:
In [12]: np.ma.masked_where(x <= 75, x)
Out[12]:
masked_array(data =
[[-- -- -- -- -- -- -- -- -- --]
[-- -- -- -- -- -- -- -- -- --]
[-- -- -- -- -- -- -- -- -- --]
[-- -- -- -- -- -- -- -- -- --]
[-- -- -- -- -- -- -- -- -- --]
[-- -- -- -- -- -- -- -- -- --]
[-- -- -- -- -- -- -- -- -- --]
[-- -- -- -- -- -- 76 77 78 79]
[80 81 82 83 84 85 86 87 88 89]
[90 91 92 93 94 95 96 97 98 99]])
请注意,这如何维护输入的2D结构.
Notice how this maintains the 2D structure of the input.
另一方面,如果这是一个浮点数组,则可以很容易地将值设置为np.nan
而不是对其进行遮罩.用imshow
绘制时,两者的行为相同.在这种情况下,我使用了带掩码的数组,因为x
是整数数组,并且不能包含NaN.
On a side note, if this were a floating point array, you could just as easily set the values to np.nan
instead of masking them. When plotted with imshow
, the two will behave identically. I used a masked array in this case because x
was an integer array and can't contain NaN's.
这篇关于如何删除圆内的一组网格网格点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!