matplotlib - Matplotlib 修改直方图修改后不显示
问题描述
我已经绘制了一个直方图并想修改它,然后重新绘制它。Figure
如果不重新定义和Axes
对象定义,它将不会再次绘制。我正在使用 Jupyter Notebook,而且我是 matplotlib 的新手,所以我不知道这是否是我对 matplotlib 不了解的地方,是否是 Jupyter Notebook 的问题或其他问题。
这是我的第一个代码块:
"""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
就会变得不可用。您的第三个代码块显示了一个绘图,因为您正在重新生成图形和轴。
推荐阅读
- docker - docker COPY 没有复制文件
- android - 如何创建发布闭包?(安卓工作室)
- php - 尽管正在发送参数,但 JSON API 返回错误
- apache - 如果 url 通过 proxypass 指向另一个网络服务器,则重定向到 https
- c# - 我想通过 ref 链接两个变量,这样当我更新一个变量时,其他变量也会更新
- database - 注册时的条款和条件是否应记录在数据库中?
- javascript - 如何将我的路由中的道具从我的组件传递到具有“onClick”的子组件
- javascript - 如何使用 fs 读取和保存 base64 图像
- tensorflow - conda install -c conda-forge tensorflow 刚刚卡在求解环境中
- node.js - Knex 客户端未定义