我正在使用 matplotlib Finance (mpfinance),目前遇到奇怪的间歇性绘图问题。(堆栈溢出中没有标签,因此正确标记具有挑战性:https ://github.com/matplotlib/mplfinance )
今晚早些时候我让这段代码工作并显示了 2 个正确的 y 标签/轴,除了一些语法清理之外,代码中没有任何重要的变化。
在 AAPL 包含的图片中,似乎次要 y 轴正在尝试绘制,但是,它显示为总体积的百分比,而不是它自己的 y 轴和百分比。
评论应该解释思考过程。
import mplfinance as mpf
# AAPL call volume %
# Selecting specifics mentioning AAPL
AAPL_df = naster_df[master_df['ticker'] == 'AAPL'][[
'ticker', 'date', 'call_volume', 'put_volume',
'call_ratio', 'put_ratio', 'open', 'low',
'high', 'close','volume']]
# MPF requires DateTimeIndex for plotting
AAPL_df = AAPL_df.set_index('date')
# Dropping NA's, Not sure if needed so commented out
AAPL_df.dropna()
# Plotting call ratio with close price and volume
# MPF package requires volume to be explicitly named
# Dropping share volume as calculation is already made
# Renaming call volume to volume
AAPL_df = AAPL_df.drop(
'volume', axis = 1).rename(
columns = {'call_volume':'volume'})
# Adding a call ratio (in %) as the bottom panel secondary y axis
ap = mpf.make_addplot((AAPL_df['call_ratio']), panel = 1, linestyle = 'dotted', ylabel = 'Options % ratio')
# Plotting AAPL share price with Open, High, Low, Close candles
# call_volume = volume
mpf.plot(AAPL_df,
type = 'candle',
volume = True,
addplot = ap,
ylabel = 'AAPL share price',
ylabel_lower = 'Call Volume')
这产生了这个情节:
这没有显示正确的情节。删除addplot = ap
不会更改此图像。
但是,具有不同股票代码数据框的相同代码在下面工作(它们具有相同的确切格式)
# Plotting call ratio with close price and volume
ap = mpf.make_addplot((TSLA_df['call_ratio']), panel = 1, color = 'black', linestyle = 'dotted', ylabel = 'Call volume %')
mpf.plot(TSLA_df,
type = 'candle',
volume = True,
addplot = ap,
style = 'binance',
ylabel = 'TSLA share price',
ylabel_lower = 'Call Volume')
产生:
他们都从数据框中提取提到特定代码的数据,并且没有 NaN,所以我不知道它为什么不起作用。我试图让y轴上的虚线位于下框的底部。我想我很难弄清楚为什么相同的代码不适用于特定的情节,并且想知道这种类型的问题是否与我的 matplotlib 财务代码有关。
如果有人对为什么会发生这种情况有任何想法,他们将不胜感激。
样本df:
date ticker call_volume call_ratio open low high close volume
2021-03-08 AAPL 1229656 0.5782918149466192 120.93 116.209999 121.0 116.360001 154376600.0
2021-03-09 AAPL 774465 3.357156230430039 119.029999 118.790001 122.059998 121.089996 129525800.0
2021-03-10 AAPL 447447 3.9110777365810923 121.690002 119.449997 122.16999799999999 119.980003 111943300.0
2021-03-11 AAPL 577996 1.730115779347954 122.540001 121.260002 123.209999 121.959999 103026500.0
2021-03-12 AAPL 884787 0.5651077603988305 120.400002 119.160004 121.16999799999999 121.029999 88105100.0
2021-03-15 AAPL 778816 1.0272002629632673 121.410004 120.41999799999999 124.0 123.989998 92403800.0
2021-03-16 AAPL 1398777 1.8768538516146607 125.699997 124.720001 127.220001 125.57 115227900.0
2021-03-17 AAPL 978950 0.30645078911078194 124.050003 122.339996 125.860001 124.760002 111932600.0
2021-03-18 AAPL 1041143 2.7229688909208436 122.879997 120.32 123.18 120.529999 121229700.0
2021-03-19 AAPL 1123895 2.2817967870664075 119.900002 119.68 121.43 119.989998 185549500.0