You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何自定义Plotly candlesticks透明度 使其填充色与边缘颜色一致

Plotly 蜡烛图填充与边缘颜色一致的实现方案

完全可以实现该效果,核心是手动指定蜡烛图涨跌状态下的填充色与描边色,保持二者参数完全一致即可。

  • 实现逻辑:Plotly 蜡烛图默认填充色自带透明度,会比边缘颜色浅,只要手动覆写increasing(上涨)、decreasing(下跌)配置项的填充和描边颜色参数,就能避免非预期的半透明效果。
  • 参考代码示例(Python 版本):
import plotly.graph_objects as go
import pandas as pd

# 示例K线数据,可替换为自己的数据集
kline_data = pd.DataFrame({
    "open": [20, 22, 21, 25, 23],
    "high": [24, 23, 26, 27, 26],
    "low": [19, 20, 20, 23, 22],
    "close": [22, 21, 25, 23, 26]
})

fig = go.Figure(data=[go.Candlestick(
    x=kline_data.index,
    open=kline_data["open"],
    high=kline_data["high"],
    low=kline_data["low"],
    close=kline_data["close"],
    # 上涨蜡烛配置:填充和描边都用红色
    increasing=dict(
        fillcolor="#ff3b30",
        line=dict(color="#ff3b30", width=1)
    ),
    # 下跌蜡烛配置:填充和描边都用绿色
    decreasing=dict(
        fillcolor="#34c759",
        line=dict(color="#34c759", width=1)
    )
)])

# 可选:关闭底部滑块,优化展示效果
fig.update_layout(xaxis_rangeslider_visible=False)
fig.show()
  • 如果你需要自定义整体不透明度,可以将颜色值替换为RGBA格式,比如半透红色写为rgba(255, 59, 48, 0.6),只要填充和描边的颜色参数完全相同,就能保证二者视觉效果一致。

内容的提问来源于stack exchange,提问作者Ferreroprod

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.24 14:45:03