matplotlib / mplfinance

Financial Markets Data Visualization using Matplotlib
https://pypi.org/project/mplfinance/
Other
3.68k stars 633 forks source link

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

Open mailtovickyrose opened 4 years ago

mailtovickyrose commented 4 years ago

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

DanielGoldfarb commented 4 years ago

Vicky,

HTH. --Daniel

dodid commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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).

dodid commented 4 years ago

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 commented 3 years ago