简体   繁体   中英

When using external axes method to plot multiple candlestick charts using mplfinance library, how to plot volume inside the candlestick chart?

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])
  • The 0.05 space to the left allows room for the y-axis labels.
  • ax1 starts three quarters (0.75) of the way up from the bottom, and stretches half way (0.5) to the right with a height of 0.25 (which takes it to the very top of the Figure).
  • ax2 starts half way (0.50) up from the bottom, also stretches half way (0.5) across to the right, and has a height of 0.25 taking it exactly to the very bottom of ax1 .

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()

在此处输入图像描述

No need so long code use the below code suppose you have volume and data for plotting in a chart using mplfinance

mpf.plot(data,type='candle',style='yahoo',volume=True)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM