Skip to content

plot_correlation


function backtide.analysis.correlation.plot_correlation(data, price_col="close", title=None, legend=None, figsize=(700, 600), filename=None, display=True)[source]

Create a correlation heatmap.

Computes pairwise Pearson correlation of period-over-period returns across symbols and displays the result as a heatmap. Requires data with at least two symbols.

Parameters

data : pd.DataFrame | pl.DataFrame

Input data containing columns symbol, the column specified by price_col, and dt with the datetime.

price_col : str, default="close"
Column name used to compute returns for correlation.

title : str | dict | None, default=None
Title for the plot.

legend : str | dict | None, default=None
Legend for the plot. Defaults to None since a colorbar is shown instead.

figsize : tuple[int, int] | None, default=(700, 600)
Figure's size in pixels, format as (x, y).

filename : str | Path | None, default=None
Save the plot using this name. The type of the file depends on the provided name (.html, .png, .pdf, etc...). If filename has no file type, the plot is saved as .html. If None, the plot isn't saved.

display : bool | None, default=True
Whether to render the plot. If None, it returns the figure.

Returns

Figure | None

The Plotly figure object. Only returned if display=None.


See Also

plot_drawdown

Create a drawdown chart.

plot_price

Create a price line chart.

plot_returns

Create a returns distribution histogram.


Example

>>> from backtide.storage import query_bars
>>> from backtide.analysis import plot_correlation

>>> df = query_bars(["AAPL", "MSFT"], "1d")
>>> plot_correlation(df)