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

PyCharm运行VectorBT量化代码时图形无法显示问题求助

PyCharm中VectorBT图形无法显示的解决办法

核心问题原因

VectorBT依赖Plotly生成可视化图形,但Plotly在PyCharm环境下不会自动触发图形渲染,必须显式调用展示方法,同时需要确认PyCharm的图形显示配置正确。

具体修复步骤

  • 显式添加图形展示代码
    在代码末尾加上fig.show(),强制触发图形渲染:

    import vectorbt as vbt
    from datetime import datetime, timedelta
    import pytz
    
    start_date = datetime(2021, 11, 30, tzinfo=pytz.utc)  # time period for analysis, must be timezone-aware
    end_date = datetime(2022, 8, 19, tzinfo=pytz.utc)
    price = vbt.YFData.download('BBAS3.SA',start= start_date, end=end_date).get('Close')
    
    fast_ma = vbt.MA.run(price, 7)
    slow_ma = vbt.MA.run(price, 21)
    entries = fast_ma.ma_crossed_above(slow_ma)
    exits = fast_ma.ma_crossed_below(slow_ma)
    
    pf1 = vbt.Portfolio.from_signals(price, entries, exits, init_cash=1000,fees=0)
    pf1.orders.records_readable
    
    fig = price.vbt.plot(trace_kwargs=dict(name='Close'))
    fast_ma.ma.vbt.plot(trace_kwargs=dict(name='Fast MA'), fig=fig)
    slow_ma.ma.vbt.plot(trace_kwargs=dict(name='Slow MA'), fig=fig)
    pf1.positions.plot(close_trace_kwargs=dict(visible=True), fig=fig)
    # 新增这一行触发图形显示
    fig.show()
    
  • 检查PyCharm图形渲染配置

    1. 打开File > Settings > Tools > Python Scientific
    2. 若想在PyCharm内置工具窗口显示图形,勾选Show plots in tool window;若要独立窗口显示,取消该选项
    3. 重启PyCharm生效配置
  • 更新依赖库版本
    确保VectorBT和Plotly为最新版本,避免兼容问题:

    pip install --upgrade vectorbt plotly
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.20 22:24:27