Search code examples
python-3.xcandlestick-chartmplfinance

mplfinance candlsticks piling on chart and not moving forward


Candlestick Chart is being updated ever minute but animation not working correctly

I am fetching the data from csv which is being updated from websocket. in the chart alothough candles are showing but they squezing the space between candles and getting smaller and smaller. Please let me know what is wrong I am doing.

    fig = mpf.figure(style='binance',figsize=(12,4))
ax1 = fig.add_subplot(1,1,1)

def animate(ival):
    df = pd.read_csv('bitcoin_data.csv', index_col=0, parse_dates=True)
    df = df[['minute', 'open', 'high',
             'low', 'close']]
    df.minute = pd.to_datetime(df.minute)
    df = df.set_index('minute')

    ax1.clear()
    mpf.plot(df, ax=ax1, type='candlestick', ylabel='price')

ani = animation.FuncAnimation(fig, animate, interval=1000)\

mpf.show()

Solution

  • My guess is that, after some time, you are trying to plot too much data. See https://github.com/matplotlib/mplfinance/wiki/Plotting-Too-Much-Data for more information.

    The solution would be to modify your animate function to plot only the most recent 500 to 700 candles from your dataframe.

    Maybe something like:

    if len(df) > 600:
        df = df.iloc[-600:-1,:] 
    mpf.plot(df, ax=ax1, type='candlestick', ylabel='price')