如何在 ipywidget 输出小部件中显示 matplotlib 饼图?我正在使用 Jupyter Notebook,目前没有显示任何内容

2023-12-06

我当前的代码结果,但我希望饼图显示在“算法”选项卡下

My code

out_algo = widgets.Output()
out_ht = widgets.Output()
outioi_trd = widgets.Output()
out_adv = widgets.Output()
out_fundexp = widgets.Output()
out_stratexp = widgets.Output()

tab = widgets.Tab(children = [out_algo,out_ht,outioi_trd,out_adv,out_fundexp,out_stratexp])
tab.set_title(0,'Algo Trades')
tab.set_title(1,'HT Trades')
tab.set_title(2,'IOI Trades')
tab.set_title(3,'Top 20 ADV Trades')
tab.set_title(4,'Fund Exposure')
tab.set_title(5,'Strategy Exposure')
display(tab)

broker_list_test = broker_list_test.set_index('Broker')
#drop columns not needed
broker_list_test = broker_list_test.drop(['Direction', 'Investment_Team', 'portfolio_name', 'full_name','Desk', 'trader', 'trade_date', 'pct_adv_1m', 'trade_qty', 'price'],axis=1)
#group by broker and USD notional sum
broker_list_test2 = broker_list_test.groupby('Broker')
broker_list_test3 = broker_list_test2.sum() #group by value per broker
broker_list_test3['USD_Notional']=broker_list_test3['USD_Notional'].astype('float')
#IF WE HAVE COMMA USE s.replace(',','.')
label = broker_list_test3.index.tolist() #pull out all index names as pie labels 
data = broker_list_test3.values.tolist() # pie data
data = reduce(lambda x,y: x+y,data) #break list of lists into one list 
data_float=[float(x) for x in data] #convert string to float
sum_algo=0
for x in data_float:
   sum_algo+=x        
with out_algo:
    plt.pie(data_float,labels=label,autopct="%0.f%%",radius=2.4)
    plt.title("Algo Usage Notional Breakdown in USD\n"+'{:,.0f}'.format(sum_algo),position=(0.5,1.5))
    plt.show()

在上面的代码片段中,如果我不使用 ipywidgets,我能够很好地生成饼图,但是如果我想将此饼图放入 ipywidget 选项卡中,我看不到任何结果,您认为还有另一种方法吗解决这个问题吗?我正在使用 Jupyter 笔记本


在 plt.show 上

plt.show(fig)在上面的答案中Wayne现已弃用:

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)) 是仅关键字调用。

所以display(fig)似乎是近期唯一有效的选择。


on %matplotlib inline

人们可以看看嵌入笔记本中的 matplotlib 图的其他选项:

  • %matplotlib notebook

    note:通常不需要显示或提供 2 位数字输出

  • %matplotlib 小部件

    note: 需要ipympl, see matplotlib/ipympl在 Github 上

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 ipywidget 输出小部件中显示 matplotlib 饼图?我正在使用 Jupyter Notebook,目前没有显示任何内容 的相关文章

随机推荐