Search code examples
pythonplotfinancecandlestick-chart

Python: Plot candlesticks with automatic Y zoom


I am looking for a Python plotting library that allows me to plot candlesticks (preferably the OHLC bars variant) with X zoom via mousewheel scrolling (or similar) and an automatically scaled Y axis when zooming.

As an example of what I am looking for, tradingview.com does this perfectly. See https://uk.tradingview.com/chart/?symbol=NASDAQ:NDX. OHLC bars can be seen by clicking the candlestick icon near the top left and selecting 'Bars'.

Plotly is almost able to do this. The Ohlc class in plotly.graph_objs give the OHLC bars, and the default rangeslider is a nice feature for X zoom (mousewheel scrolling can also be easily enabled). However automatic Y scaling is not available in Python as far as I can see (Y-axis autoscaling with x-range sliders in plotly), thus zooming in on a section of data makes it appear flat. Example code - https://plot.ly/python/ohlc-charts/

Another option I am familiar with is PyQtGraph, which has nice zoom features but does not have support for candlestick plots. Using this would involve coding my own candlestick object.

There are a wide range of Python plotting libraries out there that I don't know. Is there anything out there that has out of the box support for this? Can anyone provide example code to do this cleanly?


Solution

  • This is how to do it in finplot:

    import yfinance as yf
    import finplot as fplt
    
    df = yf.download('^NDX', start='2018-01-01', end='2020-04-29')
    print(df)
    fplt.candlestick_ochl(df[['Open','Close','High','Low']])
    fplt.show()
    

    Disclaimer: I am the author. Finplot has auto-Y-scaling, is fast and opinionated with a clean api. See examples here.