首页 > 解决方案 > Python烛台图无法显示

问题描述

我执行了下面的代码,过程中没有显示错误,但是 plt.show() 无法工作,最后什么也没显示!我很困惑,想知道为什么......

from mpl_finance import candlestick_ohlc

import matplotlib.dates as mdates

import matplotlib.pyplot as plt

import quandl

df = quandl.get("EOD/AAPL", authtoken="fzTPb-TWywaPkbdAS1VF")

df['Date'] = df.index.map(mdates.date2num)

ohlc = df[['Date','Open','High','Low','Close']]

f1, ax = plt.subplots(figsize = (10,5))

candlestick_ohlc(ax, ohlc.values, width=.6, colorup='green', colordown='red')
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m'))

plt.show()

标签: pythonmatplotlibquandl

解决方案


在 jupyter notebook 中,定义后端是有意义的,例如%matplotlib inlineor %matplotlib notebook。然后在单个单元格中运行您的代码时,它应该向您显示该图。

如果您需要在单独的单元格中运行它,plt.show()将不知道从以前的单元格中显示什么。因此,在这种情况下,请改为说明数字,

f1

显示图f1


推荐阅读