首页 > 解决方案 > Plotly:如何在图表上表示编码数据?

问题描述

好人,我有这些数据,我可以在图上表示什么

import pandas as pd 

# 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) 

# Print the output. 
df

现在,在收集我的数据时,我们使用的这些表格称为 NAR 表格,我们在每个医院共享。现在数据收集工具的编码是,当使用 NAR 表单时,它的编码为 1,当它不使用时,它的编码为 2,最后当表单提交给数据管理员时,它的编码为 3。我想将这些结果表示为一个图表,其中NAR_forms_used的列具有此代码,其中1 表示 yes2 表示 No3 表示 empty。如何在每家医院的图中表示这些数据?

我试过这个

fig = go.Figure(
    data=[go.Bar(
        x = df['Name'],
        y = df['NAR_forms_used']
                    )],
    layout=go.Layout(
        xaxis=dict(showgrid=False),
        yaxis=dict(showgrid=False),
    )
)

fig.show()

但结果不是我想要的,我怎样才能做得更好?

标签: pythonpandasplotly

解决方案


既然你已经用 plotly 标记了这个问题并且还没有得到一个 plotly 的答案,我会这样做:

情节1:

在此处输入图像描述

代码 1:

# imports
import plotly.graph_objects as go
from plotly.offline import iplot
import pandas as pd
import 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 type
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()

# 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 figure
fig = go.Figure()

# add one trace per NAR type and show counts per hospital
for 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 presentable

fig.update_layout(title='NAR per hospital',
                  yaxis=dict(title='<i>count of NAR types</i>'),
                  xaxis=dict(title='<i>Hospital</i>',
                            )
                 )


fig.show()

您可能知道,Nick 医院没有 NAR 3 型,Krish 医院也没有 NAR 1 型,所以这就是为什么这个数字乍一看可能有点奇怪。当您向样本添加更多数据时,这一切都很有意义:

情节2:

在此处输入图像描述

代码 2:

# imports
import plotly.graph_objects as go
from plotly.offline import iplot
import pandas as pd
import numpy as np

# intialise data of lists. 
data = {'Name':['Nick hospital', 'Nick hospital', 'Nick hospital', 'Nick hospital','Nick hospital', 'Nick hospital', 'Krish hospital', 'Krish hospital','Krish hospital', 'Krish hospital'], 
        'NAR_forms_used':[3, 3, 3, 2, 1, 2, 2, 2, 3, 1]
       } 

# Create DataFrame 
df = pd.DataFrame(data)

# get counts per NAR type
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()

# 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 figure
fig = go.Figure()

# add one trace per NAR type and show counts per hospital
for 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 presentable

fig.update_layout(title='NAR per hospital',
                  yaxis=dict(title='<i>count of NAR types</i>'),
                  xaxis=dict(title='<i>Hospital</i>',
                            )
                 )


fig.show()

推荐阅读