Matplotlib修改直方图赢得';修改后不显示

Matplotlib修改直方图赢得';修改后不显示,matplotlib,jupyter-notebook,histogram,Matplotlib,Jupyter Notebook,Histogram,我已经绘制了一个直方图,希望修改它,然后重新绘制它。如果不重新定义图形和轴对象定义,它将不会再次打印。我使用的是Jupyter笔记本电脑,而且我对matplotlib还不熟悉,所以我不知道这是否是我对matplotlib不了解的地方,是否是Jupyter笔记本电脑或其他方面的问题 这是我的第一段代码: """Here's some data.""" some_data = np.random.randn(150) """Here I define my `Figure` and `Axes` o

我已经绘制了一个直方图,希望修改它,然后重新绘制它。如果不重新定义
图形
对象定义,它将不会再次打印。我使用的是Jupyter笔记本电脑,而且我对matplotlib还不熟悉,所以我不知道这是否是我对matplotlib不了解的地方,是否是Jupyter笔记本电脑或其他方面的问题

这是我的第一段代码:

"""Here's some data."""
some_data = np.random.randn(150)
"""Here I define my `Figure` and `Axes` objects."""
fig, ax = plt.subplots()
"""Then I make a histogram from them, and it shows up just fine."""
ax.hist(some_data, range=(0, 5))
plt.show()
"""Here I modify the parameter `bins`."""
ax.hist(some_data, bins=20, range=(0, 5))
"""When I try to make a new histogram, it doesn't work."""
plt.show()
"""But it does work if I define new `Figure` and `Axes` objects. 
Why is this? 
How can I display new, modified plots without defining new `Figure` and/or `Axes` objects? """
new_fig, new_ax = plt.subplots()
new_ax.hist(some_data, bins=20, range=(0, 5))
plt.show()
下面是我的第一段代码的输出:

"""Here's some data."""
some_data = np.random.randn(150)
"""Here I define my `Figure` and `Axes` objects."""
fig, ax = plt.subplots()
"""Then I make a histogram from them, and it shows up just fine."""
ax.hist(some_data, range=(0, 5))
plt.show()
"""Here I modify the parameter `bins`."""
ax.hist(some_data, bins=20, range=(0, 5))
"""When I try to make a new histogram, it doesn't work."""
plt.show()
"""But it does work if I define new `Figure` and `Axes` objects. 
Why is this? 
How can I display new, modified plots without defining new `Figure` and/or `Axes` objects? """
new_fig, new_ax = plt.subplots()
new_ax.hist(some_data, bins=20, range=(0, 5))
plt.show()

这是我的第二段代码:

"""Here's some data."""
some_data = np.random.randn(150)
"""Here I define my `Figure` and `Axes` objects."""
fig, ax = plt.subplots()
"""Then I make a histogram from them, and it shows up just fine."""
ax.hist(some_data, range=(0, 5))
plt.show()
"""Here I modify the parameter `bins`."""
ax.hist(some_data, bins=20, range=(0, 5))
"""When I try to make a new histogram, it doesn't work."""
plt.show()
"""But it does work if I define new `Figure` and `Axes` objects. 
Why is this? 
How can I display new, modified plots without defining new `Figure` and/or `Axes` objects? """
new_fig, new_ax = plt.subplots()
new_ax.hist(some_data, bins=20, range=(0, 5))
plt.show()
我的第二个代码块生成无可见输出,这就是问题所在

下面是我的第三段也是最后一段代码:

"""Here's some data."""
some_data = np.random.randn(150)
"""Here I define my `Figure` and `Axes` objects."""
fig, ax = plt.subplots()
"""Then I make a histogram from them, and it shows up just fine."""
ax.hist(some_data, range=(0, 5))
plt.show()
"""Here I modify the parameter `bins`."""
ax.hist(some_data, bins=20, range=(0, 5))
"""When I try to make a new histogram, it doesn't work."""
plt.show()
"""But it does work if I define new `Figure` and `Axes` objects. 
Why is this? 
How can I display new, modified plots without defining new `Figure` and/or `Axes` objects? """
new_fig, new_ax = plt.subplots()
new_ax.hist(some_data, bins=20, range=(0, 5))
plt.show()
下面是我的第三段也是最后一段代码的输出:

"""Here's some data."""
some_data = np.random.randn(150)
"""Here I define my `Figure` and `Axes` objects."""
fig, ax = plt.subplots()
"""Then I make a histogram from them, and it shows up just fine."""
ax.hist(some_data, range=(0, 5))
plt.show()
"""Here I modify the parameter `bins`."""
ax.hist(some_data, bins=20, range=(0, 5))
"""When I try to make a new histogram, it doesn't work."""
plt.show()
"""But it does work if I define new `Figure` and `Axes` objects. 
Why is this? 
How can I display new, modified plots without defining new `Figure` and/or `Axes` objects? """
new_fig, new_ax = plt.subplots()
new_ax.hist(some_data, bins=20, range=(0, 5))
plt.show()


提前感谢。

生成图形或轴时,在用于渲染或显示之前,图形或轴始终可用于渲染或显示。在第一个块中执行
plt.show()
后,
ax
将不可用。您的第三个代码块正在显示绘图,因为您正在重新生成图形和轴。

生成图形或轴时,在将其用于渲染或显示之前,图形或轴仍可用于渲染或显示。在第一个块中执行
plt.show()
后,
ax
将不可用。您的第三块代码正在显示一个绘图,因为您正在重新生成图形和轴。

您正在使用内联后端;因此,您可以在第二个代码块中将
plt.show()
替换为
fig
。谢谢@ImportanceOfBeingEarnest。我按照您的建议做了,并使用了
fig
而不是
plt.show()
(您可以看到我在下面的代码块中注释掉了
plt.show()
)。问题是,新修改的绘图位于原始绘图的顶部。有没有办法用修改后的图替换原来的图?下面是代码:
ax.hist(一些数据,bin=20,range=(0,5))
这里是输出,如果您不创建新图形,它将显示在旧图形中,是的。您正在使用内联后端;因此,您可以在第二个代码块中将
plt.show()
替换为
fig
。谢谢@ImportanceOfBeingEarnest。我按照您的建议做了,并使用了
fig
而不是
plt.show()
(您可以看到我在下面的代码块中注释掉了
plt.show()
)。问题是,新修改的绘图位于原始绘图的顶部。有没有办法用修改后的图替换原来的图?下面是代码:
ax.hist(一些数据,bin=20,range=(0,5))
plt.show()
这里是输出,如果您不创建新图形,它将显示在旧图形中,是的。谢谢@Yaakov-Bressler。那么,有没有任何方法可以在不重新定义的情况下呈现更新版本的绘图,就像您可以在pandas中查看数据帧的更新版本一样?您可以尝试在初始化的
fig
上进行绘图,该图不会在
plt.show()
命令中消失。否则,我建议重新策划和重新展示,除非这违背了项目的目标,在这种情况下,你应该考虑次要情节。谢谢,非常感谢!谢谢@Yaakov-Bressler。那么,有没有任何方法可以在不重新定义的情况下呈现更新版本的绘图,就像您可以在pandas中查看数据帧的更新版本一样?您可以尝试在初始化的
fig
上进行绘图,该图不会在
plt.show()
命令中消失。否则,我建议重新策划和重新展示,除非这违背了项目的目标,在这种情况下,你应该考虑次要情节。谢谢,非常感谢!