Implementation:AI4Finance Foundation FinGPT Pyfolio Plotting
| Knowledge Sources | |
|---|---|
| Domains | Visualization, Portfolio_Analytics, Trading |
| Last Updated | 2026-10-18 00:00 GMT |
Overview
Concrete tool for generating matplotlib-based performance and risk visualizations (tear sheet plots) for trading strategy analysis provided by the vendored pyfolio package within the FinGPT Trading subproject.
Description
The plotting module contains dozens of plotting functions that visualize returns, drawdowns, positions, transactions, round-trip distributions, and factor exposures. It provides seaborn-style context management (`plotting_context`, `axes_style`) and a `customize` decorator that wraps each plotting function with consistent styling.
Usage
Import functions from this module when you need to render individual plots of a trading strategy's performance, or when building a custom tear sheet from scratch.
Code Reference
Source Location
- Repository: AI4Finance_Foundation_FinGPT
- File: fingpt/FinGPT_Others/FinGPT_Trading/chatgpt-trading-v2/pyfolio/plotting.py
- Lines: 1-1960
Signature
def customize(func):
"""Decorator to set plotting context and axes style during function call."""
def plotting_context(context='notebook', font_scale=1.5, rc=None): ...
def axes_style(style='darkgrid', rc=None): ...
def plot_returns(returns, live_start_date=None, ax=None, **kwargs): ...
def plot_drawdown_periods(returns, top=10, ax=None, **kwargs): ...
def plot_drawdown_underwater(returns, ax=None, **kwargs): ...
def plot_monthly_returns_heatmap(returns, ax=None, **kwargs): ...
def plot_annual_returns(returns, ax=None, **kwargs): ...
def plot_monthly_returns_dist(returns, ax=None, **kwargs): ...
def plot_return_values(returns, ax=None, **kwargs): ...
def plot_rolling_beta(returns, factor_returns, ax=None, **kwargs): ...
def plot_rolling_sharpe(returns, rolling_window=APPROX_BDAYS_PER_MONTH * 6, ax=None, **kwargs): ...
def plot_rolling_volatility(returns, rolling_window=APPROX_BDAYS_PER_MONTH * 6, ax=None, **kwargs): ...
def plot_excess_shadow_tearsheet(...): ...
def show_perf_stats(...): ...
def plot_holdings(positions, ax=None, **kwargs): ...
def plot_long_short_holdings(positions, ax=None, **kwargs): ...
def plot_turnover(transactions, positions, ax=None, **kwargs): ...
def plot_slippage_sweep(...): ...
def plot_capacity_sweep(...): ...
def plot_round_trip_life(...): ...
def show_worst_drawdown_periods(returns, top=5): ...
Import
from pyfolio.plotting import plot_returns, plot_drawdown_periods, plot_rolling_sharpe
from pyfolio.plotting import plotting_context, axes_style
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| returns | pd.Series | Yes | Daily noncumulative strategy returns |
| positions | pd.DataFrame | Varies | Daily holdings (for position plots) |
| transactions | pd.DataFrame | Varies | Trade data (for turnover plots) |
| factor_returns | pd.DataFrame | Varies | Benchmark/factor returns (for beta plots) |
| ax | matplotlib.Axes | No | Axes to draw on; created if None |
Outputs
| Name | Type | Description |
|---|---|---|
| ax | matplotlib.Axes | The axes containing the rendered plot |
Usage Examples
Plot Returns and Drawdowns
import pandas as pd
import matplotlib.pyplot as plt
from pyfolio.plotting import plot_returns, plot_drawdown_periods, plotting_context
# 1. Load strategy returns
returns = pd.read_csv("strategy_returns.csv", index_col=0, parse_dates=True)["returns"]
# 2. Render a returns + drawdown figure
fig, axes = plt.subplots(2, 1, figsize=(12, 8))
with plotting_context(font_scale=1.2):
plot_returns(returns, ax=axes[0])
plot_drawdown_periods(returns, ax=axes[1])
plt.tight_layout()
plt.savefig("tearsheet_returns.png")