为了账号安全,请及时绑定邮箱和手机立即绑定

Plotly:在一张图中出现单独的图

Plotly:在一张图中出现单独的图

森林海 2022-10-06 18:54:40
我想分开出现在最终图表上的图。我有医院的数据,这就是为什么我想分开以便我有单独的医院图表。每个医院都有自己独立的图表。这是我的工作代码# importsimport plotly.graph_objects as gofrom plotly.offline import iplotimport pandas as pdimport numpy as np# intialise data of lists. data = {'Name':['Nick hospital', 'Nick hospital','Nick hospital', 'Krish hospital', 'Krish hospital','Krish hospital'],         'NAR_forms_used':[2, 1,2, 2, 2,3]       } # Create DataFrame df = pd.DataFrame(data)# get counts per NAR typedf_nar=pd.DataFrame(df.groupby('Name')['NAR_forms_used'].value_counts())df_nar=df_nar.rename({'NAR_forms_used': 'NAR count'}, axis='columns')df_nar=df_nar.reset_index()# Manage NAR types (who knows, there may be more types with time?)nars = df_nar['NAR_forms_used'].unique()nars = nars.tolist()nars.sort(reverse=False)# set up plotly figurefig = go.Figure()# add one trace per NAR type and show counts per hospitalfor nar in nars:    # subset dataframe by NAR type    df_ply=df_nar[df_nar['NAR_forms_used']==nar]    # add trace    fig.add_trace(go.Bar(x=df_ply['Name'], y=df_ply['NAR count'], name='NAR Type='+str(nar)))# make the figure a bit more presentablefig.update_layout(title='NAR per hospital',                  yaxis=dict(title='<i>count of NAR types</i>'),                  xaxis=dict(title='<i>Hospital</i>',                            )                 )fig.show()如果您注意到最终图表将所有医院都放在一个图表中,但我想将它们分开并将每个医院图表分开,以便我可以使用医院的下拉选择添加到仪表板上。请帮助我在尼克医院中分离这个图表输出以拥有自己的图表,这与Krish 医院相同
查看完整描述

1 回答

?
慕哥9229398

TA贡献1877条经验 获得超6个赞

import plotly.graph_objects as go

import pandas as pd


df = pd.DataFrame({'Name': ['Nick hospital', 'Nick hospital', 'Nick hospital', 'Nick hospital', 'Nick hospital', 'Nick hospital',

                   'Krish hospital', 'Krish hospital', 'Krish hospital','Krish hospital', 'Krish hospital', 'Krish hospital'],

                   'NAR_forms_used': [2, 1, 3, 1, 1, 2, 1, 2, 3, 3, 3, 1]})


df_nar = pd.DataFrame(df.groupby('Name')['NAR_forms_used'].value_counts())

df_nar = df_nar.rename({'NAR_forms_used': 'NAR count'}, axis='columns')

df_nar = df_nar.reset_index()


nars = df_nar['NAR_forms_used'].unique()

nars = nars.tolist()

nars.sort(reverse=False)


# Nick hospital

fig1 = go.Figure()


for nar in nars:


    df_ply = df_nar[(df_nar['NAR_forms_used'] == nar) & (df_nar['Name'] == 'Nick hospital')]


    fig1.add_trace(go.Bar(y=df_ply['NAR count'], x=['NAR Type=' + str(nar)]))


fig1.update_layout(title='Nick hospital', showlegend=False, yaxis=dict(title='<i>count of NAR types</i>'))


fig1.show()


# Krish hospital

fig2 = go.Figure()


for nar in nars:


    df_ply = df_nar[(df_nar['NAR_forms_used'] == nar) & (df_nar['Name'] == 'Krish hospital')]


    fig2.add_trace(go.Bar(y=df_ply['NAR count'], x=['NAR Type=' + str(nar)]))


fig2.update_layout(title='Krish hospital', showlegend=False, yaxis=dict(title='<i>count of NAR types</i>'))


fig2.show()


查看完整回答
反对 回复 2022-10-06
  • 1 回答
  • 0 关注
  • 126 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号