Jovian
⭐️
Sign In

Interactive Visualization using Plotly

This kernel covers interactive visualization using Plotly . The following plots are covered

1.Pie Chart
2.Histogram
3.Scatter Plot
4.Bar Chart
5.Distribution Plots
6.Bubble Plot

In [1]:
# importing libraries
import numpy as np 
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.preprocessing import LabelEncoder
import category_encoders as ce
%matplotlib inline
In [2]:
# loading dataset 
df = pd.read_csv("../input/widsdatathon2020/training_v2.csv")
In [3]:
# Number of missing values in the data
df = df[df.columns[0:50]]
df.columns
Out[3]:
Index(['encounter_id', 'patient_id', 'hospital_id', 'hospital_death', 'age',
       'bmi', 'elective_surgery', 'ethnicity', 'gender', 'height',
       'hospital_admit_source', 'icu_admit_source', 'icu_id', 'icu_stay_type',
       'icu_type', 'pre_icu_los_days', 'readmission_status', 'weight',
       'albumin_apache', 'apache_2_diagnosis', 'apache_3j_diagnosis',
       'apache_post_operative', 'arf_apache', 'bilirubin_apache', 'bun_apache',
       'creatinine_apache', 'fio2_apache', 'gcs_eyes_apache',
       'gcs_motor_apache', 'gcs_unable_apache', 'gcs_verbal_apache',
       'glucose_apache', 'heart_rate_apache', 'hematocrit_apache',
       'intubated_apache', 'map_apache', 'paco2_apache', 'paco2_for_ph_apache',
       'pao2_apache', 'ph_apache', 'resprate_apache', 'sodium_apache',
       'temp_apache', 'urineoutput_apache', 'ventilated_apache', 'wbc_apache',
       'd1_diasbp_invasive_max', 'd1_diasbp_invasive_min', 'd1_diasbp_max',
       'd1_diasbp_min'],
      dtype='object')
In [4]:
from plotly.offline import init_notebook_mode,iplot
import plotly.graph_objects as go
import cufflinks as cf
init_notebook_mode(connected=True)

Pie Chart

In [5]:
#labels
lab = df["hospital_death"].value_counts().keys().tolist()
#values
val = df["hospital_death"].value_counts().values.tolist()
trace = go.Pie(labels=lab, 
                values=val, 
                marker=dict(colors=['Orange']), 
                # Seting values to 
                hoverinfo="value"
              )
data = [trace]

layout = go.Layout(title="Hospital Death Distribution")
fig = go.Figure(data = data,layout = layout)
iplot(fig)



In [6]:
#labels
lab = df["hospital_admit_source"].value_counts().keys().tolist()
#values
val = df["hospital_admit_source"].value_counts().values.tolist()
trace = go.Pie(labels=lab, 
                values=val, 
                marker=dict(colors=['Pink']), 
                # Seting values to 
                hoverinfo="value"
              )
data = [trace]

layout = go.Layout(title="Hospital Admit Source")
fig = go.Figure(data = data,layout = layout)
iplot(fig)

Histogram

In [7]:
# defining data
trace = go.Histogram(x=df['apache_3j_diagnosis'],nbinsx=50,histnorm='percent')
data = [trace]
# defining layout
layout = go.Layout(title="apache_3j_diagnosis Distribution")
# defining figure and plotting
fig = go.Figure(data = data,layout = layout)
iplot(fig)

Scatter Plot

In [8]:
#defining data
trace = go.Scatter(y = df['pre_icu_los_days'],x=df['weight'],text = df['hospital_death'],mode='markers')
data=[trace]
#defining layout
layout = go.Layout(title='Weight Vs pre_icu_los_days Scatter Plot',yaxis=dict(title='pre_icu_los_days'),xaxis=dict(title='Weight'),hovermode='closest')
#defining figure and plotting
figure = go.Figure(data=data,layout=layout)
iplot(figure)

Bar Chart

In [9]:
y=[]
heart_rates  = []
for i in list(df['icu_stay_type'].unique()):
    result = df[df['icu_stay_type']==i]['weight'].mean()
    heart_rate = df[df['icu_stay_type']==i]['heart_rate_apache'].mean()
    y.append(result)
    heart_rates.append(heart_rate)
In [10]:
#defining data
trace = go.Bar(x = list(df['icu_stay_type'].unique()),y=y,marker=dict(color=heart_rates,colorscale='Viridis',showscale=True),text = heart_rates)
data=[trace]
#defining layout
layout = go.Layout(title='Weight/heart_rate_apache vs icu_stay_type Bar Chart',xaxis=dict(title='icu_stay_type'),yaxis=dict(title='Weight'),hovermode='closest')
#defining figure and plotting
figure = go.Figure(data=data,layout=layout)
iplot(figure)

Distribution Plots

In [11]:
df1 =df.dropna()
In [12]:
import plotly.figure_factory as ff
#defining data
a = df1[df1['icu_admit_source']=='Floor']['glucose_apache']
b = df1[df1['icu_admit_source']=='Accident & Emergency']['glucose_apache']
c = df1[df1['icu_admit_source']=='Operating Room / Recovery']['glucose_apache']
d = df1[df1['icu_admit_source']=='Other Hospital']['glucose_apache']
e = df1[df1['icu_admit_source']=='Other ICU']['glucose_apache']

hist_data=[a,b,c,d,e]
group_labels=['Floor','Accident & Emergency','Operating Room / Recovery','Other Hospital','Other ICU']
#defining fig and plotting
fig = ff.create_distplot(hist_data,group_labels,bin_size=[1,1,1,1,1],show_curve=False)
fig.update_layout(title_text='Distribution for glucose_apache')
iplot(fig)
In [13]:
#defining data
a = df1[df1['icu_admit_source']=='Floor']['creatinine_apache']
b = df1[df1['icu_admit_source']=='Accident & Emergency']['creatinine_apache']
c = df1[df1['icu_admit_source']=='Operating Room / Recovery']['creatinine_apache']
d = df1[df1['icu_admit_source']=='Other Hospital']['creatinine_apache']
e = df1[df1['icu_admit_source']=='Other ICU']['creatinine_apache']

hist_data=[a,b,c,d,e]
group_labels=['Floor','Accident & Emergency','Operating Room / Recovery','Other Hospital','Other ICU']
#defining fig and plotting
fig = ff.create_distplot(hist_data,group_labels,bin_size=[1,1,1,1,1],show_curve=False)
fig.update_layout(title_text='Distribution for creatinine_apache')
iplot(fig)

Bubble Chart

In [14]:
#defining data
data=[
    go.Scatter(x = df1['resprate_apache'],y=df1['sodium_apache'],
               text=df1['icu_stay_type'],
                mode='markers',
               marker=dict(size=df1['apache_post_operative']*15, color=df1['hospital_death'],showscale=True),
              )]
#defining layout
layout = go.Layout(title='resprate_apache vs sodium_apache with hospital_death and icu_stay_type',xaxis=dict(title='sodium_apache'),yaxis=dict(title='resprate_apache'),hovermode='closest')
#defining figure and plotting
figure = go.Figure(data=data,layout=layout)
iplot(figure)