In [110]:
import plotly
from plotly.offline import download_plotlyjs, init_notebook_mode, iplot
init_notebook_mode() # run at the start of every notebook
In [111]:
from plotly.offline import plot
from plotly.graph_objs import *
from plotly.tools import FigureFactory as FF
import plotly.plotly as py
import numpy as np
In [107]:
def plot_pie(labels,values,title):
fig = {
'data': [{'labels': labels,
'values': values,
'type': 'pie'}],
'layout': {'title': title}
}
return iplot(fig,show_link=False)
def plot_barchart(labels,values,titles,maintitle):
trace1 = Bar(
x=labels,
y=values[0],
name=titles[0]
)
trace2 = Bar(
x=labels,
y=values[1],
name=titles[1]
)
data = [trace1, trace2]
layout = Layout(
title = maintitle,
barmode='group'
)
fig = Figure(data=data, layout=layout)
return py.iplot(fig,show_link=False)
def plot_scatter(labels,values,titles,maintitle):
trace0 = Scatter(
x=labels,
y=values[0],
name=titles[0],
mode='markers',
marker=dict(
size=np.array(values[0])/2000,
)
)
trace1 = Scatter(
x=labels,
y=values[1],
name=titles[1],
mode='markers',
marker=dict(
size=np.array(values[1])/2000,
)
)
data = [trace0,trace1]
layout = Layout(
title = maintitle,
showlegend=True,
height=600,
width=700,
)
fig = Figure(data=data, layout=layout)
return py.iplot(fig,filename="Scatter")
In [4]:
import pandas as pd
In [5]:
data = pd.read_csv('data.csv',encoding='utf-8')
data = data.drop(['Unnamed: 0'],axis=1)
In [6]:
idata=data[['Reviewer Username','Gender','Age Group','Product Type','Price(Dollar)']]
import plotly.plotly as py
import numpy as np
from plotly.tools import FigureFactory as FF
index=np.random.choice(idata.shape[0], 10)
table = FF.create_table(idata.ix[index])
py.iplot(table,show_link=False)
Out[6]:
In [7]:
ag=data['Age Group'].value_counts()
plot_pie(ag.keys().tolist(),ag.values.tolist(),'Age Group Percentage In Amazon Reviews')
In [8]:
g = data['Gender'].value_counts()
plot_pie(g.keys().tolist(),g.values.tolist(),'Gender Percentage in Amazon Reviews')
In [9]:
g = idata['Product Type'].value_counts()
g['others']= g['$0']
g= g.drop('$0')
plot_pie(g.keys().tolist(),g.values.tolist(),'Product Category Distribution')
In [89]:
females = idata.where(idata['Gender']=='female').dropna()
males = idata.where(idata['Gender']=='male').dropna()
labels = ['Books','Electronics','Desktop','Others']
values =[]
values.append(males['Product Type'].value_counts().values)
values.append(females['Product Type'].value_counts().values)
titles = ['Male','Female']
plot_barchart(labels,values,titles,'Gender specific categorywise Purchases')
Out[89]:
In [ ]: