Cufflinks Documentation

repository·master·Indexed 25 days ago

https://github.com/santosjorge/cufflinks

A library that bridges Pandas and Plotly, enabling the creation of interactive visualizations directly from Pandas DataFrames. Features include the QuantFig class for technical analysis (SMA, RSI, Bollinger Bands, MACD), interactive date range sliders and selectors, custom chart annotations, and a comprehensive cf.colors module for color conversion and palette generation. Supports both online and offline Plotly modes and provides utilities like cf.to_df() to extract data from Plotly Figures back into Pandas.

Tokens
2K
Snippets
7
Records
16
Agent score
83%

What's inside Cufflinks

  1. Set global configuration and themes

    master

    You can configure global settings for all charts generated by cufflinks:

    • Themes: Use cufflinks.set_config_file(theme='theme_name') to set a global theme (e.g., 'pearl', 'ggplot'). You can also pass a theme argument directly to .iplot().
    • World Readable: Use cufflinks.set_config_file(world_readable=True) for public charts.
    • Config File: Use cufflinks.set_config_file() to manage various global settings.
  2. Use QuantFigure for persistent graph objects and technical analysis

    master

    The QuantFig class allows you to create a graph object that maintains state, enabling you to add technical analysis studies or modify parameters incrementally before plotting.

    Basic Usage

    Initialize QuantFig with a DataFrame and then call methods like add_bollinger_bands() before calling .iplot().

    Adding Technical Analysis Studies

    You can add various studies to a QuantFig instance:

    • add_sma(periods, width, color, legendgroup): Simple Moving Average.
    • add_rsi(periods, color): Relative Strength Index.
    • add_bollinger_bands(periods, boll_std, colors, fill): Bollinger Bands.
    • add_volume(): Add volume bars.
    • add_macd(): Moving Average Convergence Divergence.
    df=cf.datagen.ohlc()
    qf=cf.QuantFig(df,title='First Quant Figure',legend='top',name='GS')
    qf.add_bollinger_bands()
    qf.iplot()
    
    # Adding more studies
    qf.add_sma([10,20],width=2,color=['green','lightgreen'],legendgroup=True)
    qf.add_rsi(periods=20,color='java')
    qf.add_bollinger_bands(periods=20,boll_std=2,colors=['magenta','grey'],fill=True)
    qf.add_volume()
    qf.add_macd()
    qf.iplot()
  3. Customize chart annotations

    master

    You can add annotations to your plots using two modes:

    1. Label Mode: Pass a dictionary to annotations where keys are dates/labels and values are the text. Use textangle, fontsize, and fontcolor to style them.
    2. Explicit Mode: Pass a list of dictionaries to annotations for precise control over position (x, y), reference (xref), arrowheads, and text styling.
  4. Configure date range sliders and selectors

    master

    Cufflinks supports interactive date range controls for time-series plots:

    • rangeslider: Displays a date range slider at the bottom of the chart. Set rangeslider=True in .iplot().
    • rangeselector: Displays buttons to quickly change the displayed date range. Pass a dictionary to the rangeselector parameter in .iplot() containing 'steps', 'bgcolor', 'x', and 'y'.
    # Enable rangeslider
    cf.datagen.ohlc().iplot(kind='candle',rangeslider=True)
    
    # Enable rangeselector
    cf.datagen.ohlc(500).iplot(kind='candle', rangeselector={ 'steps':['1y','2 months','5 weeks','ytd','2mtd','reset'], 
         'bgcolor' : ('grey',.3), 'x': 0.3 , 'y' : 0.95})
  5. Manage Plotly connection modes (Online vs Offline)

    master

    Cufflinks allows you to switch between online and offline Plotly modes:

    • cf.go_offline(): Switch to offline mode.
    • cf.go_online(): Switch to online mode.
    • cf.iplot(figure, online=True): Force online mode for a specific plot even if the global setting is offline.
  6. Explore supported figures and parameters with cf.help()

    master

    You can inspect the available figure types and the specific parameters supported by each figure using the cf.help() method.

    • Call cf.help() to see a comprehensive list of all supported figures.
    • Call cf.help('figure_name') to see the parameters available for a specific figure (e.g., 'scatter', 'candle').
    # For a list of supported figures
    cf.help()
    
    # Or to see the parameters supported that apply to a given figure try
    cf.help('scatter')
    cf.help('candle') #etc
  7. Convert between color formats with `cf.colors`

    master

    The cf.colors module provides utilities to convert colors between HEX, RGB, RGBA, and named color formats.

    • Named Colors: Access a list of pre-defined color names using cf.colors.cnames.
    • HEX to RGB: Use cf.colors.hex_to_rgb(color).
    • RGB to HEX: Use cf.colors.rgb_to_hex(color).
    • To RGBA (Transparency): Use cf.colors.to_rgba(color, alpha) where alpha is the transparency level.
    • RGBA to RGB (Flattening): Use cf.colors.rgba_to_rgb(rgba_color, transparency_color) to flatten transparency. By default, the transparency color is assumed to be 'white'.
  8. Add technical indicators to QuantFig

    master

    Enhance a QuantFig object by adding various technical indicators before calling .iplot(). Supported indicators include:

    • add_sma(periods, width, color, legendgroup): Adds Simple Moving Averages.
    • add_rsi(periods, color): Adds Relative Strength Index.
    • add_bollinger_bands(periods, boll_std, colors, fill): Adds Bollinger Bands.
    • add_volume(): Adds volume bars.
    • add_macd(): Adds Moving Average Convergence Divergence.
    qf.add_sma([10,20],width=2,color=['green','lightgreen'],legendgroup=True)
    qf.add_rsi(periods=20,color='java')
    qf.add_bollinger_bands(periods=20,boll_std=2,colors=['magenta','grey'],fill=True)
    qf.add_volume()
    qf.add_macd()
    qf.iplot()