2

Good people, I have this data which I do what to represent on a plot

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 

Now when collecting my data there are these forms we use called NAR form, which we share per hospital. Now the data collection tool is coded in that when NAR form is used, its coded 1 and when it's not used then it's coded 2 and finally when the form was submitted to the data clerk empty its coded 3. I want to represent these results on a graph where when the column for NAR_forms_used has this code which 1 is for yes, 2 is for No and 3 is for empty. How can I represent this data on a plot per hospital?

I tried this

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

But the results is not what I want, How can I do it better?

2 Answers 2

1

Since you've tagged the question with plotly and not gotten a plotly answer yet, here's how I would do it:

Plot 1:

enter image description here

Code 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() 

As you probably know, there is no NAR type 3 for Nick Hospital and no NAR type 1 for Krish Hospital, so that's why the figure might seem a little strange at first glance. It all makes sense when you add some more data to your sample:

Plot 2:

enter image description here

Code 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() 
Sign up to request clarification or add additional context in comments.

1 Comment

How can I possibly add this to plotly-dash app and plotly per hospital differently, probably by having a dropdown of hospitals
0

If you need to plot the number of NAR_forms_used for each hospital I suggest you to use seaborn. Here's a snippet:

import pandas as pd import seaborn as sns import numpy as np # intialise data of lists and create DataFrame data = {'Name':np.random.choice(['Nick hospital', 'Krish hospital'], size=80), 'NAR_forms_used':np.random.choice([1,2,3], size=80)} df = pd.DataFrame(data) df_to_plot = (df.groupby('Name')['NAR_forms_used'] .value_counts() .to_frame() .rename({'NAR_forms_used': 'NAR_forms_count'}, axis='columns') .reset_index()) sns.barplot(data=df_to_plot, x='Name', y='NAR_forms_count', hue='NAR_forms_used') 

enter image description here

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.