matplotlib show() 在第二次调用中不显示()

matplotlib show() doesnt show() in second call

我正在尝试在 Jupyter Notebook 中显示绘图。
当我第一次调用 plt.show() 时,一切都很好,但是当我稍后在另一个单元格中调用它时,它没有显示情节。 我正在使用 %matplotlib inline。

单元格 1

fig, ax = plt.subplots()
ax.set_title("Scatter Plot x1, x2")
ax.set_xlabel("x1")
ax.set_ylabel("x2")
ax.scatter(df["x1"], df["x2"], c=df["c"])
plt.show()

单元格2,这里我添加了一个新的点,想要得到新的情节

ax.scatter(center0[0], center0[1], c="red")
plt.show()

如果我删除单元格 2 中的 plt.show() 语句,我会得到

所以情节在那里,但不会显示?

实际上你不需要再次绘制,你只需要调用你的图

单元格 1:

fig, ax = plt.subplots()
ax.set_title("Scatter Plot x1, x2")
ax.set_xlabel("x1")
ax.set_ylabel("x2")
ax.scatter(df["x1"], df["x2"], c=df["c"])
plt.show()

单元格 2:

ax.scatter(center0[0], center0[1], c="red")
fig

我希望它能回答你的问题