Plotly 烛台的自定义颜色

Custom color of Plotly candlesticks

是否可以为烛台定义自定义颜色?我们想根据我们自己的业务规则为它们着色,而不是 Plotly 默认应用的“增加”和“减少”规则。

直接回答你最初的问题:

Is it possible to define custom color for candlesticks?

根据您目前提供的信息,由于缺少数据和代码,我只能将您的问题解释为:

How to apply different colors to different candlestick sizes or ranges between open and close.

这是一个图表,它为被认为是极端的向上和向下运动设置了不同的阈值:

df['change'] = df['AAPL.Close'] - df['AAPL.Open']
df_hi = df[df['change']>1.5]
df_lo = df[df['change']<-0.3]

然后,根据被认为极端的数据设置“基础”轨迹:

fig = go.Figure(go.Candlestick(x=df['Date'],
                open=df['AAPL.Open'], high=df['AAPL.High'],
                low=df['AAPL.Low'], close=df['AAPL.Close']))

然后添加两个额外的轨迹以包括被认为极端的运动:

# set up trace with extreme highs
fig.add_traces(go.Candlestick(x=df_hi['Date'],
                open=df_hi['AAPL.Open'], high=df_hi['AAPL.High'],
                low=df_hi['AAPL.Low'], close=df_hi['AAPL.Close']))

如果这正是您要寻找的,我将提供有关如何设置颜色的进一步解释。使用此设置,您还可以通过图例对不同的迹线进行子集化。

剧情:

完整代码:

import plotly.graph_objects as go
from plotly.subplots import make_subplots
import pandas as pd

# data
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv')
df=df.tail(15)

df['change'] = df['AAPL.Close'] - df['AAPL.Open']
df_hi = df[df['change']>1.5]
df_lo = df[df['change']<-0.3]

not_hi = df[df.index.isin(df_hi.index)].index
not_lo = df[df.index.isin(df_lo.index)].index
df = df.drop(not_hi)
df = df.drop(not_lo)

# set up figure with values not high and not low
# include candlestick with rangeselector
fig = go.Figure(go.Candlestick(x=df['Date'],
                open=df['AAPL.Open'], high=df['AAPL.High'],
                low=df['AAPL.Low'], close=df['AAPL.Close']))

# set up trace with extreme highs
fig.add_traces(go.Candlestick(x=df_hi['Date'],
                open=df_hi['AAPL.Open'], high=df_hi['AAPL.High'],
                low=df_hi['AAPL.Low'], close=df_hi['AAPL.Close']))

# set up traces with extreme lows
fig.add_traces(go.Candlestick(x=df_lo['Date'],
                open=df_lo['AAPL.Open'], high=df_lo['AAPL.High'],
                low=df_lo['AAPL.Low'], close=df_lo['AAPL.Close']))


color_hi_fill = 'black'
color_hi_line = 'blue'

color_lo_fill = 'yellow'
color_lo_line = 'purple'

fig.data[1].increasing.fillcolor = color_hi_fill
fig.data[1].increasing.line.color = color_hi_line
fig.data[1].decreasing.fillcolor = 'rgba(0,0,0,0)'
fig.data[1].decreasing.line.color = 'rgba(0,0,0,0)'

fig.data[2].increasing.fillcolor = 'rgba(0,0,0,0)'
fig.data[2].increasing.line.color = 'rgba(0,0,0,0)'
fig.data[2].decreasing.fillcolor = color_lo_fill
fig.data[2].decreasing.line.color = color_lo_line

fig.show()