【发布时间】: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