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

股票K线图下方成交量柱状图宽度异常、柱体粘连问题求助

股票K线图成交量柱体粘连问题排查与解决

问题描述

绘制带下方成交量图的K线图时,部分成交量柱体出现随机粘连现象。已尝试调整fig_size、修改width参数、使用.values替代DataFrame数据,但问题仍未解决。

问题截图

问题截图

相关代码

bar_width = 0.8
stick_width = 0.1
fig_size = (10, 1)
line_width = 2

width = [0.8]*104

def plotstock(df: pd.DataFrame, output_plot_dir: str):
    df = df.reset_index()
    up = df[df.close >= df.open]
    down = df[df.close < df.open]

    plt.figure(figsize=fig_size, dpi=80)
    plt.style.use('default')
    figure, axis = plt.subplots(2, height_ratios=[3,1])
    axis[0].margins(x=0.0, y=0.1)
    axis[1].margins(x=0.0, y=0.1)

    # Draw the price history
    axis[0].bar(up.index, up.close-up.open, bar_width, bottom=up.open, color=bull_color) 
    axis[0].bar(up.index, up.high-up.close, stick_width, bottom=up.close, color=bull_color) 
    axis[0].bar(up.index, up.low-up.open, stick_width, bottom=up.open, color=bull_color)

    axis[0].bar(down.index, down.close-down.open, bar_width, bottom=down.open, color=bear_color) 
    axis[0].bar(down.index, down.high-down.open, stick_width, bottom=down.open, color=bear_color) 
    axis[0].bar(down.index, down.low-down.close, stick_width, bottom=down.close, color=bear_color)

    axis[0].plot(df.wma30, color=wma30_color, linewidth=line_width)
    axis[0].axhline(df.iloc[-1].fyh, color=fyh_color, linewidth=line_width)

    axis[0].axes.get_xaxis().set_visible(False)
    axis[0].axes.get_yaxis().set_visible(False)

    # Draw the volume
    # print(df["volume"].values)
    axis[1].bar(df.index, df["volume"].values, width=width)
    
    axis[1].axes.get_xaxis().set_visible(False)
    axis[1].axes.get_yaxis().set_visible(False)

问题根源与解决方案

1. 重复创建Figure导致尺寸设置失效

代码中先调用plt.figure(figsize=fig_size)创建了一个Figure,随后又通过plt.subplots()创建新的Figure,导致fig_size参数并未应用到实际绘图的Figure上,最终图表宽度不足,柱体被挤压粘连。

修复:
移除多余的plt.figure()调用,在plt.subplots()中直接指定尺寸和dpi:

plt.style.use('default')
figure, axis = plt.subplots(2, height_ratios=[3,1], figsize=fig_size, dpi=80)

2. 固定长度的width列表适配问题

定义了固定长度为104的width列表,但如果传入的DataFrame行数不等于104,Matplotlib会循环复用该列表中的宽度值,可能导致部分柱体宽度异常,或因长度不匹配引发渲染问题。

修复:
将width改为标量值,统一所有成交量柱体的宽度:

axis[1].bar(df.index, df["volume"].values, width=0.8)

3. 子图X轴未对齐

K线图和成交量图的X轴范围可能因渲染差异出现微小偏差,导致视觉上柱体粘连。

修复:
强制两个子图的X轴范围一致:

axis[0].set_xlim(axis[1].get_xlim())

最终修改后的核心代码片段

bar_width = 0.8
stick_width = 0.1
fig_size = (10, 1)
line_width = 2

def plotstock(df: pd.DataFrame, output_plot_dir: str):
    df = df.reset_index()
    up = df[df.close >= df.open]
    down = df[df.close < df.open]

    plt.style.use('default')
    figure, axis = plt.subplots(2, height_ratios=[3,1], figsize=fig_size, dpi=80)
    axis[0].margins(x=0.0, y=0.1)
    axis[1].margins(x=0.0, y=0.1)

    # 绘制K线代码保持不变...

    # 绘制成交量
    axis[1].bar(df.index, df["volume"].values, width=0.8)
    
    # 对齐X轴范围
    axis[0].set_xlim(axis[1].get_xlim())

    # 隐藏坐标轴代码保持不变...

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.28 20:55:56