The project I am doing requires code to plot more than 300 candlestick charts in several figures using mplfinance library. I am aware that this can only be done using external axes method as it provides more flexibilities and can plot unlimited charts theoretically.
The current code I am using is as below, the charts plotted can be seen below:
import mplfinance as mpf
s = mpf.make_mpf_style(base_mpf_style='yahoo', rc={'font.size': 6})
fig = mpf.figure(figsize=(34, 13.2), style=s, tight_layout=True)
ax_p = fig.add_subplot(n_rows, n_cols, pos_price)
ax_v = fig.add_subplot(n_rows, n_cols, pos_vol, sharex=ax_p)
fig, ax_list = mpf.plot(resampled_df, type='candle', ax=ax_p, volume=ax_v, show_nontrading=False,
datetime_format='%a %d-%m-%y', xrotation=0, returnfig=True)
The screenshot of the 6 sample charts from hundreds of charts my code plotted:
The screenshot of the two charts the above code plotted is as below:
As you can see the volume chart was plotted in an individual chart below the candlestick chart. I struggle to find the solution to move the volume into candlestick chart, there is a similar post in mplfinance documentation issue 114 kind of explains how to do this...... but I found it is rather difficult to understand for new ppl to the library like me.
Would highly appreciate it if you could post the detailed code to do this!
Update on 12th Feb 2021:
I modified the code with @Daniel's suggestion, use add_axes()
rather than add_subplot()
and now the volume is at the bottom of the candlestick chart when plotting multiple charts. Beautiful! Answer accepted.
ax_intra_day_candle = fig.add_axes([x_pos, y_pos, ax_width, ax_height])
ax_intra_day_candle.set_title(title)
ax_intra_day_volume = fig.add_axes([x_pos, y_pos - ax_vol_height, ax_width, ax_vol_height], sharex=ax_intra_day_candle)
mpf.plot(intra_day_df, type='candle', ax=ax_intra_day_candle, volume=ax_intra_day_volume, show_nontrading=False,
datetime_format='%a %m-%d', xrotation=0)
I will assume what you are asking is to have the volume and candlesticks share the same x-axis similar to this image here.
The simplest way to do this is to use fig.add_axes()
(instead of fig.add_subplot()
)
In this way you can control exactly where in the Figure each Axes is placed. You can see this being done in the mplfinance code here.
The basic idea is that you specify the location of each Axes object in terms of a fraction of the total figure, indicating the lower left corner of the Axes, and its width and height.
When you want two Axes objects to touch, with no space between them, you specify the location and width/height accordingly so that the top of the lower Axes and the bottom of the upper Axes exactly meet.
So, for example, to stack two equally sized Axes exactly on top of each other, lets say in the upper left quadrant of the Figure you would have:
# ax = fig.add_axes([left,bottom,width,height])
ax1 = fig.add_axes([0.05,0.75,0.5,0.25])
ax2 = fig.add_axes([0.05,0.50,0.5,0.25])
HTH
Here is a more specific example, and the result. Notice how the candles and volume plot together only take up the upper left quadrant of the figure:
fig = mpf.figure(figsize=(8,8),style='yahoo')
ax1 = fig.add_axes([0.05,0.75,0.5,0.25])
ax2 = fig.add_axes([0.05,0.50,0.5,0.25])
mpf.plot(df,type='candle',ax=ax1,volume=ax2)
mpf.show()