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
Plotly instruction reference
- ref: https://plot.ly/python/offline/ and ref: https://plot.ly/python/ipython-notebook-tutorial/#import-data
Import Plotly Library
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)
Overview Data
- The example file is the World Population, Birthrate and Deathrate by Country
## 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()
Create and View Table Online/Offline
- Table will automatically open in Plotly website
## Create a table
table = ff.create_table(df.head(5))
py.iplot(table, filename='world population table', auto_open=True)
- 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
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)
- Link to Plotly Dashboard - Bar Graph - https://plot.ly/~jasmin.c.johnson/38/birthrate-by-country/#/
Code to View Dashboard Offline
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
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)
- 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
py.iplot({
"data": [go.Scatter(x=[1, 2, 3, 4], y=[4, 3, 2, 1])],
"layout": go.Layout(title="hello world")
},auto_open=True)
Link to Plotly Dashboard - Scatter Graph Examaple
- https://plot.ly/~jasmin.c.johnson/46/hello-world/#/