【问题标题】:Plot points over contour - Matplotlib / Python在轮廓上绘制点 - Matplotlib / Python
【发布时间】:2014-07-11 22:17:33
【问题描述】:

我正在尝试使用 Matplotlib 在轮廓上绘制一些点。

我有一个标量场,我想从中绘制轮廓。 但是,我的 ndarray 的尺寸为 0 x 20,但我的真实空间从 -4 到 4 不等。

我可以使用这段代码绘制这个轮廓:

x, y = numpy.mgrid[-4:4:20*1j, -4:4:20*1j]

# Draw the scalar field level curves
cs = plt.contour(scalar_field, extent=[-4, 4, -4, 4])
plt.clabel(cs, inline=1, fontsize=10)

问题是因为我必须在这个图上绘制一些点,这些点是使用 ndarray 获得的,即我得到的点随着这个数组维度而变化。

我尝试使用以下代码绘制这些点:

def plot_singularities(x_dim, y_dim, steps, scalar_field, min_points, max_points, file_path):
    """
    :param x_dim : the x dimension of the scalar field
    :param y_dim : the y dimension of the scalar field
    :param steps : the discretization of the scalar field
    :param file_path : the path to save the data
    :param scalar_field : the scalar_field to be plot
    :param min_points : a set (x, y) of min points of the scalar field
    :param max_points : a set (x, y) of max points of the scalar field
    """
    min_points_x = min_points[0]
    min_points_y = min_points[1]
    max_points_x = max_points[0]
    max_points_y = max_points[1]

    plt.figure()

    x, y = numpy.mgrid[-x_dim:x_dim:steps*1j, -y_dim:y_dim:steps*1j]

    # Draw the scalar field level curves
    cs = plt.contour(scalar_field, extent=[-x_dim, x_dim, -y_dim, y_dim])
    plt.clabel(cs, inline=1, fontsize=10)

    # Draw the min points
    plt.plot(min_points_x, min_points_y, 'ro')

    # Draw the max points
    plt.plot(max_points_x, max_points_y, 'bo')

    plt.savefig(file_path + '.png', dpi=100)
    plt.close()

但我得到了这张图片:

这是不正确的。

如果我改变这一行:

cs = plt.contour(scalar_field, extent=[-x_dim, x_dim, -y_dim, y_dim])

对于那个:

cs = plt.contour(scalar_field)

我得到了想要的行为,但范围没有显示我的真实数据空间,而是 ndarray 维度。

最后,如果我不绘制这些点(注释 plot() 线),我可以画出我想要的范围:

但我必须绘制点。 两个数据都在同一个空间中。 但是 contour() 函数允许我指定网格。 我可以在绘制点时找到一种方法。

如何正确设置范围?

【问题讨论】:

  • 了解您的期望会有所帮助。 “数据空间”应该是什么?点和轮廓都是不正确的,还是只是轮廓?
  • 如果你看第二张图片,它是正确的(图)。但是,将范围显示为 ndarray 维度(从 0 到 20)。我想显示它与我的数据空间不同(中心在 0,0 并且从 -4 到 4 不等)。我将我的“数据空间”称为图像空间(ndarray)和世界空间(我的真实数据空间)的 CG 概念。

标签: python matplotlib plot


【解决方案1】:

如果您不提供与标量字段对应的xy 数据,contour 将使用不超过数组大小的整数值。这就是轴显示数组维度的原因。参数extent 应该给出最小和最大xy 值;我认为这就是您所说的“数据空间”。所以对contour 的调用将是:

contour(scalar_field,extent=[-4,4,-4,4])

这可以通过指定xy 数据来重现:

contour(numpy.linspace(-4,4,20),numpy.linspace(-4,4,20),scalar_field)

然后轮廓看起来与您的第一个情节完全相同。我认为这是不正确的原因,因为最小和最大点不在正确的位置。根据您提供的信息,这是因为您传递给函数的min_pointsmax_points 是数组scalar_field索引,因此它们对应于整数,而不是实际的@987654335 @ 和 y 值。尝试使用这些索引来访问 xy 点,方法是定义:

x=numpy.linspace(-4,4,20)
y=numpy.linspace(-4,4,20)

例如,如果您有一个最小点 (0,1),它将对应于 (x[0], y[1])。我认为mgrid 可以做类似的事情,但我自己从未使用过。

【讨论】:

  • 谢谢!正如我刚刚为@Ajean 写的那样,我知道这些是索引,这就是问题所在(抱歉,如果我不能以一种好的方式表达自己)。我一直在寻找一种在 Matplolib/Python 中进行这种转换的聪明方法。非常感谢!
【解决方案2】:

您想在真实数据空间中同时绘制轮廓和点,是吗? plt.contour 将获取与您拥有的二维数组关联的 x 和 y 值,并将正确绘制在轴上。

xvals = -x_dim:x_dim:step  # I'm not sure about these ... but you get the idea
yvals = -y_dim:y_dim_step
plt.contour(xvals, yvals, scalar_field)

【讨论】:

  • 如果我这样做,我会得到第一张图像,它不尊重我在绘制点时想要的空间。
  • 你确定你要绘制的点在正确的空间中吗?看起来您正在用一件事绘制实际值,而用另一件事绘制索引。
  • 他们都在同一个空间。我在绘制轮廓时将范围更改为参数,但在绘制点时我不知道如何执行此操作。
  • 我认为这里存在一个根本性的误解......当您绘制点时,它们是 (x,y) 值,无论如何,绘图都会将它们放在它们所在的位置。要显示的 2D 数组(通过轮廓)没有与之关联的固有 (x,y) 位置,这就是您需要指定它们的原因(通过范围或上面的 xvals 和 yvals)。如果 plot 绘制的 x-y 点确实在 x_dim/y_dim 空间中(-4 到 4),这将起作用。看起来它们是索引
  • 请注意,4 个最大点位于 0-0,0-19,19-0,19-19,您的数组长度为 20。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-20
  • 2021-04-12
  • 2021-11-03
  • 2021-10-26
  • 1970-01-01
相关资源
最近更新 更多