我试图用Matplotlib在等高线上绘制一些点。
我有标量场,我想从中绘制轮廓。
然而,myndarray的维数为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()函数允许我指定网格。
我在画点的时候能找到这样做的方法。
如何正确设置范围?