rhoa.plots
Visualization module for stock prediction analysis.
This module provides a pandas DataFrame accessor for creating publication-quality visualizations of stock prediction models. It includes methods for plotting buy/sell signals, confusion matrices, and performance metrics overlaid on price charts.
The visualizations are designed to help interpret model predictions, identify false positives/negatives, and understand prediction quality at a glance.
Examples
Basic usage with the DataFrame accessor:
>>> import pandas as pd
>>> import rhoa
>>> df = rhoa.read_csv('stock_data.csv')
>>> df['Date'] = pd.to_datetime(df['Date'])
>>> # Visualize predictions
>>> fig = df.rhoa.plots.signal(y_pred=predictions, y_true=targets)
Notes
All plotting methods return matplotlib Figure objects that can be further customized using standard matplotlib/seaborn APIs.
See also
rhoa.targetsModule for generating target labels for stock prediction
Classes
Pandas DataFrame accessor for stock prediction visualization. |
- class PlotsAccessor[source]
Pandas DataFrame accessor for stock prediction visualization.
This accessor provides methods for creating professional visualizations of stock prediction model results. It is automatically available on any pandas DataFrame through the .plots attribute after importing rhoa.
The accessor specializes in visualizing: - Buy/sell signals overlaid on price charts - Confusion matrices for classification performance - False positive and false negative analysis - Model performance metrics (precision, recall)
- _df
The DataFrame instance this accessor is attached to. Should contain stock price data with at minimum a date column and price column.
- Type:
Examples
Access the plotting methods through any DataFrame:
>>> import pandas as pd >>> import rhoa >>> df = rhoa.read_csv('stock_data.csv') >>> # The .plots accessor is now available >>> fig = df.rhoa.plots.signal(y_pred=predictions)
Notes
This accessor is registered automatically when rhoa is imported. The DataFrame must contain the columns specified in each method’s parameters (typically ‘Date’ and ‘Close’ at minimum).
See also
rhoa.targetsGenerate target labels for predictions
pandas.api.extensions.register_dataframe_accessorpandas accessor registration
- __init__(pandas_obj)[source]
Initialize the plots accessor.
- Parameters:
pandas_obj (pandas.DataFrame) – The DataFrame instance to attach the accessor to.
- signal(y_pred, y_true=None, date_col='Date', price_col='Close', threshold=None, title=None, figsize=(18, 10), cmap='Blues', save_path=None, dpi=300, show=True, **kwargs)[source]
Visualize stock predictions with buy signals overlaid on price chart.
Creates a publication-quality visualization showing model predictions on a stock price chart. The visualization includes the price time series with predicted buy signals marked as bright green dots. When ground truth labels are provided, the plot includes a confusion matrix panel and marks false positives (red X) and false negatives (orange circles) on the chart.
This method is designed for evaluating machine learning models for stock trading by providing immediate visual feedback on prediction quality, timing of signals, and model errors in temporal context.
- Parameters:
y_pred (array-like of shape (n_samples,)) – Binary predictions array (0 or 1) indicating predicted buy signals. Must have the same length as the DataFrame. Typically output from a classification model like RandomForest, XGBoost, or neural network. Values should be 0 (no buy signal) or 1 (buy signal).
y_true (array-like of shape (n_samples,), optional) – Ground truth binary labels (0 or 1) for validation. When provided, the visualization adds: - A confusion matrix panel showing TP, FP, TN, FN - Light green background dots for true buy opportunities - Red X markers for false positive predictions - Orange circles for missed opportunities (false negatives) Default is None (predictions only, no validation).
date_col (str, default='Date') – Name of the date/datetime column in the DataFrame. This column is used for the x-axis of the price chart. The column should be of datetime type or convertible to datetime.
price_col (str, default='Close') – Name of the price column to plot on the y-axis. Typically ‘Close’ for closing prices, but can be ‘Open’, ‘High’, ‘Low’, or any other price column. The signals will be plotted at this price level.
threshold (float, optional) – The prediction probability threshold that was used to generate y_pred. This is displayed in the plot title for reference. For example, if you used y_pred = (y_pred_proba >= 0.67).astype(int), set threshold=0.67. Default is None (threshold not shown).
title (str, optional) – Custom title prefix for the plot. If None, generates an automatic title based on the price_col. The title will be expanded with “ - Confusion Matrix” or “ - Price with Buy Signals” depending on the panel. Default is None.
figsize (tuple of int, default=(18, 10)) – Figure size as (width, height) in inches. Large default size ensures readability with multiple panels and detailed annotations. Adjust for different display sizes or publication requirements.
cmap (str, default='Blues') – Matplotlib colormap name for the confusion matrix heatmap. Options: - ‘Blues’: Professional blue gradient (recommended) - ‘Greens’: Green gradient for emphasizing positive signals - ‘Reds’: Red gradient (use cautiously) - Any valid matplotlib colormap name
save_path (str, optional) – File path to save the figure. Supports common formats: .png, .jpg, .pdf, .svg. If None, the figure is not saved to disk. Directory must exist. Default is None.
dpi (int, default=300) – Resolution (dots per inch) for saved figures. 300 dpi is suitable for publications. Use 150 for presentations, 600+ for print. Only applies when save_path is provided.
show (bool, default=True) – Whether to display the plot using plt.show(). Set to False if you want to further customize the figure or prevent display in non- interactive environments.
**kwargs (dict, optional) – Additional keyword arguments passed to matplotlib for advanced customization. Currently not used but reserved for future extensions.
- Returns:
fig – The created figure object containing one or two subplots depending on whether y_true is provided. Can be further customized using standard matplotlib methods or saved to different formats.
- Return type:
matplotlib.figure.Figure
- Raises:
ValueError – If the specified date_col is not found in the DataFrame.
ValueError – If the specified price_col is not found in the DataFrame.
ValueError – If the length of y_pred does not match the DataFrame length.
ValueError – If y_true is provided but its length does not match the DataFrame.
See also
rhoa.targets.future_returnGenerate target labels based on future returns
rhoa.targets.drawdownGenerate target labels based on drawdown thresholds
matplotlib.pyplot.savefigSave the figure to file
sklearn.metrics.confusion_matrixCompute confusion matrix
Notes
Visualization Components:
When y_true is None (predictions only): - Single panel showing price chart with predicted buy signals (bright green) - No confusion matrix or error markers
When y_true is provided (validation mode): - Top panel: Confusion matrix with counts, percentages, and metrics - Bottom panel: Price chart with all signal types:
Light green background: True buy opportunities (ground truth)
Bright green dots: Model predicted buy signals
Red X markers: False positives (wrong predictions)
Orange circles: False negatives (missed opportunities)
Interpreting the Visualization:
Dense bright green clusters: Model is actively predicting buy signals
Green dots on light green background: True positives (correct predictions)
Red X markers: False alarms - model predicted buy but shouldn’t have
Orange circles: Missed trades - model failed to predict real opportunities
Gaps in signals: Periods where model predicts no buy opportunities
Best Practices:
Always provide y_true during model development for full diagnostics
Look for temporal patterns in false positives (e.g., during volatility)
Check if false negatives occur at specific price levels or market conditions
Compare precision/recall from confusion matrix with your trading strategy
Use threshold parameter to document the decision boundary
Save high-quality versions (dpi=300+) for documentation
Performance Metrics:
The confusion matrix panel displays: - Precision: Of all buy signals, what percentage were correct? - Recall: Of all true opportunities, what percentage did we catch? - Counts: Absolute numbers of TP, FP, TN, FN
Examples
Visualize predictions with full validation metrics:
>>> import pandas as pd >>> import numpy as np >>> import rhoa >>> >>> # Load stock data >>> df = pd.read_csv('AAPL_stock_data.csv') >>> df['Date'] = pd.to_datetime(df['Date']) >>> >>> # Generate targets using rhoa (7% return threshold) >>> targets = df.targets.future_return( ... threshold=0.07, ... holding_period=10, ... return_type='pct' ... ) >>> >>> # Assume you have model predictions >>> predictions = model.predict(features) >>> >>> # Create comprehensive visualization >>> fig = df.rhoa.plots.signal( ... y_pred=predictions, ... y_true=targets, ... threshold=0.67, ... title='AAPL Random Forest Model', ... save_path='outputs/aapl_predictions.png', ... dpi=300 ... )
Visualize predictions only (no ground truth available):
>>> # When you don't have labels (e.g., predicting future) >>> fig = df.rhoa.plots.signal( ... y_pred=predictions, ... date_col='Date', ... price_col='Close', ... title='AAPL Future Predictions', ... cmap='Greens' ... )
Customize for different price columns:
>>> # Use opening prices instead of closing >>> fig = df.rhoa.plots.signal( ... y_pred=predictions, ... y_true=targets, ... price_col='Open', ... title='Entry Signals (Open Prices)' ... )
Save without displaying (batch processing):
>>> # Useful for generating reports for multiple stocks >>> for ticker in ['AAPL', 'GOOGL', 'MSFT']: ... df = load_stock_data(ticker) ... predictions = model.predict(df) ... fig = df.rhoa.plots.signal( ... y_pred=predictions, ... save_path=f'reports/{ticker}_signals.png', ... show=False # Don't display, just save ... ) ... plt.close(fig) # Free memory
Compare different thresholds visually:
>>> # Generate predictions at different thresholds >>> proba = model.predict_proba(features)[:, 1] >>> >>> for thresh in [0.5, 0.67, 0.8]: ... preds = (proba >= thresh).astype(int) ... fig = df.rhoa.plots.signal( ... y_pred=preds, ... y_true=targets, ... threshold=thresh, ... title=f'Model Threshold {thresh}', ... save_path=f'outputs/threshold_{thresh}.png', ... show=False ... )