我有以下问题:我想从 2D 数组中提取 1D 轮廓,这相对简单。而且在任意方向上执行此操作也很容易(请参见此处)。
但我想给轮廓一定的宽度,以便对垂直于轮廓的值进行平均。我设法做到了这一点,但速度非常慢。有人对此有好的解决方案吗?
谢谢!
import numpy as np
import os
import math
import itertools
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon
def closest_point(points, coords):
min_distances = []
coords = coords
for point in points:
distances = []
for coord in coords:
distances.append(np.sqrt((point[0]-coord[0])**2 + (point[1]-coord[1])**2))
val, idx = min((val, idx) for (idx, val) in enumerate(distances))
min_distances.append(coords[idx])
return min_distances
def rect_profile(x0, y0, x1, y1, width):
xd=x1-x0
yd=y1-y0
alpha = (np.angle(xd+1j*yd))
y00 = y0 - np.cos(math.pi - …Run Code Online (Sandbox Code Playgroud) 有人知道如何优雅地在三个子图中的两个上绘制水平颜色条,并在第三个子图上绘制一个额外的水平颜色条.
理想情况下,颜色条应具有与相应图像轴相同的x尺寸.除了使用设置整个图像网格外,我没有找到任何好的解决方案matplotlib.gridspec.
绘制单个颜色条可以很好地工作mpl_toolkits.axes_grid1.ImageGrid,但是当尝试在三个轴中的两个轴上绘制颜色条时它会失败.
我有以下问题:我想整合一个二维数组,所以基本上是反转一个梯度算子。
假设我有一个非常简单的数组,如下所示:
shape = (60, 60)
sampling = 1
k_mesh = np.meshgrid(np.fft.fftfreq(shape[0], sampling), np.fft.fftfreq(shape[1], sampling))
Run Code Online (Sandbox Code Playgroud)
然后我将我的向量场构造为一个复值数组(x 向量 = 实部,y 向量 = 虚部):
k = k_mesh[0] + 1j * k_mesh[1]
Run Code Online (Sandbox Code Playgroud)
现在我取梯度:
k_grad = np.gradient(k, sampling)
Run Code Online (Sandbox Code Playgroud)
然后我使用傅立叶变换来反转它,使用以下函数:
def freq_array(shape, sampling):
f_freq_1d_y = np.fft.fftfreq(shape[0], sampling[0])
f_freq_1d_x = np.fft.fftfreq(shape[1], sampling[1])
f_freq_mesh = np.meshgrid(f_freq_1d_x, f_freq_1d_y)
f_freq = np.hypot(f_freq_mesh[0], f_freq_mesh[1])
return f_freq
def int_2d_fourier(arr, sampling):
freqs = freq_array(arr.shape, sampling)
k_sq = np.where(freqs != 0, freqs**2, 0.0001)
k = np.meshgrid(np.fft.fftfreq(arr.shape[0], sampling), np.fft.fftfreq(arr.shape[1], sampling))
v_int_x = np.real(np.fft.ifft2((np.fft.fft2(arr[1]) …Run Code Online (Sandbox Code Playgroud) 有谁知道是否可以使用 计算两个椭圆的重叠面积matplotlib.patches.Ellipse。
我必须像这样省略:
我想计算重叠面积与各个椭圆的面积之间的比率。Ellipse仅使用from是否可以matplotlib.patches