My code result currently, but I want the Pie chart to show up under the Algo Tab
我的代码
Flexible
在上面的代码片段中,如果我不使用ipywidgets,则可以很好地生成饼图,但是如果我希望将此饼图放在ipywidget选项卡中,则看不到任何结果,您认为有解决这个问题的另一种方法?我正在使用Jupyter Notebook
答案 0 :(得分:0)
基于this working code,建议您将绘图分配给变量my_plot
,然后尝试plt.show(my_plot)
或display(my_plot.figure)
。
我使用笔记本here中的甜甜圈图概述了该方法。您需要运行笔记本才能看到它的渲染。为此,请转到here,然后按“在笔记本计算机上的选项卡小部件演示中以甜甜圈图开始”旁边的launch binder
。在概述的过程中,我已经在笔记本的较早部分调用了绘图。因此,我提到了几种将其添加到上面的选项卡的方法。
从那里总结:
早先在笔记本中将甜甜圈图定义为donut_plot
,以下代码将其添加到简单标签系统的第一个标签中:
%matplotlib inline
# based on https://stackoverflow.com/a/51060721/8508004
# and https://github.com/jupyter-widgets/ipywidgets/issues/1754
# combined with donut plot from
# https://github.com/fomightez/donut_plots_with_subgroups/blob/master/demo_basics_from_df.ipynb
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)
display(donut_plot.figure)
with out2:
fig2, axes2 = plt.subplots()
data2.hist(ax = axes2)
plt.show(fig2)
更新以下带有饼图的更多自包含示例:
为响应OP表达添加饼图的麻烦,我在上面链接的笔记本中添加了另一个示例。归结为以下。为了使代码块比上面更独立,我还包括了使用的数据框:
%matplotlib inline
# based on https://stackoverflow.com/a/51060721/8508004
# and https://github.com/jupyter-widgets/ipywidgets/issues/1754
# combined with donut plot from
# https://github.com/fomightez/donut_plots_with_subgroups/blob/master/demo_basics_from_df.ipynb
import pandas as pd
obs = [('A', 1, "frizzled"),
('A', 1, "lethargic"),
('A', 1, "polythene"),
('A', 1, "epic"),
('A', 2, "frizzled"),
('A', 2, "lethargic"),
('A', 2, "epic"),
('A', 3, "frizzled"),
('A', 3, "lethargic"),
('A', 3, "polythene"),
('A', 3, "epic"),
('A', 3, "bedraggled"),
('B', 1, "frizzled"),
('B', 1, "lethargic"),
('B', 1, "polythene"),
('B', 1, "epic"),
('B', 1, "bedraggled"),
('B', 1, "moombahcored"),
('B', 2, "frizzled"),
('B', 2, "lethargic"),
('B', 2, "polythene"),
('B', 2, "epic"),
('B', 2, "bedraggled"),
('C', 1, "frizzled"),
('C', 1, "lethargic"),
('C', 1, "polythene"),
('C', 1, "epic"),
('C', 1, "bedraggled"),
('C', 1, "moombahcored"),
('C', 1, "zoned"),
('C', 1, "erstaz"),
('C', 1, "mined"),
('C', 1, "liberated"),
('C', 2, "frizzled"),
('C', 2, "lethargic"),
('C', 2, "polythene"),
('C', 2, "epic"),
('C', 2, "bedraggled"),
('C', 3, "frizzled"),
('C', 3, "lethargic"),
('C', 3, "polythene"),
('C', 3, "epic"),
('C', 3, "bedraggled"),
('C', 4, "bedraggled"),
('C', 4, "frizzled"),
('C', 4, "lethargic"),
('C', 4, "polythene"),
('C', 4, "epic"),
('C', 5, "frizzled"),
('C', 5, "lethargic"),
('C', 5, "polythene"),
('C', 5, "epic"),
('C', 5, "bedraggled"),
('C', 5, "moombahcored")]
labels = ['group', 'subgroup', 'sub-subgroup']
df = pd.DataFrame.from_records(obs, columns=labels)
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)
grouped = df.groupby("group")
grouped.size()
group_names= grouped.size().index.tolist()
group_size= grouped.size().tolist()
my_plot = plt.pie(group_size, labels=group_names,autopct="%0.f%%",radius=2.4)
plt.show(my_plot)
with out2:
fig2, axes2 = plt.subplots()
data2.hist(ax = axes2)
plt.show(fig2)
答案 1 :(得分:0)
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)
(或plt.show(block=False)
)是仅关键字调用。
因此,display(fig)
似乎是不久的将来唯一有效的选择。
%matplotlib inline
人们可以看看在笔记本中嵌入的matplotlib图的其他选项:
%matplotlib notebook
注释:通常不需要显示或提供2个图形输出
%matplotlib小部件
注释:要求ipympl
,请参见Github上的Wayne