Plotly 烛台自定义颜色

Plotly candlesticks customize color

我在这里找到了相同的主题和解决方案

但是,使用该解决方案,我的输出图根据时间戳分为“三个区域”。例如,我得到一个图表,第一个范围包括从上午 9 点到下午 14:29 的烛台,所有烛台都有相同的红色;第二个范围包括从上午 9 点到下午 14:29 的烛台,所有烛台都具有相同的绿色;等等。

但是我需要在上午 9 点到下午 14:29 的一个时间范围内所有混合颜色的烛台。

data = pd.DataFrame(master.intradata)
intradata_sell = data[data['oi'] == 1]
intradata_buy = data[data['oi'] == 2]
intradata_und = data[data['oi'] == 3] 

fig = go.Figure(go.Candlestick(x=intradata_sell['time'],
                open=intradata_sell['open'], high=intradata_sell['high'],
                low=intradata_sell['low'], close=intradata_sell['close']))

fig.add_traces(go.Candlestick(x=intradata_buy['time'],
            open=intradata_buy['open'], high=intradata_buy['high'],
            low=intradata_buy['low'], close=intradata_buy['close']))

fig.add_traces(go.Candlestick(x=intradata_und['time'],
            open=intradata_und['open'], high=intradata_und['high'],
            low=intradata_und['low'], close=intradata_und['close']))

fig.data[0].increasing.fillcolor = 'red'
fig.data[0].increasing.line.color = 'red'
fig.data[0].decreasing.fillcolor = 'red'
fig.data[0].decreasing.line.color = 'red'

fig.data[1].increasing.fillcolor = 'green'
fig.data[1].increasing.line.color = 'green'
fig.data[1].decreasing.fillcolor = 'green'
fig.data[1].decreasing.line.color = 'green'

fig.data[2].increasing.fillcolor = 'orange'
fig.data[2].increasing.line.color = 'orange'
fig.data[2].decreasing.fillcolor = 'orange'
fig.data[2].decreasing.line.color = 'orange'


fig.show()

.

  • 有模拟数据才能使用你的剧情代码
  • 为您的代码添加了额外的步骤
# ensure first dataframe has all times so additional traces match up on xaxis
intradata_sell = (
    intradata_sell.merge(intradata_buy["time"], on="time", how="outer")
    .merge(intradata_und["time"], on="time", how="outer")
    .sort_values("time")
)
  • 根据评论 - 在第一条轨迹中需要所有 xaxis 值,因此额外的轨迹不会附加到 xaxis
  • 的末尾
  • 删除此行并获得像您的图像一样的图
import plotly.graph_objects as go
from dataclasses import dataclass
import datetime as dt
import random
import numpy as np
import pandas as pd

@dataclass
class master:
    intradata: None

# simulate data....
S = 50
df = pd.DataFrame(
    {
        "time": [
            dt.time(random.randint(9, 14), min(random.randint(0, 12)*5,59)) for i in range(S)
        ],
        "oi": np.random.randint(1, 4, S),
        "value": np.random.uniform(1, 5, S) * np.random.uniform(0.1, 1, S),
    }
).groupby(["time", "oi"]).ohlc().droplevel(0, 1).reset_index()

master = master(intradata=df.to_dict("list"))

data = pd.DataFrame(master.intradata)
intradata_sell = data[data['oi'] == 1]
intradata_buy = data[data['oi'] == 2]
intradata_und = data[data['oi'] == 3] 

# ensure first dataframe has all times so additional traces match up on xaxis
intradata_sell = (
    intradata_sell.merge(intradata_buy["time"], on="time", how="outer")
    .merge(intradata_und["time"], on="time", how="outer")
    .sort_values("time")
)

fig = go.Figure(go.Candlestick(x=intradata_sell['time'],
                open=intradata_sell['open'], high=intradata_sell['high'],
                low=intradata_sell['low'], close=intradata_sell['close']))

fig.add_traces(go.Candlestick(x=intradata_buy['time'],
            open=intradata_buy['open'], high=intradata_buy['high'],
            low=intradata_buy['low'], close=intradata_buy['close']))

fig.add_traces(go.Candlestick(x=intradata_und['time'],
            open=intradata_und['open'], high=intradata_und['high'],
            low=intradata_und['low'], close=intradata_und['close']))

fig.data[0].increasing.fillcolor = 'red'
fig.data[0].increasing.line.color = 'red'
fig.data[0].decreasing.fillcolor = 'red'
fig.data[0].decreasing.line.color = 'red'

fig.data[1].increasing.fillcolor = 'green'
fig.data[1].increasing.line.color = 'green'
fig.data[1].decreasing.fillcolor = 'green'
fig.data[1].decreasing.line.color = 'green'

fig.data[2].increasing.fillcolor = 'orange'
fig.data[2].increasing.line.color = 'orange'
fig.data[2].decreasing.fillcolor = 'orange'
fig.data[2].decreasing.line.color = 'orange'


fig.show()