Skip to content
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

Bug Report: tight_layout should be applied after figsize #233

Closed
smjure opened this issue Aug 6, 2020 · 11 comments
Closed

Bug Report: tight_layout should be applied after figsize #233

smjure opened this issue Aug 6, 2020 · 11 comments
Labels
bug Something isn't working

Comments

@smjure
Copy link

smjure commented Aug 6, 2020

First of all thank you for awesome work... tight_layout and figsize features work smoothly when one only is applied. But when applied together, e.g. mpf.plot(data,type='candle',figsize=(16*2, 8*2),tight_layout=True) lots of white space 'reappear'. I assume that first tight_layout is applied and then figsize (which brings back lots of white space), but should be vice versa.

@smjure smjure added the bug Something isn't working label Aug 6, 2020
@DanielGoldfarb
Copy link
Collaborator

Please print(mpf.__version__) so I can look into this. Thank you.

@smjure
Copy link
Author

smjure commented Aug 6, 2020

Version is: 0.12.6a3. Thank you as well.

@DanielGoldfarb
Copy link
Collaborator

Please also do:

import matplotlib
print(matplotlib.get_backend())

@smjure
Copy link
Author

smjure commented Aug 6, 2020

Qt5Agg

@DanielGoldfarb
Copy link
Collaborator

DanielGoldfarb commented Aug 7, 2020

I have been playing around with this for a while now, and I am unable to reproduce what you say you are seeing.

When I set both figsize and tight_layout, there is still significantly less padding (what you call whitespace) in the Figure around the Axes. Furthermore, the code is exactly as you suggest that "tight_layout should be applied after figsize".

Note that tight_layout will always leave some padding around the Figure (particularly on the left side to leave room for the y-axis labels).

Also note that the amount of padding is not an absolute amount, but is rather a percentage, or fraction, of the entire Figure (and is, of course, a much smaller percentage when tight_layout=True). This means that the absolute amount of padding will of course be more when the figsize is larger, however the percentage will always be the same, and tight_layout=True gives less padding (a smaller percentage) surrounding the Axes regardless of whether figsize is set or not. This is the way it is coded and this is what I am seeing.

If you still think there is an issue, so that I can reproduce, and compare my results to yours, please provide:

  • data
  • code
  • images of resulting plots

Thank you. --Daniel

@smjure
Copy link
Author

smjure commented Aug 7, 2020

Thank you for looking into it. Please see the following pic created based on mpf on my monitor:
Screenshot from 2020-08-07 21-19-39

I had similar problem with matplotlib, which was simply solved. Since mpf is based on it, I supposed that the problem is similar. Please see the following code with a simple example:

import matplotlib.pyplot as plt
import numpy as np

#%% --- 1.set fig size  &  2.applty tight_layout
fig,ax = plt.subplots(figsize=(16*2,9*2)) # initi
y = np.random.normal(0,1,[100,1])
x = range(len(y))

ax.plot(x,y,'-.')
ax.set_title("1.set fig size  &  2.applty tight_layout") 
fig.tight_layout()
plt.show()

#%% --- 1.applty tight_layout  & 2.set fig size
fig1,ax1 = plt.subplots()#,figsize=(16,9)) # initi

ax1.plot(x,y,'-.') 
ax1.set_title("1.applty tight_layout  &  2.set fig size") 
fig1.tight_layout()

fig1.set_size_inches(16*2,9*2) # enlarge current fig
plt.show()

I hope it helps a bit in clarification of what I mean.

@DanielGoldfarb
Copy link
Collaborator

DanielGoldfarb commented Aug 7, 2020

As I said, the code is setting tight_layout after figsize, as you suggest. And your image appears to me as if tight_layout is indeed applied as I would expect. If you would also show an image with the same figsize but with no tight_layout, you should see a lot more white space without tight_layout; but please definitely include such an image if that is not the case..

There are a couple items to keep in mind here. Using the underlying matplotlib Figure.tight_layout() does not work for the type of Axes that mplfinance creates (i.e. using Figure.add_axes(); if you try it, matplotlib will complain). Therefore mplfinance has to do its own tight_layout implementation. As part of that implementation, as I mentioned, mplfinance leaves perhaps a little more space on the left side of the plot than is necessary, and I may improve this in the future. But even as is, mplfinance's tight_layout implementation results in significantly less space than when tight_layout is not set to True, regardless of whether or not you set figsize.

So I honestly do not see any "white space 'reappear'" If you have data or evidence otherwise, please show it.

Now, if you are not satisfied with mplfinance's implementation of tight_layout, there is a kwarg that allows you scale the amount of padding (both with, or without, tight_layout). Please see this comment for information on how to do that.

  • Please let me know if you have any concrete evidence to show "white space 'reappear'". I am happy to fix any bug in the code, but presently I don't see any evidence that figsize is somehow disabling or diminishing tight_layout, as I believe you are suggesting.

  • Please also let me know whether or not the scale_padding kwarg helps you to accomplish what you want.

All the best. --Daniel

@smjure
Copy link
Author

smjure commented Aug 7, 2020

The scale_padding solves the problem, thank you very much!

@smjure smjure closed this as completed Aug 7, 2020
@fxhuhn
Copy link
Contributor

fxhuhn commented Aug 8, 2020

Thank you for looking into it. Please see the following pic created based on mpf on my monitor:

Screenshot from 2020-08-07 21-19-39

Please see the following code with a simple example:

How do you placed the numbers in the chart?

@petric3
Copy link

petric3 commented Aug 8, 2020

See: #231.

@DanielGoldfarb
Copy link
Collaborator

@fxhuhn
Markus,
In addition to #231 , there is also an example of adding text here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants