Skip to content

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

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

Open
mailtovickyrose opened this issue Aug 23, 2020 · 6 comments
Labels
bug Something isn't working enhancement New feature or request in progress question Further information is requested

Comments

@mailtovickyrose
Copy link

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

@mailtovickyrose mailtovickyrose added the question Further information is requested label Aug 23, 2020
@DanielGoldfarb
Copy link
Collaborator

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

@dodid
Copy link

dodid commented Oct 11, 2020

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
Copy link

dodid commented Oct 11, 2020

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.

@DanielGoldfarb
Copy link
Collaborator

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: #236 and will be fixed (hopeful relatively soon).

@dodid
Copy link

dodid commented Oct 12, 2020

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

@DanielGoldfarb DanielGoldfarb added bug Something isn't working enhancement New feature or request in progress labels Apr 5, 2021
@DanielGoldfarb
Copy link
Collaborator

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request in progress question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants