matplotlib索引方式错误

rta7y2nd  于 2023-01-21  发布在  其他
关注(0)|答案(1)|浏览(94)

我正尝试在现有(实时)图上绘制垂直线。

def animate(ival):
    df =  pd.read_pickle("/Users/user/Workfiles/Python/rp/0.72.0.0/df.pkl")

    ax1.clear()
    mpf.plot(df, ax=ax1, type='candle', ylabel='p', warn_too_much_data=999999999999)

 
    try:
        ax1.hlines(y=price, xmin=df.shape[0]-10, xmax=df.shape[0], color='r', linewidth=1)
    except UnboundLocalError:
        pass

ani = FuncAnimation(fig, animate, interval=100)

mpf.show()

这是正常的:

现在我需要添加垂直线。我想看到plottet的行的索引号在这个变量中:lows_peaks
df.iloc[低峰]:

open    high  ...  
datetime                                    ...                                   
2023-01-20 15:07:30.776127  3919.0  3919.0  ...    
2023-01-20 15:14:46.116836  3915.0  3915.0  ...    
2023-01-20 15:23:23.845752  3928.0  3928.0  ...    
2023-01-20 15:30:08.680839  3917.0  3917.0  ...    
2023-01-20 15:37:26.709335  3938.0  3938.0  ...    
2023-01-20 15:43:57.275134  3941.0  3941.0  ...    
2023-01-20 15:55:56.717249  3951.0  3951.0  ...     
2023-01-20 16:03:24.278924  3939.0  3939.0  ...    
2023-01-20 16:10:05.334341  3930.0  3930.0  ...     
2023-01-20 16:18:53.015390  3955.0  3955.0

现在添加vline:

for i in df.iloc[lows_peaks].index:
    ax1.vlines(x=i, ymin=df.low.min(), ymax=df.high.max(), color='r', linewidth=1)

结果:

i是正确的时间戳:

2023-01-20 15:07:30.776127
2023-01-20 15:14:46.116836
2023-01-20 15:23:23.845752
2023-01-20 15:30:08.680839
2023-01-20 15:37:26.709335
2023-01-20 15:43:57.275134
2023-01-20 15:55:56.717249
2023-01-20 16:03:24.278924
2023-01-20 16:10:05.334341
2023-01-20 16:18:53.015390

为什么垂直线在图的右侧很远的地方?
最小可复制代码:

import pandas as pd
import numpy as np
from matplotlib.animation import FuncAnimation
import mplfinance as mpf

times = pd.date_range(start='2022-01-01', periods=50, freq='ms')

df = pd.DataFrame(np.random.randint(3000, 3100, (50, 1)), columns=['open'])
df['high'] = df.open+5
df['low'] = df.open-2
df['close'] = df.open
df.set_index(times, inplace=True)
lows_peaks = df.low.nsmallest(5).index
print(lows_peaks)

fig = mpf.figure(style="charles",figsize=(7,8))
ax1 = fig.add_subplot(1,1,1)

def animate(ival):
    ax1.clear()
    
    for i in lows_peaks:
        ax1.vlines(x=i, ymin=df.low.min(), ymax=df.high.max(), color='blue', linewidth=3)
    mpf.plot(df, ax=ax1)
    
ani = FuncAnimation(fig, animate, interval=100)

mpf.show()
wlsrxk51

wlsrxk511#

我不确定其余的代码是否正常工作,但是matplotlib.pyplot vlines似乎不太适合mplfinance绘图(至少当它们是时间戳时).查看mplfinace github有一节是关于使用垂直线的:https://github.com/matplotlib/mplfinance/blob/master/examples/using_lines.ipynb
在这里,使用代码mpf.plot(df, ax=ax1, vlines=dict(vlines=list(lows_peaks),linewidths=(1,1,1,1)))生成了一个带有线的预期位置的图形:

import pandas as pd
import numpy as np
from matplotlib.animation import FuncAnimation
import mplfinance as mpf

times = pd.date_range(start='2022-01-01', periods=50, freq='ms')

df = pd.DataFrame(np.random.randint(3000, 3100, (50, 1)), columns=['open'])
df['high'] = df.open+5
df['low'] = df.open-2
df['close'] = df.open
df.set_index(times, inplace=True)
lows_peaks = df.low.nsmallest(5).index

fig = mpf.figure(style="charles",figsize=(7,8))
ax1 = fig.add_subplot(1,1,1)

mpf.plot(df, ax=ax1, vlines=dict(vlines=list(lows_peaks),linewidths=(1,1,1,1)))

mpf.show()

相关问题