Plotly (Plot.ly as its URL goes), is a tech-computing company based in Montreal. It is known for developing and providing online analytics, statistics and graphing tools for individuals or companies. It also develops/provides scientific graphing libraries for Arduino, Julia, MATLAB, Perl, Python, R and REST.
Python Plotly Library
Plotly’s Python graphing library makes interactive graphs online and allows us to save them offline if need be.
Why Plotly
Plotly has got some amazing features that make it better than other graphing libraries:
- It is interactive by default
- Charts are not saved as images but serialized as JSON, making them open to be read with R, MATLAB, Julia and others easily
- Exports vector for print/publication
- Easy to manipulate/embed on web
Getting Started
We need PIP (python package installer) to get working with plotly, we’ll also need to create an account with plotly in case we want to use the online facilities. For this lesson, we’ll stick to offline usage only.
Installation
To install plotly, open a terminal window and run the following command:
sudo pip install plotly
This may take a few minutes to install to collect dependencies and download them:
Using Plotly
To use plotly in any of the Python scripts, we will need to import plotly as:
import plotly
A few more steps and you are ready to use plotly. For Online usage, you need to set up credentials. This can be done as:
plotly.tools.set_credentials_file(username="YourUsernameHere", api_key='YourAPIkeyHere')
For offline usage, we need to call plot like the following for offline usage:
plotly.offline.plot()
Test Installation
Before we can start using plotly, let’s test its installation with an easy script:
import plotly
print(plotly.__version__)
Let’s see the output for this program:
As expected, this returns the latest version of plotly.
Making a Simple Graph
Let’s start with a simple “Hello, World” program with a sample code snippet:
import plotly
from plotly.graph_objs import Scatter, Layout
plotly.offline.plot({
"data": [Scatter(x=[1, 2, 3, 4], y=[4, 3, 2, 1])],
"layout": Layout(title="hello world")
})
Let’s see the output for this program:
As cleary visible, this graph is saved as an HTML file in the same directory as the script.
Basic Charts
To start visualising data, we will start with Basic Charts using Plotly and then move to more complex examples which shows time-related plotting.
Scatter Plot
We’ll create a basic chart based on some random data using numpy. If numpy isn’t installed on your machine, install it using this command:
pip install numpy
Here is a sample program to show a scatter plot:
import plotly
import plotly.graph_objs as go
# Create random data with numpy
import numpy as np
N = 1000
random_x = np.random.randn(N)
random_y = np.random.randn(N)
# Create a trace
trace = go.Scatter(
x = random_x,
y = random_y,
mode="markers"
)
data = [trace]
# Plot and embed in ipython notebook!
plotly.offline.plot(data, filename="basic-scatter")
Let’s see the output for this program:
In this script, we also provided a name for the HTML file.
Line and Scatter Plot
We can create some more sophisticated/ informative plots such as Line Scatter plot in a similar manner as above:
import plotly
import plotly.graph_objs as go
# Create random data with numpy
import numpy as np
N = 100
random_x = np.linspace(0, 1, N)
random_y0 = np.random.randn(N)+5
random_y1 = np.random.randn(N)
random_y2 = np.random.randn(N)-5
# Create traces
trace0 = go.Scatter(
x = random_x,
y = random_y0,
mode="markers",
name="markers"
)
trace1 = go.Scatter(
x = random_x,
y = random_y1,
mode="lines+markers",
name="lines+markers"
)
trace2 = go.Scatter(
x = random_x,
y = random_y2,
mode="lines",
name="lines"
)
data = [trace0, trace1, trace2]
plotly.offline.plot(data, filename="scatter-mode")
Let’s see the output for this program:
This is not it. This graph is much more informative than it looks right now. Move the mouse pointer to any of the plotted point and you’ll see more information about that point:
Box Plots
Box Plots are quite informative and helpful especially when you have too much to show from very little data. Let’s try and create one:
import random
import plotly
from numpy import *
N = 30. # Number of boxes
# generate an array of rainbow colors by fixing the saturation and lightness of the HSL representation of colour
# and marching around the hue.
c = ['hsl('+str(h)+',50%'+',50%)' for h in linspace(0, 360, N)]
# Each box is represented by a dict that contains the data, the type,
# and the colour.
# Use list comprehension to describe N boxes, each with a different colour and
# with different randomly generated data:
data = [{
'y': 3.5*sin(pi * i/N) + i/N+(1.5+0.5*cos(pi*i/N))*random.rand(10),
'type':'box',
'marker':{'color': c[i]}
} for i in range(int(N))]
# format the layout
layout = {'xaxis': {'showgrid':False,'zeroline':False, 'tickangle':60,'showticklabels':False},
'yaxis': {'zeroline':False,'gridcolor':'white'},
'paper_bgcolor': 'rgb(233,233,233)',
'plot_bgcolor': 'rgb(233,233,233)',
}
plotly.offline.plot(data)
Let’s see the output for this program:
Again, we moved the mouse point to one of the point to explore more information about that point.
Contour Plots
Contour Plots are one of most commonly used scientific plots:
from plotly import tools
import plotly
import plotly.graph_objs as go
trace0 = go.Contour(
z=[[2, 4, 7, 12, 13, 14, 15, 16],
[3, 1, 6, 11, 12, 13, 16, 17],
[4, 2, 7, 7, 11, 14, 17, 18],
[5, 3, 8, 8, 13, 15, 18, 19],
[7, 4, 10, 9, 16, 18, 20, 19],
[9, 10, 5, 27, 23, 21, 21, 21],
[11, 14, 17, 26, 25, 24, 23, 22]],
line=dict(smoothing=0),
)
trace1 = go.Contour(
z=[[2, 4, 7, 12, 13, 14, 15, 16],
[3, 1, 6, 11, 12, 13, 16, 17],
[4, 2, 7, 7, 11, 14, 17, 18],
[5, 3, 8, 8, 13, 15, 18, 19],
[7, 4, 10, 9, 16, 18, 20, 19],
[9, 10, 5, 27, 23, 21, 21, 21],
[11, 14, 17, 26, 25, 24, 23, 22]],
line=dict(smoothing=0.85),
)
data = tools.make_subplots(rows=1, cols=2,
subplot_titles=('Without Smoothing', 'With Smoothing'))
data.append_trace(trace0, 1, 1)
data.append_trace(trace1, 1, 2)
plotly.offline.plot(data)
Let’s see the output for this program:
These kind of plots are used a lot while showing heat map data.
Financial Charts
Financial Charts are mycg more complex to read but are easy to make with Plotly. Let;s see some types of the charts taht can be made with Plotly.
Time Series Plot
Let’s start with a Time Series Plot. We will make use of sample data by Plotly itself which served by Github Repository. Here is a sample program:
import plotly
import plotly.graph_objs as go
import pandas as pd
df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv")
data = [go.Scatter(
x=df.Date,
y=df['AAPL.Close'])]
plotly.offline.plot(data)
Let’s see the output for this program:
If pandas
isn’t installed on your machine, install it using this command:
pip install pandas
OHLC Chart
OHLC Charts are also an excellent way to explain time-series related data for non-uniform distribution. Let’s look at a code snippet:
import plotly
import plotly.graph_objs as go
from datetime import datetime
open_data = [33.0, 33.3, 33.5, 33.0, 34.1]
high_data = [33.1, 33.3, 33.6, 33.2, 34.8]
low_data = [32.7, 32.7, 32.8, 32.6, 32.8]
close_data = [33.0, 32.9, 33.3, 33.1, 33.1]
dates = [datetime(year=2013, month=10, day=10),
datetime(year=2013, month=11, day=10),
datetime(year=2013, month=12, day=10),
datetime(year=2014, month=1, day=10),
datetime(year=2014, month=2, day=10)]
trace = go.Ohlc(x=dates,
open=open_data,
high=high_data,
low=low_data,
close=close_data)
data = [trace]
plotly.offline.plot(data, filename="ohlc_datetime")
Let’s see the output for this program:
Conclusion
In this lesson, we studied another excellent Python library which is used for visualisation and offline usage.
Read more Machine Learning posts here.