当前位置: 首页 > article >正文

Backtrader绘图cerebro.plot报错问题的处理

Backtrader绘图cerebro.plot报错问题的处理

1.问题描述

在jupyter 中使用BackTrader ,使用绘图功能时:

cerebro.plot()

提示错误:ValueError: Axis limits cannot be NaN or Inf

由于backtrader 要求有7列数据,最后一列openinterest ,以为缺少此列数据,补齐最后一列。

data[‘openinterest’] = 0

数据显示效果如下:

                open      high       low     close        vol  openinterest
date                                                                       
2020-01-02  119.3010  124.4880  119.3010  123.7272  184419.14             0
2020-01-03  123.3814  125.3871  121.6524  124.6263  140995.19             0
2020-01-06  125.1796  135.8302  123.5198  133.6171  425339.29             0
2020-01-07  133.8246  137.6284  131.9573  133.8938  250923.45             0
2020-01-08  135.0695  141.9855  133.3405  138.8041  412035.84             0

数据上肯定没有0和无穷大值,问题依然存在。

2. 问题处理过程

(1)安装组件 ipympl

Jupyter Lab 通过 jupyter-matplotlib extension 支持交互式 matplotlib 。由于ipympl Jupyter Lab版本需要NodeJS 。

pip install ipympl

安装过程中,可以看到安装了Nodejs 。

# pip install ipympl
Looking in indexes: https://pypi.douban.com/simple/
Collecting ipympl
  Downloading https://mirrors.cloud.tencent.com/pypi/packages/08/5b/83a88a44e5dd185104c400e0ed6cdba50c776a8f494d525298bb09db2e5c/ipympl-0.9.3-py2.py3-none-any.whl (511 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 511.6/511.6 kB 1.0 MB/s eta 0:00:00
Collecting ipywidgets<9,>=7.6.0
  Downloading https://mirrors.cloud.tencent.com/pypi/packages/4a/0e/57ed498fafbc60419a9332d872e929879ceba2d73cb11d284d7112472b3e/ipywidgets-8.1.1-py3-none-any.whl (139 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 139.4/139.4 kB 1.0 MB/s eta 0:00:00
Requirement already satisfied: pillow in /root/anaconda3/envs/nlp/lib/python3.8/site-packages (from ipympl) (9.5.0)
Requirement already satisfied: traitlets<6 in /root/anaconda3/envs/nlp/lib/python3.8/site-packages (from ipympl) (5.9.0)
Requirement already satisfied: matplotlib<4,>=3.4.0 in /root/anaconda3/envs/nlp/lib/python3.8/site-packages (from ipympl) (3.7.1)
Requirement already satisfied: numpy in /root/anaconda3/envs/nlp/lib/python3.8/site-packages (from ipympl) (1.23.5)
Requirement already satisfied: ipython-genutils in /root/anaconda3/envs/nlp/lib/python3.8/site-packages (from ipympl) (0.2.0)
Requirement already satisfied: ipython<9 in /root/anaconda3/envs/nlp/lib/python3.8/site-packages (from ipympl) (8.12.0)
Requirement already satisfied: stack-data in /root/anaconda3/envs/nlp/lib/python3.8/site-packages (from ipython<9->ipympl) (0.6.2)
Requirement already satisfied: pickleshare in /root/anaconda3/envs/nlp/lib/python3.8/site-packages (from ipython<9->ipympl) (0.7.5)
Requirement already satisfied: pygments>=2.4.0 in /root/anaconda3/envs/nlp/lib/python3.8/site-packages (from ipython<9->ipympl) (2.15.1)
Requirement already satisfied: pexpect>4.3 in /root/anaconda3/envs/nlp/lib/python3.8/site-packages (from ipython<9->ipympl) (4.8.0)
Requirement already satisfied: typing-extensions in /root/anaconda3/envs/nlp/lib/python3.8/site-packages (from ipython<9->ipympl) (4.5.0)
Requirement already satisfied: prompt-toolkit!=3.0.37,<3.1.0,>=3.0.30 in /root/anaconda3/envs/nlp/lib/python3.8/site-packages (from ipython<9->ipympl) (3.0.38)
Requirement already satisfied: decorator in /root/anaconda3/envs/nlp/lib/python3.8/site-packages (from ipython<9->ipympl) (5.1.1)
Requirement already satisfied: backcall in /root/anaconda3/envs/nlp/lib/python3.8/site-packages (from ipython<9->ipympl) (0.2.0)
Requirement already satisfied: jedi>=0.16 in /root/anaconda3/envs/nlp/lib/python3.8/site-packages (from ipython<9->ipympl) (0.18.2)
Requirement already satisfied: matplotlib-inline in /root/anaconda3/envs/nlp/lib/python3.8/site-packages (from ipython<9->ipympl) (0.1.6)
Requirement already satisfied: comm>=0.1.3 in /root/anaconda3/envs/nlp/lib/python3.8/site-packages (from ipywidgets<9,>=7.6.0->ipympl) (0.1.3)
Collecting jupyterlab-widgets~=3.0.9
  Downloading https://mirrors.cloud.tencent.com/pypi/packages/e8/05/0ebab152288693b5ec7b339aab857362947031143b282853b4c2dd4b5b40/jupyterlab_widgets-3.0.9-py3-none-any.whl (214 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 214.9/214.9 kB 1.0 MB/s eta 0:00:00
Collecting widgetsnbextension~=4.0.9
  Downloading https://mirrors.cloud.tencent.com/pypi/packages/29/03/107d96077c4befed191f7ad1a12c7b52a8f9d2778a5836d59f9855c105f6/widgetsnbextension-4.0.9-py3-none-any.whl (2.3 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 2.3/2.3 MB 1.0 MB/s eta 0:00:00
Requirement already satisfied: cycler>=0.10 in /root/anaconda3/envs/nlp/lib/python3.8/site-packages (from matplotlib<4,>=3.4.0->ipympl) (0.11.0)
Requirement already satisfied: packaging>=20.0 in /root/anaconda3/envs/nlp/lib/python3.8/site-packages (from matplotlib<4,>=3.4.0->ipympl) (23.1)
Requirement already satisfied: python-dateutil>=2.7 in /root/anaconda3/envs/nlp/lib/python3.8/site-packages (from matplotlib<4,>=3.4.0->ipympl) (2.8.2)
Requirement already satisfied: contourpy>=1.0.1 in /root/anaconda3/envs/nlp/lib/python3.8/site-packages (from matplotlib<4,>=3.4.0->ipympl) (1.0.7)
Requirement already satisfied: fonttools>=4.22.0 in /root/anaconda3/envs/nlp/lib/python3.8/site-packages (from matplotlib<4,>=3.4.0->ipympl) (4.39.3)
Requirement already satisfied: importlib-resources>=3.2.0 in /root/anaconda3/envs/nlp/lib/python3.8/site-packages (from matplotlib<4,>=3.4.0->ipympl) (5.12.0)
Requirement already satisfied: pyparsing>=2.3.1 in /root/anaconda3/envs/nlp/lib/python3.8/site-packages (from matplotlib<4,>=3.4.0->ipympl) (3.0.9)
Requirement already satisfied: kiwisolver>=1.0.1 in /root/anaconda3/envs/nlp/lib/python3.8/site-packages (from matplotlib<4,>=3.4.0->ipympl) (1.4.4)
Requirement already satisfied: zipp>=3.1.0 in /root/anaconda3/envs/nlp/lib/python3.8/site-packages (from importlib-resources>=3.2.0->matplotlib<4,>=3.4.0->ipympl) (3.15.0)
Requirement already satisfied: parso<0.9.0,>=0.8.0 in /root/anaconda3/envs/nlp/lib/python3.8/site-packages (from jedi>=0.16->ipython<9->ipympl) (0.8.3)
Requirement already satisfied: ptyprocess>=0.5 in /root/anaconda3/envs/nlp/lib/python3.8/site-packages (from pexpect>4.3->ipython<9->ipympl) (0.7.0)
Requirement already satisfied: wcwidth in /root/anaconda3/envs/nlp/lib/python3.8/site-packages (from prompt-toolkit!=3.0.37,<3.1.0,>=3.0.30->ipython<9->ipympl) (0.2.6)
Requirement already satisfied: six>=1.5 in /root/anaconda3/envs/nlp/lib/python3.8/site-packages (from python-dateutil>=2.7->matplotlib<4,>=3.4.0->ipympl) (1.16.0)
Requirement already satisfied: pure-eval in /root/anaconda3/envs/nlp/lib/python3.8/site-packages (from stack-data->ipython<9->ipympl) (0.2.2)
Requirement already satisfied: asttokens>=2.1.0 in /root/anaconda3/envs/nlp/lib/python3.8/site-packages (from stack-data->ipython<9->ipympl) (2.2.1)
Requirement already satisfied: executing>=1.2.0 in /root/anaconda3/envs/nlp/lib/python3.8/site-packages (from stack-data->ipython<9->ipympl) (1.2.0)
Installing collected packages: widgetsnbextension, jupyterlab-widgets, ipywidgets, ipympl
Successfully installed ipympl-0.9.3 ipywidgets-8.1.1 jupyterlab-widgets-3.0.9 widgetsnbextension-4.0.9
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
[root@data /]# pip install nodejs-bin
Looking in indexes: https://pypi.douban.com/simple/
Collecting nodejs-bin
  Downloading https://mirrors.cloud.tencent.com/pypi/packages/14/f5/b85f10ddb2b6bf58395bd08a7794ded91518f7eca1dc771c22c808c44e81/nodejs_bin-18.4.0a4-py3-none-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (47.3 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 47.3/47.3 MB 866.4 kB/s eta 0:00:00
Installing collected packages: nodejs-bin
Successfully installed nodejs-bin-18.4.0a4
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv

(2)出现 Javascript Error: IPython is not defined

使用以下命令绘图,还是失败

cerebro.plot()
cerebro.plot(iplot=False)

用两种方式绘图,都是报 Javascript Error: IPython is not defined 错误。
matplotlib 的版本是 3.7.1 ,网上建议是 3.2.2,与BackTrader 兼容,不建议更高版本。

 #pip list | grep matplot
matplotlib                     3.7.1
matplotlib-inline              0.1.6

卸载matplotlib ,重新安装3.2.2版本:

pip uninstall matplotlib
pip install matplotlib==3.2.2

重启jupyter 。
使用 matplotlib inline 组件,绘图加参数 iplot=False

%matplotlib inline
cerebro.plot(iplot=False)

终于可以在jupyter 中BackTrader 绘制收益图示,贴张图纪念一下:

在这里插入图片描述


http://www.kler.cn/a/135197.html

相关文章:

  • 【安全通信】告别信息泄露:搭建你的开源视频聊天系统briefing
  • 字节跳动Android面试题汇总及参考答案(80+面试题,持续更新)
  • Elasticsearch 实战应用:高效搜索与数据分析
  • odoo 17 后端路由接口认证自定义
  • 蔚来Java面试题及参考答案
  • Android OpenGL ES详解——立方体贴图
  • 美国DDoS服务器:如何保护你的网站免遭攻击?
  • 优化资源利用率:kubernetes中装箱的好处与挑战
  • Scala---WordCount
  • 著名的勃艮第葡萄酒是如何分类的?
  • springboot中动态api如何设置
  • redission源码解读
  • HIS系统源码,云HIS源码,二级医院信息管理系统源码,预约挂号支持、病患问诊、电子病历、开药发药、会员管理、统计查询、医生工作站、护士工作站
  • Golang Context 的使用指南
  • String字符串性能优化的几种方案
  • QT专栏1 -Qt安装教程
  • 21、ila
  • Sentinel入门
  • 基于STC12C5A60S2系列1T 8051单片的模数芯片ADC0809实现模数转换应用
  • 2、LeetCode之两数相加
  • 为什么我们在Springmvc拦截器的时候要加判断 handler instanceof HandlerMethod
  • Google Chrome 任意文件读取 (CVE-2023-4357)漏洞
  • Transformer中WordPiece/BPE等不同编码方式详解以及优缺点
  • uvm环境获取系统时间的方法和使用案例
  • Python爬虫批量下载图片
  • Kafka快速入门