Data Visualization II - Plotly

A data dashboard is an information management tool that visually tracks, analyzes and displays key performance indicators, metrics and key data points. Behind the scenes, a dashboard connects to your files, attachments, services and API’s, but on the surface displays all this data in the form of tables, line charts, bar charts and gauges.

Plotly provides a web-service for hosting graphs and we can view the dashboard offline as well.


Goal: Data Visualization in Plotly

- Import Data into Plotly
- You can execute the code when new data avaiable on the daily or weekly or quarterly basis
- Create the table then view the table online or offline
- Plot the dashboard online or offline  


Import Plotly Library

In [3]:
import plotly.plotly as py
import plotly.figure_factory as ff

from plotly import __version__
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot

import plotly.plotly as py
import plotly.graph_objs as go

print(__version__) # requires version >= 1.9.0

init_notebook_mode(connected=True)
3.1.0

Overview Data
- The example file is the World Population, Birthrate and Deathrate by Country

In [4]:
## Import the file
df = pd.read_csv('data/LifePlotly.csv')

## You can import the file from sepecific website, for example if store the file in github
## df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/LifePlotly.csv")

df.head()

## View the country in 88th row
country = df.CountryName
country[88]

## Calculates the standard deviation for each column.
df.std()
Out[4]:
CountryName Population Birthrate Deathrate
0 Aruba 31056997 46.60 20.34
1 Afghanistan 3581655 15.11 5.22
2 Angola 32930091 17.14 4.61
3 Albania 57794 22.46 3.27
4 Andorra 71201 8.71 6.25
Out[4]:
'Grenada'
Out[4]:
Population    1.178913e+08
Birthrate     1.117672e+01
Deathrate     4.990026e+00
dtype: float64


Create and View Table Online/Offline
- Table will automatically open in Plotly website

In [5]:
## Create a table
table = ff.create_table(df.head(5))

py.iplot(table, filename='world population table', auto_open=True)
Out[5]:

- Link to Plotly Table - https://plot.ly/~jasmin.c.johnson/14/#/


Code to View Table Offline
- plotly.offline.iplot(table, filename='world population table')


Plot Dashboard Online or Offline

- We can view the dashboard - "Birthrate by Country"" online or offline
- Dashboard in Bar graph with/without title
- Dashboard in Scatter graph with title


View Dashbord in Bar Graph Online with Title

In [6]:
data = [go.Bar(x=df.CountryName,
            y=df.Birthrate)]

py.iplot({
    "data": [go.Bar(x=df.CountryName, y=df.Birthrate)],
    "layout": go.Layout(title="Birthrate_by_Country")
}, auto_open=True)
Out[6]:

- Link to Plotly Dashboard - Bar Graph - https://plot.ly/~jasmin.c.johnson/38/birthrate-by-country/#/


Code to View Dashboard Offline

In [ ]:
plotly.offline.iplot({
    "data": [go.Bar(x=df.CountryName, y=df.Birthrate)],
    "layout": go.Layout(title="Birthrate_by_Country")})


View Dashbord in Scatter Graph Online with Title

In [7]:
trace0 = go.Scatter(
    x=df.CountryName,
    y=df.Birthrate,
    name='Birthrate'
)

trace1 = go.Scatter(
    x=df.CountryName,
    y=df.Population,
    name='Population'
)

data = [trace0, trace1]
fig = go.Figure(data=data)

py.iplot(fig, filename='birthrate_population_by_country',auto_open=True)
Out[7]:

- Link to Plotly Dashboard - Scatter Chart - https://plot.ly/~jasmin.c.johnson/18/birthrate-vs-population/#/


Example of How to Creat a Scatter Graph Dashboard

In [8]:
py.iplot({
    "data": [go.Scatter(x=[1, 2, 3, 4], y=[4, 3, 2, 1])],
    "layout": go.Layout(title="hello world")
},auto_open=True)
Out[8]:

Link to Plotly Dashboard - Scatter Graph Examaple
- https://plot.ly/~jasmin.c.johnson/46/hello-world/#/