Ploly烛台的定制颜色

问题描述

可以为烛台定义自定义颜色吗?我们希望根据我们自己的业务规则为它们上色,而不是默认应用的递增规则和递减规则。


解决方案

要直接回答您的初始问题,wrt:

是否可以为烛台定义自定义颜色?

从你到目前为止提供的信息,以及缺乏数据和代码,我只能将你的问题解释为:

如何在打开和关闭之间为不同的烛台大小或范围应用不同的颜色。

这张图为极端的向上和向下移动设置了不同的阈值:

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

然后,从不被认为是极端数据的数据中设置跟踪(&q;base";):

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()

相关文章