gpt4 book ai didi

python-3.x - 如何在 ipywidget out 小部件中显示 matplotlib 饼图?我正在使用 Jupyter Notebook,目前没有任何显示

转载 作者:行者123 更新时间:2023-12-04 15:34:40 25 4
gpt4 key购买 nike

My code result currently, but I want the Pie chart to show up under the Algo Tab

我的代码

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)(或 plt.show(block=False))是仅关键字调用。

因此 display(fig) 似乎是不久的将来唯一有效的选择。


关于 %matplotlib inline

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

  • %matplotlib 笔记本

    注意:通常不需要显示或给出 2 个图形输出

  • %matplotlib 小部件

    注意:需要ipympl,参见matplotlib/ipympl在 Github 上

关于python-3.x - 如何在 ipywidget out 小部件中显示 matplotlib 饼图?我正在使用 Jupyter Notebook,目前没有任何显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60150375/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com