You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Feature Request: Adjust a panel's x_lim and title padding #212
Thanks for reviving mplfinance and making it better! I like the new panel additions. They are awesome!
Is your feature request related to a problem? Please describe.
I was able to target mpf.make_addplot(ylim: tuple)) to adjust a panel's top and bottom ylim's so the panel's are not too close. Is it possible to at least set xlim=(mpf.minx, 0.9 * mpf.max)?
The bottom padding for title seems large. Possible bug? 🤷 Where self.title = "SPY (D)".
Here is the heart of the mpf plot code for the chart below:
Describe the solution you'd like
Ideally, I would like the ability to adjust at least the left padding of each panel. Or a tight_layout with an adjustable right padding for each panel.
First l'd like to say that's a great looking plot. I like what you've done with mplfinance: creative and useful.
Please try setting tight_layout=True in the call to mpf.plot( ) and let me know how that works for you. That should automatically adjust your x-axis limits, and should also fix the extra padding around the title at the top of the Figure ("SPY (D)") by placing the title slightly lower and decreasing the padding somewhat.
Beyond that, there are two ways that you can adjust the Figure padding surround the panels. It cannot be done per-panel, because by definition the panels are all the same width, and the padding relates to the Figure as a whole.
mpf.plot( ) has a kwarg scale_padding which can be set in one of two ways:
scale_padding=<number> where number is an integer or float greater than or less than 1.0 to either increase or decrease the amount of padding around all four sides of the Figure.
scale_padding=<dict> where dict contains one or more of the following keys: left, right, top, bottom and the value associated with the key is an integer or float greater than or less than 1.0 to either increase or decrease the amount of padding on the side of the Figure indicated by the dict key(s).
Please note: tight_layout and scale_padding are independent of each other, so you can use one or both, and they will each apply the effect they were coded to apply. Both will affect the padding, but only tight_layout will also affect the x-axis limits and the placement of the Figure title.
Not sure why scale_padding.left != 1 and scale_padding.right != 1 are ineffective. It could be that tight_layout scales them down so much that you would need to either:
(a) try scale_paddingwithouttight_layout, or
(b) make left and right padding significantly more than 1.0 to counteract the downscaling of padding by tight_layout.
Please try both (a) and (b) and let me know how it looks.
I will also consider adding a kwarg to adjust xlimits, since presently the only way to adjust xlimits is via tight_layout.
Thanks. Are any of those even close to what you are trying to accomplish? Perhaps the last one (except for maybe the exact placement of the Title)?
I just want to point out that I had never really intended to publicize scale_padding, but I am glad if it is helping. The intention was that scale_padding was a temporary kwarg to help me experiment while developing tight_layout. Afterwards I decided to leave the kwarg in place, "just in case" it might be useful for someone.
Ideally, there is adjustable padding/margin in the orange zone just like when tight_layout=False. Everything in the orange zone would be shifted left so there is some space to the right.
To adjust the amount of padding, modify the number you pass into df.tail()
And don't forget to add .copy() otherwise the nans may affect the original dataframe.
When I do the above on my own chart it looks like this:
It's funny that you posted that. Last night I was considering doing something similar, but by adding additional empty Business days to df then passing to plot() like you did.
Also it seems that vlines can only be applied to the main panel. But I'll make a seperate feature request so not to drag Issue 212 further.
I will post a chart update after some refactoring.
@twopirllc yes, there are already two issues requesting the various lines kwargs be available for make_addplot and on any panel.
will do it, but not yet sure how soon; maybe within the next two to three months. in the meantime, generating line data and plotting that data on any panel with make_addplot() is a mildly tedious, but workable, solution.
Sorry it has taken so long to get back to you with an example. I had to create some preliminary code in my project. Anyhow, here is a basic chart with right padding with future dates and the code I used:
classChart(object):
# ...def_right_pad_df(self, df: pd.DataFrame, rpad: int, delta_unit: str="D", range_freq: str="B"):
ifrpad>0:
dfpad=df[-rpad:].copy()
dfpad.iloc[:,:] =np.NaNdf_frequency=df.index.value_counts().mode()[0] # Most common frequencyfreq_delta=pd.Timedelta(df_frequency, unit=delta_unit)
new_dr=pd.date_range(start=df.index[-1] +freq_delta, periods=rpad, freq=range_freq)
dfpad.index=new_dr# Update the padded index with new datesdf=df.append(dfpad)
returndfdef_plot(self, df: pd.DataFrame, plot_ratios: tuple= (12, 1.7), **kwargs):
ifdf.empty:
print(f"[X] DataFrame is missing!")
returnifnotisinstance(plot_ratios, tuple):
print(f"[X] plot_ratios must be a tuple")
returnticker=df.tickerrpad=kwargs["rpad"] if"rpad"inkwargselseself.rpaddf=self._right_pad_df(df, rpad)
df=df.tail(self.last)
mpf_style=mpf.make_mpf_style(base_mpf_style=mpf_styles[-1], rc={'figure.facecolor': '#EDEDED'})
width_config={'candle_linewidth':0.8, 'candle_width':0.525, 'volume_width': 0.525}
mpf.plot(
df, type="candle", title=ticker,
style=mpf_style, datetime_format="%-m/%-d/%Y",
volume=True,
figsize=(12, 10), scale_padding={'left': 1, 'top': 4, 'right': 1, 'bottom': 1},
panel_ratios=plot_ratios, xrotation=15,
update_width_config=width_config, tight_layout=True,
)
Activity
DanielGoldfarb commentedon Jul 8, 2020
Kevin,
First l'd like to say that's a great looking plot. I like what you've done with mplfinance: creative and useful.
Please try setting
tight_layout=Truein the call tompf.plot( )and let me know how that works for you. That should automatically adjust your x-axis limits, and should also fix the extra padding around the title at the top of the Figure ("SPY (D)") by placing the title slightly lower and decreasing the padding somewhat.Beyond that, there are two ways that you can adjust the Figure padding surround the panels. It cannot be done per-panel, because by definition the panels are all the same width, and the padding relates to the Figure as a whole.
mpf.plot( )has a kwargscale_paddingwhich can be set in one of two ways:scale_padding=<number>where number is an integer or float greater than or less than 1.0 to either increase or decrease the amount of padding around all four sides of the Figure.scale_padding=<dict>where dict contains one or more of the following keys:left,right,top,bottomand the value associated with the key is an integer or float greater than or less than 1.0 to either increase or decrease the amount of padding on the side of the Figure indicated by the dict key(s).Please note:
tight_layoutandscale_paddingare independent of each other, so you can use one or both, and they will each apply the effect they were coded to apply. Both will affect the padding, but onlytight_layoutwill also affect the x-axis limits and the placement of the Figure title.HTH. All the best. --Daniel
twopirllc commentedon Jul 8, 2020
Hi Daniel,
Thank you for the suggestions. Here are some of my results:
Using
tight_layoutyields the following:
Using
tight_layoutandscale_paddingSo
scale_padding.top = 5helped adjust thetitle. Whereasscale_padding.left != 1andscale_padding.right != 1yield no meaningful results.yields the following:
That helps. That is the closest I can get to desired output, so I'll run with it. I am gonna refactor some code add it to my repo's example notebook.
Thanks again!
KJ
DanielGoldfarb commentedon Jul 8, 2020
Thank you for posting your results.
Not sure why scale_padding.left != 1 and scale_padding.right != 1 are ineffective. It could be that
tight_layoutscales them down so much that you would need to either:(a) try
scale_paddingwithouttight_layout, or(b) make left and right padding significantly more than 1.0 to counteract the downscaling of padding by
tight_layout.Please try both (a) and (b) and let me know how it looks.
I will also consider adding a kwarg to adjust xlimits, since presently the only way to adjust xlimits is via
tight_layout.Thanks. --Daniel
twopirllc commentedon Jul 8, 2020
Below are my results:
(A)
scale_paddingwithouttight_layout:(1)
scale_padding.left=4andtight_layout=False:yields:

(2)
scale_padding.right=4andtight_layout=False:yields:

(3)
scale_padding.left=4andscale_padding.right=4andtight_layout=False:yields:
(4)
scale_padding.left=2andscale_padding.right=2andtight_layout=False:yields:

(B)
scale_padding.left >> 1andscale_padding.right >> 1withtight_layout:(1)
scale_padding.left=4andscale_padding.right=4andtight_layout=True:yields:

(2)
scale_padding.left=2andscale_padding.right=2andtight_layout=True:yields:

Hope this helps!
Thank you,
KJ
DanielGoldfarb commentedon Jul 8, 2020
Thanks. Are any of those even close to what you are trying to accomplish? Perhaps the last one (except for maybe the exact placement of the Title)?
I just want to point out that I had never really intended to publicize
scale_padding, but I am glad if it is helping. The intention was thatscale_paddingwas a temporary kwarg to help me experiment while developingtight_layout. Afterwards I decided to leave the kwarg in place, "just in case" it might be useful for someone.twopirllc commentedon Jul 9, 2020
Sure.
Not any of the previous tests. This version is the closest:
yielding:

Ideally, there is adjustable padding/margin in the orange zone just like when
tight_layout=False. Everything in the orange zone would be shifted left so there is some space to the right.Hope this make sense.
Thanks for your time,
KJ
DanielGoldfarb commentedon Jul 9, 2020
Thank you. That makes sense. Until we have a
xlimitkwarg, you can add some padding on the right side as follows:To adjust the amount of padding, modify the number you pass into
df.tail()And don't forget to add
.copy()otherwise the nans may affect the original dataframe.When I do the above on my own chart it looks like this:
before padding:

after padding:

twopirllc commentedon Jul 9, 2020
@DanielGoldfarb,
Thanks for the suggested work around.
It's funny that you posted that. Last night I was considering doing something similar, but by adding additional empty Business days to
dfthen passing toplot()like you did.Also it seems that
vlinescan only be applied to the main panel. But I'll make a seperate feature request so not to drag Issue 212 further.I will post a chart update after some refactoring.
Thanks again!
KJ
DanielGoldfarb commentedon Jul 9, 2020
@twopirllc yes, there are already two issues requesting the various
lineskwargs be available formake_addplotand on any panel.will do it, but not yet sure how soon; maybe within the next two to three months. in the meantime, generating line data and plotting that data on any panel with
make_addplot()is a mildly tedious, but workable, solution.twopirllc commentedon Jul 17, 2020
Hello @DanielGoldfarb,
Sorry it has taken so long to get back to you with an example. I had to create some preliminary code in my project. Anyhow, here is a basic chart with right padding with future dates and the code I used:
Thanks again for your time and expertise.
Kevin
DanielGoldfarb commentedon Aug 9, 2020
kwarg
xlimis now available.pip install --upgrade mplfinancetwopirllc commentedon Aug 9, 2020
Great! Thank you for your time and excellence.
Regards,
Kevin