简体   繁体   English

当代码中的任何地方都不接受“returnfig=True”时更改 mplfinance 图表样式

[英]Changing mplfinance chart styling when "returnfig=True" is not being accepted anywhere in the code

I am trying to change the axis titles and font size in multiple charts that are plotted using mpf.plot and fig.add_axes to place them on screen.我正在尝试更改使用 mpf.plot 和 fig.add_axes 绘制的多个图表中的轴标题和字体大小,以将它们放置在屏幕上。

I have understood the process and examples given in this link How to change font size and font type in mplfinance title and on the github discussing the approach, but it will not work with my code.我已经了解了此链接中给出的过程和示例How to change font size and font type in mplfinance title以及在 github 上讨论该方法,但它不适用于我的代码。 Anywhere I put "returnfig=True" it causes errors.我把“returnfig = True”放在任何地方都会导致错误。

Here is my code snippet of the relevant section这是我相关部分的代码片段

fig = mpf.figure(figsize=(12,8),style='yahoo')

ax1 = fig.add_axes([0.05,0.70,0.3,0.25]) # ax = fig.add_axes([left,bottom,width,height])
ax2 = fig.add_axes([0.05,0.65,0.3,0.05])

ax3 = fig.add_axes([0.36,0.70,0.3,0.25])
ax4 = fig.add_axes([0.36,0.65,0.3,0.05])

ax5 = fig.add_axes([0.68,0.70,0.3,0.25])
ax6 = fig.add_axes([0.68,0.65,0.3,0.05])

ax7 = fig.add_axes([0.05,0.25,0.3,0.25])
ax8 = fig.add_axes([0.05,0.20,0.3,0.05])

ax9 = fig.add_axes([0.36,0.25,0.3,0.25])
ax10 = fig.add_axes([0.36,0.20,0.3,0.05])

ax11 = fig.add_axes([0.68,0.25,0.3,0.25])
ax12 = fig.add_axes([0.68,0.20,0.3,0.05])

mpf.plot(ausd,type='candle',ax=ax1,volume=ax2, mav=(9), show_nontrading=False, axtitle='M6A=F')
mpf.plot(gbpf,type='candle',ax=ax3,volume=ax4, mav=(9), show_nontrading=False, axtitle='M6B=F')
mpf.plot(cadf,type='candle',ax=ax5,volume=ax6, mav=(9), show_nontrading=False, axtitle='M6E=F')
mpf.plot(btcf,type='candle',ax=ax7,volume=ax8, mav=(9), show_nontrading=False, axtitle='M6C=F')
mpf.plot(gldf,type='candle',ax=ax9,volume=ax10, mav=(9), show_nontrading=False, axtitle='MBT=F')
mpf.plot(slvf,type='candle',ax=ax11,volume=ax12, mav=(9), show_nontrading=False, axtitle='MGC=F')

The are two ways to gain access to the Figure and Axes objects that mplfinance uses.两种方法可以访问 mplfinance 使用的 Figure 和 Axes 对象。 These two ways are documented here . 这两种方式都记录在这里

1. If you use returnfig=True it means that mpf.plot() is creating the Figure and Axes objects internally, and returning them to you for further manipulation before you eventually call mpf.show() or fig.savefig() to complete the plot. 1.如果您使用returnfig=True这意味着mpf.plot()正在内部创建 Figure 和 Axes 对象,并在最终调用mpf.show()fig.savefig()完成之前将它们返回给您以进行进一步操作剧情。

2. However, if you create your own Figure and Axes objects externally to mpf.plot() , even if you use mpf.figure() to create the Figure object, then mpf.plot() does not own the Figure and Axes objects, and so cannot return them to you! 2.但是,如果您在mpf.plot()外部创建自己的Figure 和 Axes 对象,即使您使用mpf.figure()创建 Figure 对象,那么mpf.plot()也不拥有 Figure 和 Axes 对象,因此无法将它们退还给您!
In fact, you already have them and own them so there is no point to returning them!事实上,您已经拥有它们并拥有它们,因此没有必要归还它们! This is why, when mpf.plot() detects that you have passed Axes into it (thus it is in "External Axes Mode") then it will reject the kwarg returnfig because in that context it doesn't make any sense to return a Figure and Axes that you already have.这就是为什么,当mpf.plot()检测到您已将 Axes 传递给它时(因此它处于“外部 Axes 模式”)然后它将拒绝 kwarg returnfig因为在这种情况下返回 a 没有任何意义您已经拥有的图形和轴。

I understand that returnfig=True is required if you want to access each of the panels when they are set up.我知道如果您想在设置每个面板时访问它们,则需要returnfig=True My understanding may still be lacking.我的理解可能还不够。 If you set up a subplot with additional axes, I don't think you need returnfig=True .如果您设置带有附加轴的子图,我认为您不需要returnfig=True Completing the graph with the ticker in the previous question would result in the following code.使用上一个问题中的股票代码完成图表将产生以下代码。 I introduced loop processing, shortened the long date format, and reduced the orientation of the ticker.我引入了循环处理,缩短了长日期格式,并减少了代码的方向。

myTickers = ['M6A=F','M6B=F','M6C=F','M6E=F','MBT=F','MGC=F']
fig = mpf.figure(figsize=(12,12), style='yahoo')

ax1 = fig.add_axes([0.05,0.70,0.25,0.25]) 
ax2 = fig.add_axes([0.05,0.60,0.25,0.08])

ax3 = fig.add_axes([0.36,0.70,0.25,0.25])
ax4 = fig.add_axes([0.36,0.60,0.25,0.08])

ax5 = fig.add_axes([0.68,0.70,0.25,0.25])
ax6 = fig.add_axes([0.68,0.60,0.25,0.08])

ax7 = fig.add_axes([0.05,0.25,0.25,0.25])
ax8 = fig.add_axes([0.05,0.15,0.25,0.08])

ax9 = fig.add_axes([0.36,0.25,0.25,0.25])
ax10 = fig.add_axes([0.36,0.15,0.25,0.07])

ax11 = fig.add_axes([0.68,0.25,0.25,0.25])
ax12 = fig.add_axes([0.68,0.15,0.25,0.08])

for i,(t,ax) in enumerate(zip(myTickers,[[ax1,ax2],[ax3,ax4],[ax5,ax6],[ax7,ax8],[ax9,ax10],[ax11,ax12]])):
    dff = df.loc[:,t]
    mpf.plot(dff, type='candle',
             ax=ax[0],
             volume=ax[1],
             mav=(9),
             show_nontrading=False,
             axtitle=t,
             datetime_format='%b %d',
             xrotation=30)
    
for ax in [ax1,ax3,ax5,ax7,ax9,ax11]:
    ax.set_xticklabels([])

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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