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 ipymplDownloading 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.0Downloading 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.9Downloading 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.9Downloading 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-binDownloading 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.mzph.cn/news/148507.shtml

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

相关文章

springcloud新闻发布系统源码

开发技术&#xff1a; jdk1.8&#xff0c;mysql5.7&#xff0c;nodejs&#xff0c;idea&#xff0c;vscode springcloud springboot mybatis vue elementui 功能介绍&#xff1a; 用户端&#xff1a; 登录注册 首页显示搜索新闻&#xff0c;新闻分类&#xff0c;新闻列表…

Redis 访问控制列表(ACL)

Redis ACL 关于 Redis ACL与旧版本兼容ACL helpACL 配置模式redis.conf 配置模式外部 ACL File 配置模式 ACL 规则总结 关于 Redis ACL Redis ACL (访问控制列表) 是 Access Control List 的缩写&#xff0c;它允许某些连接在可以执行的命令和可以访问的密钥方面受到限制。它…

Vue 路由缓存 防止路由切换数据丢失 路由的生命周期

在切换路由的时候&#xff0c;如果写好了一丢数据在去切换路由在回到写好的数据的路由去将会丢失&#xff0c;这时可以使用路由缓存技术进行保存&#xff0c;这样两个界面来回换数据也不会丢失 在 < router-view >展示的内容都不会被销毁&#xff0c;路由来回切换数据也…

算法设计与分析复习--贪心(二)

文章目录 上一篇哈夫曼编码单源最短路最小生成树Kruskal算法Prim算法 多机调度问题下一篇 上一篇 算法设计与分析复习–贪心&#xff08;一&#xff09; 哈夫曼编码 产生这种前缀码的方式称为哈夫曼树 哈夫曼树相关习题AcWing 148. 合并果子 #include <iostream> #inc…

RT-Thread STM32F407 BMI088--SPI

BMI088是一款高性能6轴惯性传感器&#xff0c;由16位数字三轴24g加速度计和16位数字三轴2000/ s陀螺仪组成。 这里用SPI来驱动BMI088进行数据解读 第一步&#xff0c;首先在 RT-Thread Settings中进行配置 第二步&#xff0c;退出RT-Thread Settings&#xff0c;进入board.h…

Linux|僵死进程

1.僵死进程产生的原因或者条件: 什么是僵死进程? 当子进程先于父进程结束,父进程没有获取子进程的退出码,此时子进程变成僵死进程. 简而言之,就是子进程先结束,并且父进程没有获取它的退出码; 那么僵死进程产生的原因或者条件就是:子进程先于父进程结束,并且父进程没有获取…

基于卡尔曼滤波实现行人目标跟踪

目录 1. 作者介绍2. 目标跟踪算法介绍2.1 目标跟踪背景2.2 目标跟踪任务分类2.3 目标跟踪遇到的问题2.4 目标跟踪方法 3. 卡尔曼滤波的目标跟踪算法介绍3.1 所用数据视频说明3.2 卡尔曼滤波3.3 单目标跟踪算法3.3.1 IOU匹配算法3.3.2 卡尔曼滤波的使用方法 3.4 多目标跟踪算法 …

rocketmq 安装dashboard1.0.0 mq消息控制台安装 rocketmq控制台安装 rocketmq-dashboard-1.0.0编译安装

1. 官网&#xff1a; 下载 | RocketMQ 2. dashboard安装包位置&#xff1a; 在连接最下面&#xff0c;点击download.zip即可 3. 需要安装maven, 编译命令&#xff1a; mvn clean install -U -Dmaven.test.skiptrue4. 启动jar: java -jar rocketmq-dashboard-1.0.0.jar &…

在线随机字符串生成工具

具体请前往&#xff1a;在线随机字符串生成器--通过该工具生成动态复杂随机密码,随机字符串等&#xff0c;加密盐等

Leetcode—141.环形链表【简单】

2023每日刷题&#xff08;三十三&#xff09; Leetcode—141.环形链表 快慢指针算法思想 关于快慢指针为什么能检测出环&#xff0c;可以这么思考。 假设存在一个环: 慢指针进入环后&#xff0c;快指针和慢指针之间相距为d&#xff0c;每一次移动&#xff0c;d都会缩小1&…

【项目设计】网络版五子棋游戏

文章目录 一、项目介绍1. 项目简介2. 开发环境3. 核心技术4. 开发阶段 二、环境搭建1. 安装 wget 工具2. 更换 yum 源3. 安装 lrzsz 传输工具4. 安装⾼版本 gcc/g 编译器5. 安装 gdb 调试器6. 安装分布式版本控制工具 git7. 安装 cmake8. 安装 boost 库9. 安装 Jsoncpp 库10. 安…

[AI]ChatGPT4 与 ChatGPT3.5 区别有多大

ChatGPT 3.5 注册已经不需要手机了&#xff0c;直接邮箱认证就可以&#xff0c;这可真算是好消息&#xff0c;坏消息是 ChatGPT 4 还是要收费。 那么 GPT-3.5 与 GPT-4 区别有多大呢&#xff0c;下面简单测试一下。 以从 TDengine 订阅数据为例&#xff0c;TDengine 算是不太小…

Appium自动化测试:通过appium的inspector功能无法启动app的原因

在打开appium-desktop程序&#xff0c;点击inspector功能&#xff0c;填写app的配置信息&#xff0c;启动服务提示如下&#xff1a; 报错信息&#xff1a; An unknown server-side error occurred while processing the command. Original error: Cannot start the cc.knowyo…

专业数据标注公司:景联文科技领航数据标注行业,满足大模型时代新需求

随着大模型的蓬勃发展和相关政策的逐步推进&#xff0c;为数据要素市场化配置的加速推进提供了有力的技术保障和政策支持。数据要素生产力度的不断提升&#xff0c;为数据标注产业带来了迅速发展的契机。 根据国家工信安全发展研究中心测算&#xff0c;2022年中国数据加工环节的…

c# 字符串转换为byte

c# 字符串转换为byte using System.Text; class proj {internal static void Main(string[] args){byte[] anew byte[3];Console.WriteLine("打印a");Console.WriteLine("a的长度{0}",a.Length);foreach (byte b in a){ Console.WriteLine(b); }a Encodi…

linux系统环境下mysql安装和基本命令学习

此篇文章为蓝桥云课--MySQL的学习记录 块引用部分为自己的实验部分&#xff0c;其余部分是课程自带的知识&#xff0c;链接如下&#xff1a; MySQL 基础课程_MySQL - 蓝桥云课 本课程为 SQL 基本语法及 MySQL 基本操作的实验&#xff0c;理论内容较少&#xff0c;动手实践多&am…

muduo源码剖析之TcpServer服务端

简介 TcpServer拥有Acceptor类&#xff0c;新连接到达时new TcpConnection后续客户端和TcpConnection类交互。TcpServer管理连接和启动线程池&#xff0c;用Acceptor接受连接。 服务端封装 - muduo的server端维护了多个tcpconnection 注意TcpServer本身不带Channel&#xff0…

国民技术Cortex-M0系列单片机IAP升级

考虑到设备部署到现场后有可能需要进行软件升级&#xff0c;之前做过PIC系列单片机的升级&#xff0c;现在想做个国民技术N32G031系列Cortex-M0内核的单片机IAP方案。 因为国民技术系列单片机在很多大程度上都模仿了STM32&#xff0c;所以我想其升级方案极有可能差不多。于是在…

视频封面:从视频中提取封面,轻松制作吸引人的视频

在当今的数字时代&#xff0c;视频已成为人们获取信息、娱乐和交流的重要方式。一个吸引人的视频封面往往能抓住眼球&#xff0c;提高点击率和观看率。今天将介绍如何从视频中提取封面&#xff0c;轻松制作吸引人的视频封面。 一、准备素材选择合适的视频片段 首先&#xff0…

ubuntu中用docker部署jenkins,并和码云实现自动化部署

1.部署jenkins docker network create jenkins docker run --name jenkins-docker --rm --detach \--privileged --network jenkins --network-alias docker \--env DOCKER_TLS_CERTDIR/certs \--volume jenkins-docker-certs:/certs/client \--volume jenkins-data:/var/jen…