当使用外轴法使用 mplfinance 库绘制多个烛台图表时,如何在烛台图表内绘制成交量? [英] When using external axes method to plot multiple candlestick charts using mplfinance library, how to plot volume inside the candlestick chart?

查看:189
本文介绍了当使用外轴法使用 mplfinance 库绘制多个烛台图表时,如何在烛台图表内绘制成交量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做的项目需要代码使用 mplfinance 库在几个数字中绘制 300 多个烛台图表.我知道这只能使用外轴方法来完成,因为它提供了更多的灵活性,并且理论上可以绘制无限的图表.

我当前使用的代码如下,绘制的图表如下:

将mplfinance导入为mpfs = 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)图, 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)

我的代码绘制的数百个图表中的 6 个示例图表的屏幕截图:

以上代码绘制的两张图表截图如下:

如您所见,成交量图表绘制在烛台图表下方的单独图表中.我很难找到将交易量移动到烛台图表的解决方案,mplfinance 文档中有一个类似的帖子

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)

解决方案

我会假设您要的是让交易量和烛台共享相同的 x 轴

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

这篇关于当使用外轴法使用 mplfinance 库绘制多个烛台图表时,如何在烛台图表内绘制成交量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆