[英]How to display matplotlib plots in a Jupyter tab widget?
I am having trouble displaying plots inside of Jupyter tab widgets.我在 Jupyter 选项卡小部件内显示图时遇到问题。 Consider the following snippet:
考虑以下片段:
import matplotlib.pyplot as plt
import pandas as pd
import ipywidgets as widgets
import numpy as np
out1 = widgets.Output()
out2 = widgets.Output()
data1 = pd.DataFrame(np.random.normal(size = 50))
data2 = pd.DataFrame(np.random.normal(size = 100))
with out1:
fig1, axes1 = plt.subplots()
data1.hist(ax = axes1)
display(fig1)
with out2:
fig2, axes2 = plt.subplots()
data2.hist(ax = axes2)
display(fig2)
tab = widgets.Tab(children = [out1, out2])
tab.set_title(0, 'First')
tab.set_title(1, 'Second')
display(tab)
(I am running Python 3.5.2, Jupyter 4.4.0, ipywidgets 7.2.1 on Ubuntu 16.04 inside a virtual environment.) (我在虚拟环境中的 Ubuntu 16.04 上运行 Python 3.5.2、Jupyter 4.4.0、ipywidgets 7.2.1。)
If I put this code on the first row of the notebook and run it, I see a tab widget with two tabs, each one of which displays a string, but not the plot:如果我将此代码放在笔记本的第一行并运行它,我会看到一个带有两个选项卡的选项卡小部件,每个选项卡都显示一个字符串,但不显示绘图:
If I run it for a second time, or if I rerun it putting everything after the import of matplotlib
in a second cell, I see a tab widget with one plot on each tab, but I get the two plots displayed a second time outside of the tabs.如果我第二次运行它,或者如果我重新运行它,将
matplotlib
导入后的所有内容都放在第二个单元格中,我会看到一个选项卡小部件,每个选项卡上都有一个图,但是我第二次显示这两个图选项卡。
I can get rid of the additional displays by wrapping my code inside calls to plt.ioff
and plt.ion
, but it has been suggested to me that this is a hack.我可以通过将我的代码包装在对
plt.ioff
和plt.ion
调用中来摆脱额外的显示,但有人向我建议这是一个黑客。 And in any case, it does not make matplotlib display the plots in the first cell.无论如何,它不会使 matplotlib 在第一个单元格中显示绘图。
Question : What is the proper way of displaying plots inside tabs?问题:在选项卡内显示图的正确方法是什么?
I added a couple of things to make your code work as you would like我添加了一些东西来让你的代码像你想要的那样工作
%matplotlib inline
at the top of the cell%matplotlib inline
添加%matplotlib inline
display(fig)
calls with plt.show(fig)
calls.plt.show(fig)
调用替换display(fig)
plt.show(fig)
调用。%matplotlib inline
import matplotlib.pyplot as plt
import pandas as pd
import ipywidgets as widgets
import numpy as np
out1 = widgets.Output()
out2 = widgets.Output()
data1 = pd.DataFrame(np.random.normal(size = 50))
data2 = pd.DataFrame(np.random.normal(size = 100))
tab = widgets.Tab(children = [out1, out2])
tab.set_title(0, 'First')
tab.set_title(1, 'Second')
display(tab)
with out1:
fig1, axes1 = plt.subplots()
data1.hist(ax = axes1)
plt.show(fig1)
with out2:
fig2, axes2 = plt.subplots()
data2.hist(ax = axes2)
plt.show(fig2)
plt.show(fig)
in the answer above from ac24 is now deprecated:上面来自ac24的答案中的
plt.show(fig)
现在已弃用:
In [1]: import matplotlib.pyplot as plt
In [2]: fig = plt.figure()
In [3]: plt.show(fig)
<ipython-input-3-d1fd62acb551>:1: MatplotlibDeprecationWarning: Passing the
block parameter of show() positionally is deprecated since Matplotlib 3.1; the
parameter will become keyword-only in 3.3.
plt.show(fig)
plt.show(block=True)
(or plt.show(block=False)
) is the keyword-only call. plt.show(block=True)
(或plt.show(block=False)
)是仅关键字调用。
from IPython.display 导入显示
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.