You can run this code interactively by just forking this project, use the FORK button on your right 👉
This project shows a quick overview of Plotly and Dash, two amazing visualization products. We'll start with "pure" Python Plotly. Part 2 (2. Intro to Dash
on the right nav in your screen 👉) introduces Dash.
Let's get started!
Plotly is a free and open source visualization library for Python. In contrast to other Python viz libraries like for example matplotlib or seaborn, Plotly charts are interactive (javascript based).
RMOTR Notebooks supports Plotly out of the box, you can just import it and use it, both in your Lab (notebooks) and also in the public project view (to share your beautiful visualizations with the world). Let's start by building a quick chart (Plotly's version of "hello world"):
import plotly
import plotly.graph_objs as go
plotly.offline.init_notebook_mode(connected=True)
plotly.offline.iplot({
"data": [go.Scatter(x=[1, 2, 3, 4], y=[4, 3, 2, 1])],
"layout": go.Layout(title="hello world")
})
As you can see, plotting with Plotly is simple and the results are both beautiful and useful.
Plotly simplifies the process by using a consistent JSON API. Our previous example is simple, we're just passing a dictionary to plotly.offline.iplot
. For more flexibility you can use Figure
s and Layout
s. Same example using those:
trace1 = go.Scatter(x=[1, 2, 3, 4], y=[4, 3, 2, 1])
data = [trace1]
layout=go.Layout(title="Hello World", xaxis={'title':'X Axis'}, yaxis={'title':'Y Axis'})
figure=go.Figure(data=data, layout=layout)
plotly.offline.iplot(figure)
As you can see, both Figure
and Layout
provide the "structure" of the plot. The actual blue line that you see is a go.Scatter
object. Plotly supports multiple types of charts (all under graph_objs
). And they can be highly customized:
trace1 = go.Scatter(x=[1, 2, 3, 4], y=[4, 3, 2, 1], marker={'color': 'red', 'symbol': 104, 'size': 10},
mode="markers+lines", text=['Four', 'Three', 'Two', 'One'], name='1st Trace')
data = [trace1]
layout=go.Layout(title="Hello World", xaxis={'title':'X Axis'}, yaxis={'title':'Y Axis'})
figure=go.Figure(data=data, layout=layout)
plotly.offline.iplot(figure)
Plotly's Figure
JSON Object:
figure
A more interesting example¶
We're going to use Cryptowatch API to get price information of Bitcoin and plot it using both an OHLC chart and a Candlestick chart:
# defined in utils.py, fork the project to see it in detail
from utils import get_historic_price
btc = get_historic_price('btc', after='2018-01-01', period='daily')
btc.head()
With our BTC dataframe, we can now build the Ohlc
chart:
trace = go.Ohlc(
x=btc.index,
open=btc['OpenPrice'],
high=btc['HighPrice'],
low=btc['LowPrice'],
close=btc['ClosePrice'])
data = [trace]
plotly.offline.iplot(data)
To create a Candlestick, we'll just swap the graph object we use to Candlestick
. I'll use a smaller dataset (prices starting in May) so you can appreciate better how the candles are drawn:
df = btc.loc['2018-05':]
trace = go.Candlestick(
x=df.index,
open=df['OpenPrice'],
high=df['HighPrice'],
low=df['LowPrice'],
close=df['ClosePrice'])
data = [trace]
plotly.offline.iplot(data)
Final thoughts and more resources¶
As you can see, plotly combines an extremely simple API with beautiful and powerful visualizations. There are A LOT more charts for you to take a look at.
If you want to learn more, you can use both the User Guide and the Graph objects reference.
Next...¶
Navigate now to 2. Intro to Dash
(using the navbar on the right 👉) to see a quick intro to Dash, Plotly's new flagship product.