如何使用具有随机间隔而不是等距间隔的 numpy 的 Meshgrid 函数? [英] How does one use numpy's Meshgrid function with a random interval rather than a equally spaced one?

查看:48
本文介绍了如何使用具有随机间隔而不是等距间隔的 numpy 的 Meshgrid 函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从概念上讲,这就是我想做的事情.我想在 2D 平面中选择随机点,然后绘制一个表面图来展示它的样子.类似地,另一种(次优想法)可能是对 Meshgrid 进行分级并选择 N 个随机点及其对应的高度,然后将它们绘制在曲面图中.

Conceptually this is sort of what I wanted to do. I wanted to select random points in a 2D plane and then plot a surface plot of how that would look like. Similarly, an alternative (sub-optimal idea) could be to grade a Meshgrid and select N random points and their corresponding heights and then plot them in a surface plot.

目前,我能够制作曲面图的唯一成功方法是使用以下方法使用等间隔点:

Currently the only successful way that I've been able to make surface plots is with equally intervaled points using the following:

start_val, end_val = -1,1
N = 100
x_range = np.linspace(start_val, end_val, N)
y_range = np.linspace(start_val, end_val, N)
(X,Y) = np.meshgrid(x_range, y_range)
Z = np.sin(2*np.pi*X) + 4*np.power(Y - 0.5, 2) #function height for the sake of an example

然后用标准绘制事物:

fig = plt.figure()
ax = fig.gca(projection='3d')
surf = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm)
plt.title('Original function')

plt.show()

正如我所料,这会产生一个漂亮的函数图.

this results in a pretty plot of the function just as I expected.

但是,我想在已知间隔内选择随机点.为此,我尝试了替代方法:

However, I'd like to select random points in a known interval. For that I tried the alternative:

start_val, end_val = -1,1
N = 100
x_range = np.random.uniform(low=start_val, high=end_val, size=N)
y_range = np.random.uniform(low=start_val, high=end_val, size=N)
(X,Y) = np.meshgrid(x_range, y_range)
Z = np.sin(2*np.pi*X) + 4*np.power(Y - 0.5, 2) #function height for the sake of an example

不幸的是,这导致了废话:

Unfortunately, that results in non-sense:

而不是像原始点那样均匀分布的更好的东西:

instead of something nicer as the original with the evenly spaced points:

是否有人知道如何完成我正在尝试执行的任务(或类似任务),但能够以合理的方式绘制随机点或包含一些随机性?

does someone know how to do the task (or a similar task) I am trying to do but being able to plot random points or include some randomness in a sensible way?

推荐答案

用于 meshgrid 的 x 和 y 范围不应随​​机排列.这是因为要绘制曲面,仅在 XYZ 空间中有一堆点是不够的:我们还需要知道哪些点与哪些点相关联.这是由数组中值的邻接决定的.

The x and y ranges used for meshgrid should not be in random order. This is because to draw a surface, it's not enough just to have a bunch of points in XYZ space: we also need to know which are to be connected with which. This is determined from the adjacency of values in the arrays.

示例:如果 x_range 是 [1, 2, 3] 并且 y_range 是 [5, 9],那么我们知道点 (1,5)、(2,5)、(1,9) 和 (2,9) 形成要包含在表面中的矩形的顶点(当然还有相应的 Z 值).如果 x_range 是 [1, 3, 2],我们将得到一个基于 (1,5)、(3,5)、(1,9) 和 (3,9) 的矩形.

Example: if x_range is [1, 2, 3] and y_range is [5, 9], then we know that the points (1,5), (2,5), (1,9), and (2,9) form the vertices of a rectangle to be included in the surface (with the corresponding Z-values, of course). If the x_range was instead [1, 3, 2], we would instead have a rectangle based on (1,5), (3,5), (1,9), and (3,9).

要得到 z=f(x,y) 的图,我们需要对 x 和 y 数组进行排序,这样我们得到的矩形看起来像这样:

To have a graph of z=f(x,y), we need sorted x and y arrays, so that rectangles we get look like this:

(对于参数图,x,y,z 是参数 u,v 的函数,我们不必对 x 和 y 值进行排序;但 u 和 v 可能会排序,它们的顺序将决定x,y.)

(For parametric graphs, with x,y,z being functions of parameters u,v, we won't necessarily have sorted x and y values; but u and v probably will be, and their order will determine the order of x,y.)

这篇关于如何使用具有随机间隔而不是等距间隔的 numpy 的 Meshgrid 函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆