使用Vectorbt绘制RSI时DOMWidget属性错误的解决求助
解决Vectorbt绘制RSI时的AttributeError错误
问题场景
在Python 3.10.9(Vectorbt支持的最新版本)环境下,使用Vectorbt库在Jupyter Notebook中绘制简单RSI指标,图表可正常生成,但运行代码时触发AttributeError: type object 'DOMWidget' has no attribute '_ipython_display_'错误。已尝试升级Plotly至最新版本,问题仍未解决。
相关代码
#nilagyan ng r para raw file dahil nababasa ng python as UNICODE pag may slash tapos U ang kasunod df = pd.read_csv(r"C:\Users\Mark\Documents\Trading\BTC-2021min\BTC-2021min.csv") #dahil ang unix code ay about sa time, cinonvert sa seconds para magkaroon ng column na date #basically nag add tayo ng COLUMN or Data Frame na pangalan ay date df["date"] = pd.to_datetime(df["unix"], unit = "s") #nanggawa ng column at index na date at time kaya nakaSET ang INDEX sa DATE df.set_index("date", inplace=True) #aggregation ng 1 minute candles into something bigger, sakin ay 15 minutes kaya 15T df = df.resample("15T").agg({ "open":"first", "high": "max", "low": "min", "close": "last", }) #this makes it go through only a specific date rather than the whole csv file, start date at end date lang df = df.loc["2021-10-01":"2021-10-31"] rsi = vbt.RSI.run(df.close, window=8) rsi.plot()
报错堆栈
AttributeError Traceback (most recent call last) File c:\Users\Mark\AppData\Local\Programs\Python\Python310\lib\site-packages\IPython\core\formatters.py:920, in IPythonDisplayFormatter.__call__(self, obj) 918 method = get_real_method(obj, self.print_method) 919 if method is not None: ---> 920 method() 921 return True File c:\Users\Mark\AppData\Local\Programs\Python\Python310\lib\site-packages\plotly\basewidget.py:741, in BaseFigureWidget._ipython_display_(self) 737 """ 738 Handle rich display of figures in ipython contexts 739 """ 740 # Override BaseFigure's display to make sure we display the widget version ---> 741 widgets.DOMWidget._ipython_display_(self) AttributeError: type object 'DOMWidget' has no attribute '_ipython_display_'
解决方法
降级ipywidgets到兼容版本
该错误源于ipywidgets 8.x+版本对DOMWidget的方法做了重构,而Plotly的basewidget仍依赖旧方法。执行以下命令降级:pip install ipywidgets==7.7.1安装完成后重启Jupyter Notebook即可。
强制使用静态渲染模式
绕开Widget渲染逻辑,直接使用Plotly的静态渲染器。在代码开头添加配置:import plotly.io as pio # 根据你使用的环境选择渲染器,Notebook用'notebook',JupyterLab用'jupyterlab' pio.renderers.default = 'notebook'或者直接在plot方法中指定渲染器:
rsi.plot(renderer='notebook')同步Vectorbt与Plotly版本
查看Vectorbt官方要求的Plotly版本范围,执行pip show vectorbt查看Requires字段,安装符合要求的Plotly版本,避免版本过高导致不兼容。例如:# 假设Vectorbt要求Plotly>=5.0,<5.15 pip install plotly==5.14.1
内容的提问来源于stack exchange,提问作者zcodec
相关产品推荐
相关产品推荐

