python画函数等高线_在等高线上绘制点-Matplotlib/Python

我试图用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()函数允许我指定网格。

我在画点的时候能找到这样做的方法。

如何正确设置范围?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值