我的代码如下,我相信应该产生一个图表,其中有一个 scatter plot
叠加在一个 contourf
地块
但这并没有发生。
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.scatter(examples[:,0], examples[:, 1])
ax.contourf(x, y, Z)
我以为 scatter
下图将叠加在 contourf
情节。
plt.scatter(x = examples[:,0], y = examples[:, 1])
为什么会这样,应该如何修改代码?
换一下就好了 contourf
和 scatter
秩序。
import numpy as np
import matplotlib.pyplot as plt
N = 1000
xl = np.linspace(0, 10, N)
yl = np.linspace(0, 10, N)
x, y = np.meshgrid(xl, yl)
Z = x**2 + y**2
examples = np.random.uniform(low = 0, high = 10, size = (10, 2))
fig, ax = plt.subplots()
ax.contourf(x, y, Z)
ax.scatter(examples[:,0], examples[:, 1], color = 'red')
plt.show()
你写的最后一条情节线与前一条情节线重叠。