I would like to separate plots that are appearing on final graph. I have data on hospitals and thats why I want to separate so that I have individual hospital graph. Each hospital to have its own separate graph. Here is my working code
# 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()
if you notice the final graph has all hospitals in one graph but I want to separate them and have each hospitals graph separate so that I can add on a dashboard using a drop down selection of a hospital. Kindly assist me separate this graph output in that Nick Hospital to have its own graph and that is same to Krish hospital
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()