散点图不出现在等高线图的前景上。

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

我的代码如下,我相信应该产生一个图表,其中有一个 scatter plot 叠加在一个 contourf 地块

但这并没有发生。

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.scatter(examples[:,0], examples[:, 1])
ax.contourf(x, y, Z)

enter image description here

我以为 scatter 下图将叠加在 contourf 情节。

plt.scatter(x = examples[:,0], y = examples[:, 1])

enter image description here

为什么会这样,应该如何修改代码?

python matplotlib scatter-plot subplot contourf
1个回答
0
投票

换一下就好了 contourfscatter 秩序。

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()

enter image description here

你写的最后一条情节线与前一条情节线重叠。

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