Matplotlib 等高线值似乎不正确

问题描述 投票:0回答:1

我知道等高线图显示共享特定值的所有点的线。但是,当我在

matplotlib
中为 x^2 和 y^2 之和的平方绘制等高线图时,(1, 1) 的点(计算为 1.41)位于标记为 1.5 和 2.0 的等高线之间.

该点不应该位于 1.0 和 1.5 的等高线之间吗?

同样,(0.707, 0.707) 的点(计算为 1)位于 1.5 的等高线上。该点不应该在 1.0 轮廓线上吗?

我在这里错过了什么/做错了什么?

import numpy as np
import matplotlib.pyplot as plt

zval = 1
xval = np.sqrt(zval**2/2)
print(xval)   # 0.707
yval = xval
print(yval)
print(np.sqrt(xval**2 + yval**2))  # 1

xlist = np.linspace(-3.0, 3.0, 3)
ylist = np.linspace(-3.0, 3.0, 4)
X, Y = np.meshgrid(xlist, ylist)
Z = np.sqrt(X**2 + Y**2)

fig = plt.figure(figsize=(7,6))
left, bottom, width, height = 0.1, 0.1, 0.8, 0.8
ax = fig.add_axes([left, bottom, width, height]) 

cp = ax.contour(X, Y, Z)
ax.clabel(cp, inline=True, 
          fontsize=10)
ax.set_title('Contour Plot')
ax.set_xlabel('x (cm)')
ax.set_ylabel('y (cm)')

# Z value is 1 but point is plotted beyond the 1.5 line -- why?
ax.scatter(
    x=xval, 
    y=yval,
    color="orange"
)

ax.scatter(1,1, color="red")
print(np.sqrt(1**2 + 1**2))

plt.grid()
plt.show()

contour-and-dots

python numpy matplotlib contour
1个回答
0
投票

问题是一开始传入的数组不够细粒度,无法使 Z 值传入反射曲线。

如果我们使用

xlist = np.linspace(-3.0, 3.0, 300)
ylist = np.linspace(-3.0, 3.0, 400)

轮廓线变成曲线,点落在合理的线之间。

© www.soinside.com 2019 - 2024. All rights reserved.