python-3.x mplfinance:'secondary_y=True'不工作,由于Y轴数据范围非常宽,因此未绘制多条线/蜡烛图

0md85ypi  于 2023-06-07  发布在  Python
关注(0)|答案(1)|浏览(217)

我想在python 3.11.3中使用mplfinance在一个图表对象(单个面板)中比较两个股票指数的价格走势。
已尝试代码段中的代码:https://github.com/matplotlib/mplfinance/blob/master/examples/addplot.ipynb
我有数据在3 excel csv文件中,这些文件将在一个图表中绘制(线或蜡烛):
Indeces data
Data of scrip1
Data of scrip2
两个表的比较生成下图:
Chart comparing data in two tables
我尝试了中给出的mplfinance方法
Plot multiple mplfinance plots sharing x axis
此方法退出控制台。
然后,尝试了以下代码:

import mplfinance as mpf
import pandas as pd

idc = pd.read_csv(r'D:\NUVAMA_STOCK_DATA\CHARTING-BANKNIFTY-25 MAY 2023\Nifty Bank_03March2023-25 MAY 2023_CHART.csv')
idc['Date'] = pd.to_datetime(idc['Date'], format = "mixed")
idc.set_index('Date', inplace=True)
intraday = idc
intraday.index.name = 'Date'
intraday.shape
intraday.head(3)
intraday.tail(3)

iday = intraday.loc['22-05-2023  09:16:00':'25-05-2023  15:29:00',:]

ce = pd.read_csv(r'D:\NUVAMA_STOCK_DATA\CHARTING-BANKNIFTY-25 MAY 2023\BANKNIFTY 25MAY 2023 CE 46500_CHART.csv')
ce['Date'] = pd.to_datetime(ce['Date'], format = "mixed")
ce.set_index('Date', inplace=True)
intraday2 = ce
intraday2.index.name = 'Date'
intraday2.shape
intraday2.head(3)
intraday2.tail(3)

iday2 = intraday2.loc['22-05-2023  09:16:00':'25-05-2023  15:29:00',:]


pe = pd.read_csv(r'D:\NUVAMA_STOCK_DATA\CHARTING-BANKNIFTY-25 MAY 2023\BANKNIFTY 25 May 2023 PE 43600_CHART.csv')
pe['Date'] = pd.to_datetime(pe['Date'], format = "mixed")
pe.set_index('Date', inplace=True)
intraday1 = pe
intraday1.index.name = 'Date'
intraday1.shape
intraday1.head(3)
intraday1.tail(3)

iday1 = intraday1.loc['22-05-2023  09:16:00':'25-05-2023  15:29:00',:]

ap = [mpf.make_addplot(iday1, type='line', y_on_right=True), mpf.make_addplot(iday2, type='candle', y_on_right=True),]

mpf.plot(
    iday,
    volume=True,
    title='BANKNIFTY&PE 43600', ylabel='Price ($)', ylabel_lower='Volumes', #figratio=(12,4),
    type="line",
    style="sas",
    addplot=ap, 
    )

此代码给出以下错误消息:

"D:\PYTHON_3.11.3\Lib\site-packages\mplfinance\plotting.py:694: UserWarning: Attempting to set identical low and high ylims makes transformation singular; automatically expanding.
  volumeAxes.set_ylim(vymin,vymax)
Traceback (most recent call last):
  File "D:\PyProgFiles\ChatGPT\CS_Mpf\TEST_MULTI_BNPECE_StackOverFlow.py", line 44, in <module>
    mpf.plot(
  File "D:\PYTHON_3.11.3\Lib\site-packages\mplfinance\plotting.py", line 780, in plot
    ax = _addplot_columns(panid,panels,ydata,apdict,xdates,config)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\PYTHON_3.11.3\Lib\site-packages\mplfinance\plotting.py", line 1075, in _addplot_columns
    ymhi = math.log(max(math.fabs(np.nanmax(yd)),1e-7),10)
                                  ^^^^^^^^^^^^^
  File "<__array_function__ internals>", line 200, in nanmax
  File "D:\PYTHON_3.11.3\Lib\site-packages\numpy\lib\nanfunctions.py", line 483, in nanmax
    res = np.amax(a, axis=axis, out=out, **kwargs)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<__array_function__ internals>", line 200, in amax
  File "D:\PYTHON_3.11.3\Lib\site-packages\numpy\core\fromnumeric.py", line 2820, in amax
    return _wrapreduction(a, np.maximum, 'max', axis, None, out,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\PYTHON_3.11.3\Lib\site-packages\numpy\core\fromnumeric.py", line 86, in _wrapreduction
    return ufunc.reduce(obj, axis, dtype, out, **passkwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: zero-size array to reduction operation maximum which has no identity"

无法理解哪里出了问题。
数据:
BANKNIFTY 2023年5月25日CE 46500_CHART.csv
BANKNIFTY 2023年5月25日PE 43600_CHART
Nifty Bank_2023年3月3日-2023年5月25日_CHART. csv

rqqzpn5f

rqqzpn5f1#

问题是数据文件"BANKNIFTY 25 May 2023 PE 43600_CHART.csv"在右侧包含完全为空的额外列;注意右边的额外逗号:

!head 'BANKNIFTY25MAY2023_43600.csv'
Date,Open,High,Low,Close,Volume,,
24-04-2023 09:16,953.09998,953.09998,953.09998,953.09998,0,,
24-04-2023 09:17,953.09998,953.09998,953.09998,953.09998,0,,
24-04-2023 09:18,953.09998,953.09998,953.09998,953.09998,0,,
24-04-2023 09:19,953.09998,953.09998,953.09998,953.09998,0,,
24-04-2023 09:20,953.09998,953.09998,953.09998,953.09998,0,,
24-04-2023 09:21,953.09998,953.09998,953.09998,953.09998,0,,
24-04-2023 09:22,953.09998,953.09998,953.09998,953.09998,0,,
24-04-2023 09:23,953.09998,953.09998,953.09998,953.09998,0,,
24-04-2023 09:24,953.09998,953.09998,953.09998,953.09998,0,,
!tail 'BANKNIFTY25MAY2023_43600.csv'
25-05-2023 15:22,0.1,0.15,0.1,0.1,1328475,,
25-05-2023 15:23,0.15,0.15,0.1,0.15,876225,,
25-05-2023 15:24,0.15,0.15,0.1,0.1,842600,,
25-05-2023 15:25,0.15,0.15,0.05,0.05,846850,,
25-05-2023 15:26,0.1,0.1,0.05,0.05,1129650,,
25-05-2023 15:27,0.05,0.1,0.05,0.05,471825,,
25-05-2023 15:28,0.05,0.05,0.05,0.05,145525,,
25-05-2023 15:29,0.05,0.05,0.05,0.05,108575,,
25-05-2023 15:30,0.05,0.05,0.05,0.05,71075,,
25-05-2023 15:30,0.05,0.05,0.05,0.05,0,,1501

当您将整个 Dataframe (而不是数组或序列)传递到mpf.make_addplot()时,mpf.plot()将尝试绘制 Dataframe 中的 * 每一列 *。这是你正在做的代码行:ap = [mpf.make_addplot(iday1, type='line', y_on_right=True), ...
对于mpf.plot()尝试绘制的每个列,它还将尝试查找列中的最大和最小非NaN值,以确定y轴比例。然而,对于这些最后到列,存在零个非NaN值,因此不可能确定最小值和最大值。
一个可能的解决方案是用一行代码来删除这些列,如下所示:

iday1 = iday1[['Open','High','Low','Close','Volume']]

如果你在调用mpf.make_addplot()之前添加上面的一行代码,那么你的代码就可以工作了,结果图看起来像这样:

相关问题