Python中的 plotly.figure_factory.create_candlestick()函数
Python的 Plotly 库对于数据可视化和简单轻松地理解数据非常有用。
plotly.figure_factory.create_candlestick
烛台图是一种金融图表风格,用于描述给定 x 坐标(最可能的时间)的 o。方框代表开盘价和收盘价之间的价差,线条代表低值和高值之间的价差。
Syntax: plotly.figure_factory.create_candlestick(open, high, low, close, dates=None, direction=’both’, **kwargs)
Parameters
open: it is used for opening values
high: it is used for high values
low: it is used for low values
close: it is used for closing values
dates: it is used for list of datetime objects. Default: None
direction: it can be used for both increasing and decreasing. When the direction is ‘increasing’, the returned figure consists of all candlesticks where the close value is greater than the corresponding open value, and when the direction is ‘decreasing’, the returned figure consists of all candlesticks where the close value is less than or equal to the corresponding open value. When the direction is ‘both’, both increasing and decreasing candlesticks are returned. Default: ‘both’
kwargs – it describe other attributes about the ohlc Scatter trace such as the color or the legend name. For more information on valid kwargs call help(plotly.graph_objects.Scatter)
示例 1 :使用日期时间对象
Python3
import plotly.graph_objects 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)]
fig = go.Figure(data=[go.Candlestick(x=dates,
open=open_data, high=high_data,
low=low_data, close=close_data)])
fig.show()
Python3
from plotly.figure_factory import create_candlestick
from datetime import datetime
# Add data
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)]
# Create ohlc
fig = create_candlestick(open_data, high_data,
low_data, close_data, dates=dates)
fig.show()
输出:
示例 2:带有日期时间对象的烛台图
Python3
from plotly.figure_factory import create_candlestick
from datetime import datetime
# Add data
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)]
# Create ohlc
fig = create_candlestick(open_data, high_data,
low_data, close_data, dates=dates)
fig.show()
输出: