python - 如何在 Jupyter 笔记本中显示之前生成的绘图?

标签 python matplotlib jupyter-notebook

我正在运行 Jupyter 笔记本来绘制一些图:

import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

x = np.arange(0, 7, 0.01)

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x, np.sin(x))

当我执行此操作时,绘图将被渲染并显示为输出。但是,我想稍后在笔记本中查看它,但 plt.show() 不执行任何操作,并且Fig.show() 警告我 matplotlib 正在使用非 GUI 后端。但是,我尝试使用 this answer 中列出的所有后端运气不好的话,如果我使用非 GUI 后端,我对如何渲染绘图感到困惑。然而,fig.savefig(filename) 工作正常。

最佳答案

使用 %matplotlib inline 后端,jupyter 中的图形显示为 png 图像。因此,您没有可用的交互式后端,该后端允许使用 fig.show()
另一方面,内联后端将确保在单元格内引用图形后自动显示该图形。因此,只需在单元格内声明图形句柄 fig 即可将图形附加到单元格输出中。

enter image description here

关于python - 如何在 Jupyter 笔记本中显示之前生成的绘图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47438572/

相关文章:

Python 2.x - 如何通过trainSet和testSet获取NLTK朴素贝叶斯分类的结果

python - IPython : How to show the same plot in different cells?

python - matplotlib中的移动轴指数

python - 无法标记多行 sns.catplot()

python - 我在哪里可以找到有关 seaborn 中的联合绘图函数或 Python 中的 matplotlib 中此参数 'joint_kws' 的详细定义?

Python:如何构建用于加载数据的文本文件?

Python:为什么 [0,1,2][-1:1] 不引发错误?

python - 在Python中获取最接近1的字典键的最快方法

python - pandas.DataFrame 中的 NaN 不打印到 Excel

python - 这是 python pandas DataFrame 后排序绘图错误吗?