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

news2024/11/16 19:49:09

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
(nlp) [root@bigdata Thread231031]# 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.coloradmin.cn/o/1227025.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Halcon (5):Halcon Solution Guide I basics 导论解析

文章目录 文章专栏前言文章目录翻译文档的说明 结论 文章专栏 Halcon开发 前言 今天开始看Halcon的官方文档。由于市面上的教学主要是以基础的语法&#xff0c;算子简单介绍为主。所以我还是得看官方的文本。别的不多说了。有道词英语词典&#xff0c;启动。 还有就是今天LOL…

Python基础:错误和异常

在Python中的错误可&#xff08;至少&#xff09;被分为两种&#xff1a;语法错误和 异常&#xff0c;均是指在程序中发生的问题和意外情况。Python提供了异常处理机制&#xff0c;使程序能够更容易地应对这些问题。 1. 语法错误&#xff08;Syntax Error&#xff09; 语法错误…

C++算法入门练习——树的带权路径长度

现有一棵n个结点的树&#xff08;结点编号为从0到n-1&#xff0c;根结点为0号结点&#xff09;&#xff0c;每个结点有各自的权值w。 结点的路径长度是指&#xff0c;从根结点到该结点的边数&#xff1b;结点的带权路径长度是指&#xff0c;结点权值乘以结点的路径长度&#x…

前端必备工具

一、百度Ai功能合集 https://chat.baidu.com/ 二、超级复制 这是一个edge插件&#xff0c;那些禁止复制的文本&#xff0c;用这个插件点一下图标&#xff0c;会把当前网页解除禁止复制。 三、集中工具箱 例如一键抠图&#xff0c;感觉还可以&#xff1a; 四、代码漂亮截图 几…

七、文件包含漏洞

一、文件包含漏洞 解释&#xff1a;文件包含漏洞是一种注入型漏洞&#xff0c;其本质就是输入一段用户能够控制的脚本或者代码&#xff0c;并让服务端执行&#xff1b;其还能够使得服务器上的源代码被读取&#xff0c;在PHP里面我们把可重复使用的函数写入到单个文件中&#x…

三菱PLC 模拟量输出模拟量转换功能块S_RTI(内嵌ST)

模拟量输出模拟量转换功能块S_RTI算法原理和公式介绍请参考下面文章链接: PLC模拟量输出 模拟量转换FC S_RTI-CSDN博客文章浏览阅读5.3k次,点赞2次,收藏11次。1、本文主要展示西门子博途模拟量输出转换的几种方法, 方法1:先展示下自编FC:计算公式如下:intput intput Rea…

linux 定时执行脚本

先写一个简单的shell脚本用来测试定时执行脚本 [rootVM-12-12-centos wz]# cat shell_cron_test.sh #!/bin/bashif [ -f "/home/wz/cron_test.txt" ];thennum$(($(wc -l /home/wz/cron_test.txt | cut -d -f 1)1))elsenum1 fi echo "$(date "%y-%m-%d …

在线代码调试运行微信开放平台官方接口调试校验工具大全

具体前往&#xff1a;在线代码调试&API校验工具大全

【实用技巧】更改ArduinoIDE默认库文件位置,解放系统盘,将Arduino15中的库文件移动到其他磁盘

本文主要介绍更改Arduino IDE &#xff08;含2.0以上版本&#xff09;默认库文件位置的方法。 原创文章&#xff0c;转载请注明出处&#xff1a; 【实用技巧】更改ArduinoIDE默认库文件位置&#xff0c;解放C盘&#xff0c;将Arduino15中的库文件移动到其他磁盘-CSDN博客文章浏…

JavaScript职责链模式

JavaScript职责链模式 1 什么是职责链模式2 举个例子3 用职责链模式重构代码4 灵活可拆分的职责链节点5 异步的职责链 1 什么是职责链模式 职责链模式是一种行为型设计模式&#xff0c;它允许将请求沿着处理者链进行传递&#xff0c;直到其中一个处理者能够处理该请求为止&…

Flutter笔记:缩放手势

Flutter笔记 缩放手势 作者&#xff1a;李俊才 &#xff08;jcLee95&#xff09;&#xff1a;https://blog.csdn.net/qq_28550263 邮箱 &#xff1a;291148484163.com 本文地址&#xff1a;https://blog.csdn.net/qq_28550263/article/details/134485138 目 录 1. 概述2. 缩放手…

QGroundControl源码编译的三种方法

1.使用QtCreator编译: 下载qgroundcontrol源码 https://github.com/mavlink/qgroundcontrol.git 克隆 同步子模块 使用打开qgroundcontrol.pro 打开前要求先安装qt 5.15.2

【神印王座】月夜大尺度诱惑,皓晨潜入月魔宫,枫秀降临男扮女装

Hello,小伙伴们&#xff0c;我是拾荒君。 为了能安全回到联盟&#xff0c;龙皓晨决定让月夜商队护送他们&#xff0c;这也是他们目前处境更快更安全回到人类境地的方法。于是&#xff0c;龙皓晨只身一人去寻找月夜&#xff0c;此次执行的任务完全超出龙皓晨的掌握之外&#xf…

数据结构与算法-图

图 &#x1f388;2.图的存储结构&#x1f4d6;2.4.2邻接表的存储✅2.4.2.1逆邻接表✅2.4.2.2邻接表存储结构的定义✅2.4.2.3邻接表存储结构的类定义✅2.4.2.4创建n个顶点m条边的无向网✅2.4.2.5创建n个顶点m条边的有向网✅2.4.2.6定位操作-查找定点信息在顶点数组中的下标✅2.4…

操作系统:操作系统教程第六版(骆斌、葛季栋、费翔林)习题一计算机操作系统概述

目录 前言1. 思考题2. 应用题 前言 本系列文章是针对操作系统教程第六版&#xff08;骆斌、葛季栋、费翔林&#xff09;的习题解答&#xff0c;其中简答题部分为博主自己搜索整理的&#xff0c;错漏之处在所难免。应用题部分有答案为依据。 1. 思考题 &#xff08;1&#xf…

算数通关村第十一关-白银挑战位运算高频题

位移的运算 位1的个数 描述 : 编写一个函数&#xff0c;输入是一个无符号整数&#xff08;以二进制串的形式&#xff09;&#xff0c;返回其二进制表达式中数字位数为 1 的个数&#xff08;也被称为汉明重量&#xff09;。 题目 : LeetCode 191.位1的个数 191. 位1的个数…

pr出现由于找不到msvcp110.dll,无法继续执行代码怎么办?如何修复

为什么我们在打开运行电脑软件会出现msvcr110.dll无法继续执行此代码的问题呢&#xff1f;因为msvcr110.dll是Microsoft Visual C Redistributable Package for Visual Studio 2013的一个动态链接库。它是一个重要的组件&#xff0c;用于帮助游戏和软件运行。如果某个程序是用它…

程序的编译链接以及装载

目录 一、预处理 二、编译 三、汇编 四、链接 五、装载 一、预处理 读取c源程序&#xff0c;对其中的伪指令&#xff08;以#开头的指令&#xff09;和特殊符号进行处理&#xff0c; 伪指令主要包括以下五个方面&#xff1a; 宏定义指令&#xff0c;如#define Name Token…

BUUCTF [BJDCTF2020]你猜我是个啥 1

BUUCTF:https://buuoj.cn/challenges 题目描述&#xff1a; 来源&#xff1a;https://github.com/BjdsecCA/BJDCTF2020 密文&#xff1a; 下载附件&#xff0c;得到一个zip压缩包。 解题思路&#xff1a; 1、尝试解压压缩包&#xff0c;提示“attachment_10.zip”不是压缩文…