mplfinance icon indicating copy to clipboard operation
mplfinance copied to clipboard

how to add x-axis and y-axis limits(like xlim and ylim in matplotlib)

Open mailtovickyrose opened this issue 4 years ago • 6 comments

how to add x-axis and y-axis limits(like xlim and ylim in matplotlib)

mailtovickyrose avatar Aug 23 '20 05:08 mailtovickyrose

Vicky,

  • Method mpf.plot() accepts kwargs xlim= and ylim= where the value must be a len=2 tuple (min,max).

  • Method mpf.make_addplot() similarly accepts kwarg ylim=.
    (Since all panels share the same x-axes, there is no need for addplot to accept xlim.)

HTH. --Daniel

DanielGoldfarb avatar Aug 23 '20 18:08 DanielGoldfarb

Setting xlim gives me this error:

xlim = (ohlc.index[0], ohlc.index[-1])
mpf.plot(ohlc, type='candle', figsize=(17, 5), style='yahoo', show_nontrading=True, xlim=xlim)
TypeError                                 Traceback (most recent call last)
<ipython-input-15-6ef2295d5295> in <module>
      1 xlim = (ohlc.index[0], ohlc.index[-1])
----> 2 mpf.plot(ohlc, type='candle', figsize=(17, 5), style='yahoo', show_nontrading=True, xlim=xlim)

~\Anaconda3\envs\ib\lib\site-packages\mplfinance\plotting.py in plot(data, **kwargs)
    271     """
    272 
--> 273     config = _process_kwargs(kwargs, _valid_plot_kwargs())
    274 
    275     dates,opens,highs,lows,closes,volumes = _check_and_prepare_data(data, config)

~\Anaconda3\envs\ib\lib\site-packages\mplfinance\_arg_validators.py in _process_kwargs(kwargs, vkwargs)
    241                 import inspect
    242                 v = inspect.getsource(vkwargs[key]['Validator']).strip()
--> 243                 raise TypeError('kwarg "'+key+'" validator returned False for value: "'+str(value)+'"\n    '+v)
    244 
    245        # ---------------------------------------------------------------

TypeError: kwarg "xlim" validator returned False for value: "(Timestamp('2020-08-28 09:30:01-0400', tz='US/Eastern', freq='S'), Timestamp('2020-08-28 16:00:00-0400', tz='US/Eastern', freq='S'))"
    'Validator'    : lambda value: isinstance(value, (list,tuple)) and len(value) == 2

while

isinstance(xlim, (list,tuple)) and len(xlim) == 2
True

dodid avatar Oct 11 '20 16:10 dodid

Also it's better not to have any margin along x axis by default, then we don't need to manually set xlim. Another issue is that the xtick label ignores the timezone setting and show hours in UTC. So US market open becomes 13:30.

dodid avatar Oct 11 '20 16:10 dodid

Setting xlim gives me this error ... TypeError: kwarg "xlim" validator returned False for value: "(Timestamp('2020-08-28 09:30:01-0400', tz='US/Eastern', freq='S'), Timestamp('2020-08-28 16:00:00-0400', tz='US/Eastern', freq='S'))" 'Validator' : lambda value: isinstance(value, (list,tuple)) and len(value) == 2

I will look into that. It seems like it should work. If you can provide your actual data, and show the code where you are specifically setting the xlim variable, that would be helpful.


Also it's better not to have any margin along x axis by default

That is your opinion. Not everyone would agree.
Also, instead of setting xlim, you can set tight_layout=True to remove x-axis margins


Another issue is that the xtick label ignores the timezone setting and show hours in UTC.

This bug was already reported here: https://github.com/matplotlib/mplfinance/issues/236 and will be fixed (hopeful relatively soon).

DanielGoldfarb avatar Oct 11 '20 23:10 DanielGoldfarb

Here are the code and data. Thanks for the workaround.

xlim = (ohlc.index[0], ohlc.index[-1])
mpf.plot(ohlc, type='candle', figsize=(17, 5), style='yahoo', show_nontrading=True, xlim=xlim)

ohlc.zip

dodid avatar Oct 12 '20 00:10 dodid

  • Added bug label to look into the error reported here: https://github.com/matplotlib/mplfinance/issues/245#issuecomment-706731127

  • Enhancment and In Progress labels because currently working on accepting any of various date/datetime types and/or string representations of dates/datetimes for the xlim specification.

DanielGoldfarb avatar Apr 05 '21 15:04 DanielGoldfarb