我正在尝试用 VSC 中的一些测试代码来显示一个图。
我安装了 Matplotlib 3.2.2 版本,正如 Backtrader 的 documentation 中所述,这是兼容版本。我在自定义环境中运行 Python 3.10.13。
这是我的 matplotlib 安装。尽管我通过 pip 重新安装了 Matplotlib_inline,但它仍然显示在 conda-forge 频道中。我试图以相同的方式安装软件包,但我的一些软件包是使用 pip 与 conda 安装的。我安装了下面实际代码中的所有导入包。我不知道这是否是依赖/兼容性问题,但我怀疑。
# Name Version Build Channel
matplotlib 3.2.2 pypi_0 pypi
matplotlib-inline 0.1.7 pyhd8ed1ab_0 conda-forge
我认为我的代码没有任何问题——没有出现错误。尝试绘制后我最后一个单元格下的输出是
[[<Figure size 640x480 with 4 Axes>]]
。
我的代码:
%matplotlib inline
import matplotlib
import matplotlib_inline
import matplotlib.pyplot as plt # For plotting with matplotlib
import backtrader as bt
import backtrader.plot as btplt
import datetime
import yfinance as yf
import backtrader.plot as btplt
df = yf.download('AAPL', start='2010-01-01', end='2024-01-01')
feed = bt.feeds.PandasData(dataname=df)
cerebro = bt.Cerebro()
cerebro.adddata(feed)
cerebro.run()
matplotlib.pyplot.switch_backend('module://ipykernel.pylab.backend_inline')
cerebro.plot()
最终单元格中没有显示任何图,仅显示
[[<Figure size 640x480 with 4 Axes>]]
我尝试卸载并重新安装所有关联的软件包。我什至尝试了旧版本的 matplotlib。我希望这能解决无情节问题。仍然没有情节显示。
尝试使用此方法在 VSC 和 Colab 中显示绘图。将绘图保存为图像并使用 IPython.display.Image 显示它:
from IPython.display import Image
# Set the path to save the image
img_path = "backtrader_plot.png"
# Generate the plot and save it
fig = cerebro.plot(style="candlestick")[0][0]
fig.savefig(img_path)
# Display the saved image
Image(filename=img_path)