WePlot

Introduction

A tutorial for our plotly wrapper library weplot https://github.com/arraystream/weplot

In [2]:
import plotly
plotly.offline.init_notebook_mode()
In [3]:
import pandas as pd
import numpy as np
from weplot import FigureBuilder, FigureHolder
from weplot import Line, Scatter, Bar, Box, Histogram, Heatmap, Scatter3D,Surface
from weplot import Shape, Annotation, XAxis, YAxis, ZAxis
import plotly.figure_factory as plotly_tool

Line Plot

In [4]:
np.random.seed(100)
ts = pd.Series(np.random.randn(1000), index=pd.date_range('1/1/2000', periods=1000))
ts = ts.cumsum()
df = pd.DataFrame(np.random.randn(1000, 4), index=ts.index, columns=list('ABCD'))
df = df.cumsum()
In [5]:
builder = FigureBuilder()
builder.add(Line(x=df.index,y=df.A,name='A'))
builder.add(Line(x=df.index,y=df.B,name='B',line=dict(dash='dot')))
builder.add(Line(x=df.index,y=df.C,name='C'))
builder.add(Line(x=df.index,y=df.D,name='D',line=dict(dash='dot')))
builder.add(Shape(x0='2001-02-04',x1='2001-04-06',fillcolor='#d3d3d3',opacity=0.7,line=dict(width=0)))
builder.add(Shape(x0='2002-4-20',x1='2002-05-22',fillcolor='#d3d3d3',opacity=0.7,line=dict(width=0)))
builder.add(Annotation(x='2001-2-4',y=40,text='grey shade',showarrow=True))
builder.update_layout(title='Line Plot').plot()

Subplot

In [6]:
builder = FigureBuilder()
builder.add(Line(x=df.index,y=df.A,name='A'),1,1)
builder.add(Line(x=df.index,y=df.B,name='B',line=dict(dash='dot')),1,2)
builder.add(Line(x=df.index,y=df.C,name='C'),2,1)
builder.add(Line(x=df.index,y=df.D,name='D',line=dict(dash='dot')),2,2)
builder.add(Shape(x0=df.index[0],x1=df.index[-1],y0=df.A.mean(),y1=df.A.mean(),xref='x1',yref='y1',type='line',line=dict(color='red',dash='dot')))
builder.add(Annotation(x='2001-2-4',y=df.A.mean(),text='mean of A',xref='x1',yref='y1',showarrow=True))
builder.add(Shape(x0='2001-2-4',x1='2001-2-4',y0=df.D.min(),y1=df.D.max(),xref='x4',yref='y4',type='line',line=dict(color='blue',dash='dot')))
builder.add(Annotation(x='2001-2-4',y=df.D.loc['2001-2-4'],text='vertical line',xref='x4',yref='y4',showarrow=True))
builder.update_layout(title='Line SubPlot').subplot()
This is the format of your plot grid:
[ (1,1) x1,y1 ]  [ (1,2) x2,y2 ]
[ (2,1) x3,y3 ]  [ (2,2) x4,y4 ]

In [7]:
builder = FigureBuilder()
builder.add(Line(x=df.index,y=df.A,name='A'),1,1)
builder.add(Line(x=df.index,y=df.B,name='B',line=dict(dash='dot')),1,2)
builder.add(Line(x=df.index,y=df.C,name='C'),2,1,1,2)
builder.add(Line(x=df.index,y=df.D,name='D',line=dict(dash='dot')),3,1,1,2)
builder.update_layout(title='Line SubPlot Customization').subplot()
This is the format of your plot grid:
[ (1,1) x1,y1 ]  [ (1,2) x2,y2 ]
[ (2,1) x3,y3           -      ]
[ (3,1) x4,y4           -      ]

Area Plot

In [8]:
builder = FigureBuilder()
builder.add(Line(x=df.index,y=df.A,name='A',fill='tozeroy'))
builder.add(Line(x=df.index,y=df.B,name='B',fill='tozeroy'))
builder.update_layout(title='Line Area Plot').plot()

Multiple Y-Axes

In [9]:
builder = FigureBuilder()
builder.add(Line(x=[1, 2, 3],y=[4, 5, 6],name='yaxis1 data',mode='lines+markers'))
builder.add(Line(x=[2, 3, 4],y=[40, 50, 60],name='yaxis2 data',mode='lines+markers',yaxis='y2'))
builder.add(Line(x=[4, 5, 6],y=[400, 500, 600],name='yaxis3 data',mode='lines+markers',yaxis='y3'))
builder.add(Line(x=[5, 6, 7],y=[4000, 5000, 6000],name='yaxis4 data',mode='lines+markers',yaxis='y4'))
builder.add(XAxis(title='',domain=[0.3, 0.7]))
builder.add(YAxis(title='yaxis1 title'))
builder.add(YAxis(title='yaxis2 title',name='yaxis2',anchor='free',overlaying='y',side='left',position=0.15))
builder.add(YAxis(title='yaxis3 title',name='yaxis3',anchor='x',overlaying='y',side='right'))
builder.add(YAxis(title='yaxis4 title',name='yaxis4',anchor='free',overlaying='y',side='right',position=0.85))
builder.update_layout(title='Multiple Y-Axies Plot').plot()

Bar Plot

In [10]:
df2 = pd.DataFrame(np.random.rand(10, 4), columns=['a', 'b', 'c', 'd'])
In [11]:
builder = FigureBuilder()
builder.add(Bar(x=df.index,y=df2.a,name='a'))
builder.add(Bar(x=df.index,y=df2.b,name='b'))
builder.add(Bar(x=df.index,y=df2.c,name='c'))
builder.add(Bar(x=df.index,y=df2.d,name='d'))
builder.update_layout(title='Bar Plot Group',barmode='group').plot()
In [12]:
builder.update_layout(title='Bar Plot Stack',barmode='stack').plot()
In [13]:
builder = FigureBuilder()
builder.add(Bar(x=df.index,y=df2.a,name='a'),1,1)
builder.add(Bar(x=df.index,y=df2.b,name='b'),1,2)
builder.add(Bar(x=df.index,y=df2.c,name='c'),2,1)
builder.add(Bar(x=df.index,y=df2.d,name='d'),2,2)
builder.update_layout(title='Bar Plot Subplot',barmode='group').subplot()
This is the format of your plot grid:
[ (1,1) x1,y1 ]  [ (1,2) x2,y2 ]
[ (2,1) x3,y3 ]  [ (2,2) x4,y4 ]

In [14]:
builder = FigureBuilder()
builder.add(Bar(x=df.index,y=df2.a,name='a').horizontal())
builder.add(Bar(x=df.index,y=df2.b,name='b').horizontal())
builder.add(Bar(x=df.index,y=df2.c,name='c').horizontal())
builder.add(Bar(x=df.index,y=df2.d,name='d').horizontal())
builder.update_layout(title='Bar Plot Group',barmode='group').plot()
In [15]:
builder.update_layout(title='Bar Plot Stack',barmode='stack').plot()

Histogram

In [16]:
df4 = pd.DataFrame({'a': np.random.randn(1000) + 1, 'b': np.random.randn(1000),'c': np.random.randn(1000) - 1}, columns=['a', 'b', 'c'])
In [17]:
builder = FigureBuilder()
builder.add(Histogram(x=df4.a,name='a'))
builder.add(Histogram(x=df4.b,name='b'))
builder.add(Histogram(x=df4.c,name='c'))
builder.update_layout(title='Histogram Stack',barmode='stack',bargap=0.1).plot()
In [18]:
df_min=df4.min().min()
df_max=df4.max().max()
bins=20
builder = FigureBuilder()
builder.add(Histogram(x=df4.a,name='a').xbins(df_min,df_max,bins=bins))
builder.add(Histogram(x=df4.b,name='b').xbins(df_min,df_max,bins=bins))
builder.add(Histogram(x=df4.c,name='c').xbins(df_min,df_max,bins=bins))
builder.update_layout(title='Histogram',barmode='stack',bargap=0.1).plot()
In [19]:
builder = FigureBuilder()
builder.add(Histogram(x=df4.a,name='a').xbins(bins=20))
builder.update_layout(title='Histogram',bargap=0.1,bargroupgap=0.1,showlegend=True).plot()

Box Plot

In [20]:
df5 = pd.DataFrame(np.random.rand(10, 5), columns=['A', 'B', 'C', 'D', 'E'])
In [21]:
builder = FigureBuilder()
builder.add(Box(y=df5.A,name='A'))
builder.add(Box(y=df5.B,name='B'))
builder.add(Box(y=df5.C,name='C'))
builder.add(Box(y=df5.D,name='D'))
builder.add(Box(y=df5.E,name='E'))
builder.update_layout(title='Box Plot',barmode='group').plot()
In [22]:
builder = FigureBuilder()
builder.add(Box(y=df5.A,name='A').horizontal())
builder.add(Box(y=df5.B,name='B').horizontal())
builder.add(Box(y=df5.C,name='C').horizontal())
builder.add(Box(y=df5.D,name='D').horizontal())
builder.add(Box(y=df5.E,name='E').horizontal())
builder.update_layout(title='Box Plot Horizontal',barmode='group').plot()

Density Plot

In [23]:
x1 = np.random.randn(200)-2
x2 = np.random.randn(200)
x3 = np.random.randn(200)+2
x4 = np.random.randn(200)+4
fig = plotly_tool.create_distplot([x1, x2, x3, x4], ['Group 1', 'Group 2', 'Group 3', 'Group 4'], bin_size=.2)

figure = FigureHolder(fig)
figure.update_layout(title='Density Plot')
figure.plot()