mdkb
mdkb

Reputation: 402

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.

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. Anywhere I put "returnfig=True" it causes errors.

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

Upvotes: 0

Views: 1324

Answers (2)

r-beginners
r-beginners

Reputation: 35230

I understand that returnfig=True is required if you want to access each of the panels when they are set up. My understanding may still be lacking. If you set up a subplot with additional axes, I don't think you need 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([])

enter image description here

Upvotes: 1

Daniel Goldfarb
Daniel Goldfarb

Reputation: 7724

The are two ways to gain access to the Figure and Axes objects that mplfinance uses. 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.

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!
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.

Upvotes: 3

Related Questions