Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 609b033

Browse filesBrowse files
committed
reformatted examples that focus on pandas
1 parent c3c8672 commit 609b033
Copy full SHA for 609b033

File tree

Expand file treeCollapse file tree

1 file changed

+146
-197
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+146
-197
lines changed

‎plotly/tools.py

Copy file name to clipboardExpand all lines: plotly/tools.py
+146-197Lines changed: 146 additions & 197 deletions
Original file line numberDiff line numberDiff line change
@@ -2257,55 +2257,84 @@ def create_ohlc(open, high, low, close,
22572257
22582258
:rtype (dict): returns a representation of an ohlc chart figure.
22592259
2260-
Example 1: Plot simple ohlc chart
2260+
Example 1: Simple OHLC chart from a Pandas DataFrame
22612261
```
22622262
import plotly.plotly as py
2263-
import plotly.tools as tls
2264-
from plotly.graph_objs import *
2263+
from plotly.tools import FigureFactory as FF
22652264
2266-
# Add data
2267-
open_data = [33.0, 33.3, 33.5, 33.0, 34.1]
2268-
high_data = [33.1, 33.3, 33.6, 33.2, 34.8]
2269-
low_data = [32.7, 32.7, 32.8, 32.6, 32.8]
2270-
close_data = [33.0, 32.9, 33.3, 33.1, 33.1]
2265+
import pandas.io.data as web
22712266
2272-
# Create ohlc
2273-
ohlc = FigureFactory.create_ohlc(open_data, high_data,
2274-
low_data, close_data)
2267+
df = web.DataReader("aapl", 'yahoo', datetime(2008, 8, 15), datetime(2008, 10, 15))
2268+
fig = FF.create_ohlc(df.Open, df.High, df.Low, df.Close, dates=df.index)
22752269
2276-
# Plot
2277-
py.plot(ohlc, filename='simple ohlc', validate=False)
2270+
py.plot(fig, filename='finance/aapl-ohlc')
22782271
```
22792272
2280-
Example 2: Plot ohlc chart with date labels
2273+
Example 2: Add text and annotations to the OHLC chart
22812274
```
22822275
import plotly.plotly as py
2283-
import plotly.tools as tls
2284-
from plotly.graph_objs import *
2276+
from plotly.tools import FigureFactory as FF
2277+
2278+
import pandas.io.data as web
2279+
2280+
df = web.DataReader("aapl", 'yahoo', datetime(2008, 8, 15), datetime(2008, 10, 15))
2281+
fig = FF.create_ohlc(df.Open, df.High, df.Low, df.Close, dates=df.index)
2282+
2283+
# Update the fig - all options here: https://plot.ly/python/reference/#Layout
2284+
fig['layout'].update({
2285+
'title': 'The Great Recession',
2286+
'yaxis': {'title': 'AAPL Stock'},
2287+
'shapes': [{
2288+
'x0': '2008-09-15', 'x1': '2008-09-15', 'type': 'line',
2289+
'y0': 0, 'y1': 1, 'xref': 'x', 'yref': 'paper',
2290+
'line': {'color': 'rgb(40,40,40)', 'width': 0.5}
2291+
}],
2292+
'annotations': [{
2293+
'text': "the fall of Lehman Brothers",
2294+
'x': '2008-09-15', 'y': 1.02,
2295+
'xref': 'x', 'yref': 'paper',
2296+
'showarrow': False, 'xanchor': 'left'
2297+
}]
2298+
})
2299+
2300+
py.plot(fig, filename='finance/aapl-recession-ohlc', validate=False)
2301+
```
22852302
2303+
Example 3: Customize the OHLC colors
2304+
```
2305+
import plotly.plotly as py
2306+
from plotly.tools import FigureFactory as FF
2307+
from plotly.graph_objs import Line, Marker
22862308
from datetime import datetime
22872309
2288-
# Add data
2289-
open_data = [33.0, 33.3, 33.5, 33.0, 34.1]
2290-
high_data = [33.1, 33.3, 33.6, 33.2, 34.8]
2291-
low_data = [32.7, 32.7, 32.8, 32.6, 32.8]
2292-
close_data = [33.0, 32.9, 33.3, 33.1, 33.1]
2293-
dates = [datetime(year=2013, month=10, day=10),
2294-
datetime(year=2013, month=11, day=10),
2295-
datetime(year=2013, month=12, day=10),
2296-
datetime(year=2014, month=1, day=10),
2297-
datetime(year=2014, month=2, day=10)]
2310+
import pandas.io.data as web
22982311
2299-
# Create ohlc
2300-
ohlc = tls.FigureFactory.create_ohlc(open_data, high_data,
2301-
low_data, close_data,
2302-
dates=dates)
2312+
df = web.DataReader("aapl", 'yahoo', datetime(2008, 1, 1), datetime(2009, 4, 1))
2313+
2314+
# Make increasing ohlc sticks and customize their color and name
2315+
fig_increasing = FF.create_ohlc(df.Open, df.High, df.Low, df.Close, dates=df.index,
2316+
direction='increasing', name='AAPL',
2317+
line=Line(color='rgb(150, 200, 250)'))
2318+
2319+
# Make decreasing ohlc sticks and customize their color and name
2320+
fig_decreasing = FF.create_ohlc(df.Open, df.High, df.Low, df.Close, dates=df.index,
2321+
direction='decreasing',
2322+
line=Line(color='rgb(128, 128, 128)'))
2323+
2324+
# Initialize the figure
2325+
fig = fig_increasing
23032326
2304-
py.plot(ohlc, filename='ohlc with dates', validate=False)
2327+
# Add decreasing data with .extend()
2328+
fig['data'].extend(fig_decreasing['data'])
2329+
2330+
py.iplot(fig, filename='finance/aapl-ohlc-colors', validate=False)
23052331
```
23062332
2307-
Example 3: Plot ohlc chart with Title
2333+
Example 4: OHLC chart with datetime objects
23082334
```
2335+
import plotly.plotly as py
2336+
from plotly.tools import FigureFactory as FF
2337+
23092338
from datetime import datetime
23102339
23112340
# Add data
@@ -2320,101 +2349,10 @@ def create_ohlc(open, high, low, close,
23202349
datetime(year=2014, month=2, day=10)]
23212350
23222351
# Create ohlc
2323-
ohlc = tls.FigureFactory.create_ohlc(open_data, high_data,
2324-
low_data, close_data,
2325-
dates=dates)
2326-
2327-
# Customize layout by using .update()
2328-
fig = ohlc
2329-
fig['layout'].update(title = 'OHLC Chart')
2330-
2331-
py.plot(fig, filename='ohlc with title', validate=False)
2332-
```
2333-
2334-
Example 4: OHLC with increasing vs decreasing keyword arguments
2335-
```
2336-
from datetime import datetime
2337-
2338-
# Add Data
2339-
high_data = [33.2, 33.3750, 33.6250, 33.2500, 34.1880, 33.2, 33.3750,
2340-
33.6250, 33.2500, 34.1880]
2341-
low_data = [32.7, 32.7500, 32.8750, 32.6250, 32.8130, 32.7, 32.7500,
2342-
32.8750, 32.6250, 32.8130,]
2343-
close_data = [33.1, 32.9380, 33.3750, 33.1880, 33.1880, 33.1, 32.9380,
2344-
33.3750, 33.1880, 33.1880]
2345-
open_data = [33.0, 33.3125, 33.5000, 33.0625, 34.1250, 33.0, 33.3125,
2346-
33.5000, 33.0625, 34.1250]
2347-
x = [datetime(year=2013, month=3, day=4),
2348-
datetime(year=2013, month=6, day=5),
2349-
datetime(year=2013, month=9, day=6),
2350-
datetime(year=2014, month=3, day=4),
2351-
datetime(year=2014, month=6, day=5),
2352-
datetime(year=2014, month=9, day=4),
2353-
datetime(year=2015, month=3, day=5),
2354-
datetime(year=2015, month=6, day=6),
2355-
datetime(year=2015, month=9, day=4),
2356-
datetime(year=2016, month=3, day=5)]
2357-
2358-
# Make increasing ohlc sticks and set kwargs
2359-
ohlc_incr = FigureFactory.create_ohlc(open_data,
2360-
high_data,
2361-
low_data,
2362-
close_data,
2363-
dates=x,
2364-
direction='increasing',
2365-
name='XYZ Increasing',
2366-
line=Line(color='rgb(150, 200,
2367-
250)'))
2368-
2369-
# Make decreasing ohlc sticks and set kwargs
2370-
ohlc_decr = FigureFactory.create_ohlc(open_data,
2371-
high_data,
2372-
low_data,
2373-
close_data,
2374-
dates=x,
2375-
direction='decreasing',
2376-
name='XYZ Decreasing',
2377-
showlegend=True,
2378-
line=Line(color='rgb(128, 128,
2379-
128)'))
2380-
2381-
# Set figure with increasing data and layout
2382-
fig = ohlc_incr
2383-
2384-
# Add decreasing data with .extend()
2385-
fig['data'].extend(ohlc_decr['data'])
2386-
2387-
# Plot!
2388-
py.iplot(fig, filename='ohlc keywords', validate=False)
2389-
2390-
```
2391-
2392-
Example 5: Plot OHLC with Pandas
2393-
```
2394-
import pandas.io.data as web
2395-
import pandas as pd
2396-
from datetime import datetime
2397-
2398-
# Get Data
2399-
start = datetime(2010, 1, 1)
2400-
end = datetime(2012, 1, 27)
2401-
df = web.DataReader("aapl", 'yahoo', start, end)
2402-
2403-
# Get Dates
2404-
datelist = (pd.date_range(datetime(2010, 1, 1),
2405-
periods=len(df.Open)).tolist())
2406-
d=[]
2407-
for i in range(len(datelist)):
2408-
d.append(datelist[i].to_datetime())
2409-
2410-
# Make OHLC
2411-
ohlc_panda = tls.FigureFactory.create_ohlc(df.Open, df.High,
2412-
df.Low, df.Close,
2413-
dates=d)
2414-
2415-
# Plot
2416-
py.plot(ohlc_panda, filename='ohlc with pandas', validate=False)
2352+
fig = FF.create_ohlc(open_data, high_data,
2353+
low_data, close_data, dates=dates)
24172354
2355+
py.iplot(fig, filename='finance/simple-ohlc', validate=False)
24182356
```
24192357
"""
24202358
if dates:
@@ -2596,85 +2534,96 @@ def create_candlestick(open, high, low, close,
25962534
25972535
:rtype (dict): returns a representation of candlestick chart figure.
25982536
2599-
Example 1: Plot candlestick chart
2537+
Example 1: Simple candlestick chart from a Pandas DataFrame
26002538
```
26012539
import plotly.plotly as py
2602-
import plotly.tools as tls
2603-
from plotly.graph_objs import *
2540+
from plotly.tools import FigureFactory as FF
2541+
from datetime import datetime
26042542
2605-
# Add data
2606-
high_data = [34.20, 34.37, 33.62, 34.25, 35.18, 33.25, 35.37, 34.62]
2607-
low_data = [31.70, 30.75, 32.87, 31.62, 30.81, 32.75, 32.75, 32.87]
2608-
close_data = [34.10, 31.93, 33.37, 33.18, 31.18, 33.10, 32.93, 33.70]
2609-
open_data = [33.01, 33.31, 33.50, 32.06, 34.12, 33.05, 33.31, 33.50]
2610-
2611-
# Make candlestick
2612-
candle = FigureFactory.create_candlestick(open_data,
2613-
high_data,
2614-
low_data,
2615-
close_data)
2616-
2617-
# Plot!
2618-
py.iplot(candle, filename='candle', validate=False, overwrite=True)
2543+
import pandas.io.data as web
2544+
2545+
df = web.DataReader("aapl", 'yahoo', datetime(2007, 10, 1), datetime(2009, 4, 1))
2546+
fig = FF.create_candlestick(df.Open, df.High, df.Low, df.Close, dates=df.index)
2547+
py.plot(fig, filename='finance/aapl-candlestick', validate=False)
2548+
```
2549+
2550+
Example 2: Add text and annotations to the candlestick chart
2551+
```
2552+
fig = FF.create_candlestick(df.Open, df.High, df.Low, df.Close, dates=df.index)
2553+
# Update the fig - all options here: https://plot.ly/python/reference/#Layout
2554+
fig['layout'].update({
2555+
'title': 'The Great Recession',
2556+
'yaxis': {'title': 'AAPL Stock'},
2557+
'shapes': [{
2558+
'x0': '2007-12-01', 'x1': '2007-12-01',
2559+
'y0': 0, 'y1': 1, 'xref': 'x', 'yref': 'paper',
2560+
'line': {'color': 'rgb(30,30,30)', 'width': 1}
2561+
}],
2562+
'annotations': [{
2563+
'x': '2007-12-01', 'y': 0.05, 'xref': 'x', 'yref': 'paper',
2564+
'showarrow': False, 'xanchor': 'left',
2565+
'text': 'Official start of the recession'
2566+
}]
2567+
})
2568+
py.plot(fig, filename='finance/aapl-recession-candlestick', validate=False)
26192569
```
26202570
2621-
Example 2: Plot candlestick chart with dates and change trace colors
2571+
Example 3: Customize the candlestick colors
26222572
```
2573+
import plotly.plotly as py
2574+
from plotly.tools import FigureFactory as FF
2575+
from plotly.graph_objs import Line, Marker
26232576
from datetime import datetime
26242577
2625-
# Add Data
2626-
open_data = [33.01, 33.31, 33.50, 32.06, 34.12,
2627-
33.05, 33.31, 33.50, 32.62]
2628-
high_data = [34.20, 34.37, 33.62, 34.25, 35.18,
2629-
33.25, 35.37, 34.62, 34.25]
2630-
low_data = [31.70, 30.75, 32.87, 31.62, 30.81,
2631-
32.75, 32.75, 32.87, 32.62]
2632-
close_data = [34.10, 31.93, 33.37, 33.18, 31.18,
2633-
33.10, 32.93, 33.70, 33.18]
2634-
2635-
x = [datetime(year=2013, month=3, day=4),
2636-
datetime(year=2013, month=6, day=5),
2637-
datetime(year=2013, month=9, day=6),
2638-
datetime(year=2013, month=12, day=4),
2639-
datetime(year=2014, month=3, day=5),
2640-
datetime(year=2014, month=6, day=6),
2641-
datetime(year=2014, month=9, day=4),
2642-
datetime(year=2014, month=12, day=5),
2643-
datetime(year=2015, month=3, day=6)]
2644-
2645-
c_inc = FigureFactory.create_candlestick(open_data,
2646-
high_data,
2647-
low_data,
2648-
close_data,
2649-
dates=x,
2650-
direction='increasing',
2651-
line=Line(color='rgb(204,
2652-
229,
2653-
255)',
2654-
width=4),
2655-
marker=Marker(color='rgb(204,
2656-
229,
2657-
255)')
2658-
)
2659-
2660-
c_dec = FigureFactory.create_candlestick(open_data,
2661-
high_data,
2662-
low_data,
2663-
close_data,
2664-
dates=x,
2665-
direction='decreasing',
2666-
line=Line(color='rgb(160,
2667-
160,
2668-
160)',
2669-
width=4),
2670-
marker=Marker(color='rgb(160,
2671-
160,
2672-
160)'),
2673-
)
2674-
fig = c=_inc
2675-
fig['data'].extend(c_dec['data'])
2676-
2677-
py.iplot(fig, filename='candle', validate=False, overwrite=True)
2578+
import pandas.io.data as web
2579+
2580+
df = web.DataReader("aapl", 'yahoo', datetime(2008, 1, 1), datetime(2009, 4, 1))
2581+
fig = FF.create_candlestick(df.Open, df.High, df.Low, df.Close, dates=df.index)
2582+
2583+
# Make increasing ohlc sticks and customize their color and name
2584+
fig_increasing = FF.create_candlestick(df.Open, df.High, df.Low, df.Close, dates=df.index,
2585+
direction='increasing', name='AAPL',
2586+
marker=Marker(color='rgb(150, 200, 250)'),
2587+
line=Line(color='rgb(150, 200, 250)'))
2588+
2589+
# Make decreasing ohlc sticks and customize their color and name
2590+
fig_decreasing = FF.create_candlestick(df.Open, df.High, df.Low, df.Close, dates=df.index,
2591+
direction='decreasing',
2592+
marker=Marker(color='rgb(128, 128, 128)'),
2593+
line=Line(color='rgb(128, 128, 128)'))
2594+
2595+
# Initialize the figure
2596+
fig = fig_increasing
2597+
2598+
# Add decreasing data with .extend()
2599+
fig['data'].extend(fig_decreasing['data'])
2600+
2601+
py.iplot(fig, filename='finance/aapl-candlestick-custom', validate=False)
2602+
```
2603+
2604+
Example 4: Candlestick chart with datetime objects
2605+
```
2606+
import plotly.plotly as py
2607+
from plotly.tools import FigureFactory as FF
2608+
2609+
from datetime import datetime
2610+
2611+
# Add data
2612+
open_data = [33.0, 33.3, 33.5, 33.0, 34.1]
2613+
high_data = [33.1, 33.3, 33.6, 33.2, 34.8]
2614+
low_data = [32.7, 32.7, 32.8, 32.6, 32.8]
2615+
close_data = [33.0, 32.9, 33.3, 33.1, 33.1]
2616+
dates = [datetime(year=2013, month=10, day=10),
2617+
datetime(year=2013, month=11, day=10),
2618+
datetime(year=2013, month=12, day=10),
2619+
datetime(year=2014, month=1, day=10),
2620+
datetime(year=2014, month=2, day=10)]
2621+
2622+
# Create ohlc
2623+
fig = FF.create_candlestick(open_data, high_data,
2624+
low_data, close_data, dates=dates)
2625+
2626+
py.iplot(fig, filename='finance/simple-candlestick', validate=False)
26782627
```
26792628
"""
26802629
FigureFactory.validate_ohlc(open, high, low, close, direction,

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.