2021年1月23日星期六

Overlaying data in a candlestick chart using matplotlib

My very 1st need is to overlay a curve over a candlestick chart. To draw this candlestick chart, mplfinance appears very nice. Unfortunately, I have found no way to overlay additional curve to the candlestick.

Considering example below, if anyone of you knows how to overlay 'Noise' column over the candlesticks drawn from 'minutely' dataframe, this would be great!

import pandas as pd  import mplfinance as mpf    minutely = pd.DataFrame({'Date':[pd.Timestamp('2021-01-07 00:00:00'),                                   pd.Timestamp('2021-01-07 00:01:00'),                                   pd.Timestamp('2021-01-07 00:02:00'),                                   pd.Timestamp('2021-01-07 00:03:00'),                                   pd.Timestamp('2021-01-07 00:04:00')],                            'Open':[36769.36, 36880.00, 36851.42,36922.19,37083.18],                            'High':[36880.00, 36880.00, 36950.00, 37089.69, 37094.70],                            'Low': [36760.00,  36817.64, 36810.03, 36922.13, 36565.49],                            'Close':[36880.00, 36851.97, 36922.14, 37075.80, 36691.3]})    noise = pd.DataFrame({'Date':[pd.Timestamp('2021-01-07 00:00:00'),                                   pd.Timestamp('2021-01-07 00:01:00'),                                   pd.Timestamp('2021-01-07 00:02:00'),                                   pd.Timestamp('2021-01-07 00:03:00'),                                   pd.Timestamp('2021-01-07 00:04:00')],                            'Noise':[36779.36, 36870.00, 36881.42,36902.19,37103.18]})    # Draw candlesticks  minutely = minutely.set_index('Date')  noise = noise.set_index('Date')  mpf.plot(minutely, type='candle')  

Not stopping here, I had in mind to use the 'old' candlestick_ohlcv function to draw a candlestick graph, then overlay data using native matplotlib capabilities. This gives the code below, but when displaying the candlesticks, the x scale appears meaningless.

I should have only 5 candles, with a x scale going from 2020/01/07 00:00 to 2020/01/07 00:04.

I can see the graph shows a x scale starting from 2020/01/06 16:48 to 2020/01/07 07:12.

I don't understand this and candles are meaningless...

import pandas as pd  import matplotlib.pyplot as plt   from mplfinance.original_flavor import candlestick_ohlc  import matplotlib.dates as mpdates     minutely = pd.DataFrame({'Date':[pd.Timestamp('2021-01-07 00:00:00'),                                   pd.Timestamp('2021-01-07 00:01:00'),                                   pd.Timestamp('2021-01-07 00:02:00'),                                   pd.Timestamp('2021-01-07 00:03:00'),                                   pd.Timestamp('2021-01-07 00:04:00')],                            'Open':[36769.36, 36880.00, 36851.42,36922.19,37083.18],                            'High':[36880.00, 36880.00, 36950.00, 37089.69, 37094.70],                            'Low': [36760.00,  36817.64, 36810.03, 36922.13, 36565.49],                            'Close':[36880.00, 36851.97, 36922.14, 37075.80, 36691.3]})    noise = pd.DataFrame({'Date':[pd.Timestamp('2021-01-07 00:00:00'),                                   pd.Timestamp('2021-01-07 00:01:00'),                                   pd.Timestamp('2021-01-07 00:02:00'),                                   pd.Timestamp('2021-01-07 00:03:00'),                                   pd.Timestamp('2021-01-07 00:04:00')],                            'Noise':[36779.36, 36870.00, 36881.42,36902.19,37103.18]})    minutely['Date'] = minutely['Date'].map(mpdates.date2num)    plt.style.use('dark_background')  fig, ax = plt.subplots()   candlestick_ohlc(ax, minutely.values, width = 0.6,                     colorup = 'green', colordown = 'red',                     alpha = 0.8)   # Setting labels    ax.set_xlabel('Date')   ax.set_ylabel('Price')  # Formatting Date  date_format = mpdates.DateFormatter('%d-%m-%Y %H:%M')   ax.xaxis.set_major_formatter(date_format)   fig.autofmt_xdate()   fig.tight_layout()   # show the plot   plt.show()  

Please, would someone know of a way to overlay this external data to candlestick data? Thanks in advance for any help, Bests,

https://stackoverflow.com/questions/65778125/overlaying-data-in-a-candlestick-chart-using-matplotlib January 19, 2021 at 12:08AM

没有评论:

发表评论