-
Notifications
You must be signed in to change notification settings - Fork 657
External Axes Mode and SaveFig #301
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I have no issues with 0.12.7a0 and the below: fig, axlist = mpf.plot(df, type='candle', ..., ylabel='Price', ylabel_lower='Volume', ... , style=s, ..., returnfig=True) |
This is by design. When in external axes mode The general rule is that if two conditions are met, then the kwarg will be ignored or disabled. The two conditions are: (1) The kwarg is just as easily implemented by calling a method on the external Figure or Axes, and (2) there is at least one case where, in external axes mode, it would be undesireable for the code to take the action that the kwarg requests. In this specific case:
I hope that makes sense. I probably should document all of the kwargs that do not work in external axes mode, along with the reasons why for each kwarg. Let me know if you have any more questions on this topic. |
Thank you both for the responses during holidays. I gave a quick shot to Manuel's but it seems not working on my side. I've updated to '0.12.7a4' and the scripts read: file_specs = {'fname': r'.\figure_001.png',
'dpi': 64,
'bbox_inches': 'tight',
'pad_inches': 0.0}
fig, axlist = mpf.plot(df_ohlc,
type = 'candle',
addplot = ap,
ax = ax1,
returnfig = True)
fig.savefig(savefig = file_specs) Where "ap" uses a second axis for volume, and "ax1" is the mean axis. "returnfig" seems not working as expected and thus "fig" is actually None. Did I miss anything here? |
Not sure, but if I am remember correctly it should work this way: fname = r'.\figure_001.png'
kwargs = {'dpi': 64,
'bbox_inches': 'tight',
'pad_inches': 0.0}
fig, axlist = mpf.plot(df_ohlc,
type = 'candle',
addplot = ap,
ax = ax1,
returnfig = True)
fig.savefig(fname,**kwargs) hth |
To make it simpler: try dropping all the info 'dpi, 'bbox_inches', 'pad_inches' and just pass the absolute path of your file's position into fig.savefig (and I am using forward slash / on my Mac: fig.savefig('/Users/...Plots/figure_1.png') and see if it works like this? Daniel will know more than me for sure... |
Hmmm, does not seem to be working either. I guess the key issue here is mpf.plot does not return anything and thus fig is actually None: TypeError: cannot unpack non-iterable NoneType object For completeness, the scripts that creates the figure and axis are added below (not sure if this makes any difference):
The figure is successfully plotted in the console though. Puzzled... |
Ok, you on Windows as you using backslashes to separate directories in file path, correct? I am not sure but you 'define' fig twice in your code:
I have only 2. in my code and my 'style' s, -defined beforehand- is applied into it (not in mpf.figure) like below: fig, axlist = mpf.plot(df, type='candle', axtitle=(ticker.ticker), ylabel='Price', ylabel_lower='Volume', Finally I do call fig.savefig('/Users/...Plots/figure_1.png') |
Yes, I am on Windows and have access to the drive. The issue, I think, is that "mpf.plot" does not return a figure handle when operating in external axis mode. Your scrips are working fine as they are operating on the "panel" mode. I tried that mode originally but realized it's difficult to precisely control the figure size when using "tight_layout" and thus switched to the "external axis mode". I am not sure if this is an intrinsic issue with the "external axis mode" as Daniel indicated and, if so, there is a solution. |
A few things: First
... exactly is not truly correct, as I corrected myself in my comment here where I indicated that with the Second
Do one or the other, but not both. Third That said, if all you are trying to accomplish is some specific sizing, and saving the figure, both of these things can be accomplished entirely with various kwargs to
Please try these things first (without savefig and without external axes), and/or describe exactly what you want to accomplish, what you want the end-result plot to look like, and I will do my best to provide you with a solution that will keep your code as simple as possible. Wishing you all the best, and thank you for your interest in mplfinance. --Daniel |
Per your suggestions above, @DanielGoldfarb, I played around with these features which I believe is a good way to learn. Now, I can get almost all features I need by using mpf.plot() without "external axes mode". As you suggested, scale_padding is a very useful feature in controlling the figure padding and figsize at the same time. The only thing that seems still missing is how to reduce the additional blank space as shown below.
Is there a more reliable and safer way to reduce the padding without clipping? Btw, there are a few coloring abnormalities which are highlighted in yellow circle in the second chart. |
Regarding the extra padding, there are a few things you can try:
|
Yes, I used axisoff=True in mpf.plot() for my plots, and they've been working as expected. It seems to me tight_layout is not a good choice for my application as it may clip data for certain dates. I will go with customized scale_padding for now as you suggested, which has been working fine. @DanielGoldfarb Thanks a lot for your help and appreciate your spending time during holidays! Wish you a happy New Year! P.S. Strangely, I couldn't reproduce the coloring abnormalities today with the same scripts. I will definitely revert if it happens again in the future. |
@ftnpang Thanks. Happy New Year to you and yours! All the best. --Daniel |
It seems that mpf.plot does not save plot to file when operating in external axes mode (by specifying "ax" argument). Is this a known issue or has been implemented. I am using '0.12.7a0' but no luck.
The text was updated successfully, but these errors were encountered: