UK Alcohol dealth data (2014)The following outlines how we can integrate Python with Plotly [data] |
Coding
The following is an outline of the coding:
import numpy as np import pandas as pd import sys import matplotlib.pyplot as plt import statsmodels.api as sm import math xval = 'State'; yval = 'City'; zval='Population' file='1111' fdata='al.csv' type=0 import plotly.tools as tls import plotly.graph_objs as go import plotly.plotly as py import matplotlib.pyplot as plt import plotly.plotly as py tls.set_credentials_file(username="zzzz",api_key="zzzz") if (len(sys.argv)>1): file=str(sys.argv[1]) if (len(sys.argv)>2): xval=str(sys.argv[2]) if (len(sys.argv)>3): yval=str(sys.argv[3]) if (len(sys.argv)>4): fdata=str(sys.argv[4]) if (len(sys.argv)>5): zval=str(sys.argv[5]) ver=pd.read_csv(fdata) ver = ver.replace(np.nan,0, regex=True) x=ver['Class'] y=ver['Mental and behavioural disorders due to use of alcohol'] y1=ver['Degeneration of nervous system due to alcohol'] y2=ver['Alcoholic polyneuropathy'] y3=ver['Alcoholic cardiomyopathy'] y4=ver['Alcoholic gastritis'] y5=ver['Alcoholic liver disease'] df = pd.DataFrame({'x': x, 'y': y, 'y2':y2, 'y3':y3, 'y4':y4, 'y5':y5}) data = [ go.Bar( x=df['x'], # assign x as the dataframe column 'x' y=df['y'], name='Mental and behavioural disorders due to use of alcohol' ), go.Bar( x=df['x'], y=df['y2'], name='Degeneration of nervous system due to alcohol' ), go.Bar( x=df['x'], y=df['y3'], name='Alcoholic polyneuropathy' ), go.Bar( x=df['x'], y=df['y4'], name='Alcoholic gastritis' ), go.Bar( x=df['x'], y=df['y5'], name='Alcoholic liver disease' ) ] layout = go.Layout( barmode='stack', title='Stacked Bar with Pandas' ) fig = go.Figure(data=data, layout=layout) url = py.plot(data, file,layout=layout,auto_open=False,show_link=False,sharing='public') print url