【问题标题】:Scatter plot does not appear on the foreground on top of contourf plot散点图没有出现在轮廓图顶部的前景上
【发布时间】:2020-10-07 14:35:01
【问题描述】:

我的代码如下,我相信应该生成一个图表,其中scatter 图叠加在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?
  • 如果您确实需要在contourf之前调用scatter,请使用ax.scatter(..., zorder=2)

标签: python matplotlib scatter-plot subplot contourf


【解决方案1】:

只需交换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()

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

【讨论】:

    猜你喜欢
    • 2023-03-22
    • 2021-07-03
    • 2013-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-25
    • 2022-08-11
    • 1970-01-01
    相关资源
    最近更新 更多