Python 在 Jupyter Notebook 中内联 %matplotlib 之后使用 %matplotlib notebook 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43545050/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using %matplotlib notebook after %matplotlib inline in Jupyter Notebook doesn't work
提问by PeterB
I am using Jupyter Notebook for plotting piechart figures.
我正在使用 Jupyter Notebook 绘制饼图。
In first cellwith my code I have a magic command %matplotlib inline
and after this magic command I run my code, everything works fine and my figure renders.
在我的代码的第一个单元格中,我有一个魔法命令%matplotlib inline
,在这个魔法命令之后我运行我的代码,一切正常,我的图形呈现。
But in second cellwhen I set %matplotlib notebook
for interactive plotting my figure won't render after running this second cell.
但是在第二个单元格中,当我设置%matplotlib notebook
为交互式绘图时,我的图形在运行第二个单元格后不会呈现。
I need to restart kernel and run cell with %matplotlib notebook
again and cannot run %matplotlib inline
command before that.
我需要重新启动内核并%matplotlib notebook
再次运行单元格,并且%matplotlib inline
在此之前无法运行命令。
Here is my code for first cellwith %matplotlib inline
, which renders fine:
这里是我的代码,第一个单元格用%matplotlib inline
,这使得罚款:
import matplotlib.pyplot as plt
%matplotlib inline
labels = "No", "Yes"
sizes = [100, 50]
fig, ax = plt.subplots(figsize=(6, 6))
_, texts, autotexts = ax.pie(sizes, explode=explode, labels=labels, colors=colors, autopct='%1.1f%%',
shadow=False, startangle=90)
ax.axis('equal')
After that I have second cellwith same code, just %matplotlib inline
is changed to %matplotlib notebook
. Figure won't render after I run this cell and I need to restart kernel and run this cell again.
之后我有第二个具有相同代码的单元格,只是%matplotlib inline
更改为%matplotlib notebook
. 运行此单元后图不会呈现,我需要重新启动内核并再次运行此单元。
Why?
为什么?
回答by ImportanceOfBeingErnest
You just have the wrong order of your commands. A backend should be set before importing pyplot in jupyter. Or in other words, after changing the backend, pyplot needs to be imported again.
你只是有错误的命令顺序。在 jupyter 中导入 pyplot 之前应该设置一个后端。或者换句话说,更改后端后,需要再次导入pyplot。
Therefore call %matplotlib ...
prior to importing pyplot.
因此%matplotlib ...
在导入 pyplot 之前调用。
In first cell:
在第一个单元格中:
%matplotlib inline
import matplotlib.pyplot as plt
plt.plot([1,1.6,3])
In second cell:
在第二个单元格中:
%matplotlib notebook
#calling it a second time may prevent some graphics errors
%matplotlib notebook
import matplotlib.pyplot as plt
plt.plot([1,1.6,3])
回答by lolopop
Edit: turns out that you can in fact change backends dynamically on jupyter. Still leaving the answer here because I think it's relevant and explains some matplotlib magic that can pop out sometimes.
编辑:事实证明,您实际上可以在 jupyter 上动态更改后端。仍然在这里留下答案,因为我认为它是相关的,并解释了一些有时会出现的 matplotlib 魔法。
The magic command, as seen in the source code, is calling matplotlib.pyplot.switch_backend(newbackend)
to change the backend. As stated in matplotlib's docs:
如源代码中所示,魔术命令正在调用matplotlib.pyplot.switch_backend(newbackend)
以更改后端。正如 matplotlib 的文档中所述:
matplotlib.pyplot.switch_backend(newbackend)
Switch the default backend. This feature is experimental, and is only expected to work switching to an image backend. e.g., if you have a bunch of PostScript scripts that you want to run from an interactive ipython session, you may want to switch to the PS backend before running them to avoid having a bunch of GUI windows popup. If you try to interactively switch from one GUI backend to another, you will explode..
matplotlib.pyplot.switch_backend(newbackend)
切换默认后端。此功能是实验性的,预计只能切换到图像后端。例如,如果您想从交互式 ipython 会话中运行一堆 PostScript 脚本,您可能希望在运行它们之前切换到 PS 后端,以避免弹出一堆 GUI 窗口。如果您尝试以交互方式从一个 GUI 后端切换到另一个,您将爆炸..
So you really have to restart the kernel each time you switch backends, because matplotlib has a problem to switch the backend after being used.
所以真的每次切换后端都要重启内核,因为matplotlib用完后切换后端有问题。
This problem is mainly due to incompatibilities between different main-loops of the GUI backend. Because normally each backend is also taking care of threads and user input you can't run Qt and Tkinter side-by-side. So that limitation is carried over to jupyter.
这个问题主要是由于 GUI 后端的不同主循环之间的不兼容。因为通常每个后端还要处理线程和用户输入,所以您不能同时运行 Qt 和 Tkinter。所以这个限制会延续到 jupyter。
Also see this question: How to switch backends in matplotlib / Python
另请参阅此问题:如何在 matplotlib / Python 中切换后端
回答by Michelle Abaya
In Jupyter notebook, you have to enter matplotlib notebook in the same line as the one you want to run. Even if you enter "inline" then followed by "notebook", it still won't work. It has to be on the same line as the code you want to render.
在 Jupyter notebook 中,您必须在要运行的同一行中输入 matplotlib notebook。即使您输入“inline”,然后输入“notebook”,它仍然不起作用。它必须与您要呈现的代码在同一行。