Plot Utilities#
Plot Utils Module#
This module provides utility functions for creating and manipulating matplotlib figures, particularly for side-by-side plot comparisons with support for 2D plots, 3D plots, heat maps, and shared colorbars.
The module is designed to work with the InflGame package and supports creating publication-quality
figures with consistent styling and formatting.
Dependencies:#
matplotlib
numpy
Usage:#
The side_by_side_plots function can be used to combine two existing plots into a single figure with optional shared colorbars and axis labels.
Example:#
from InflGame.utils.plot_utils import side_by_side_plots
import matplotlib.pyplot as plt
import numpy as np
# Create two example plots
fig1, ax1 = plt.subplots()
ax1.plot([1, 2, 3], [1, 4, 9])
ax1.set_title("Plot 1")
fig2, ax2 = plt.subplots()
ax2.plot([1, 2, 3], [1, 2, 3])
ax2.set_title("Plot 2")
# Combine them side by side
combined_fig = side_by_side_plots(
ax1, ax2,
title_main="Combined Plots",
cbar_params={'common_cbar': False},
axis_params={'common_axis': True, 'axis_xlabel': 'X-axis', 'axis_ylabel': 'Y-axis'}
)
combined_fig.show()
Functions
- InflGame.utils.plot_utils.side_by_side_plots(ax1, ax2, title_main, title_ads=[], cbar_params={'cbar_title': '', 'common_cbar': False}, axis_params={'axis_xlabel': '', 'axis_ylabel': '', 'common_axis': False}, font={'cbar_size': 12, 'default_size': 12, 'font_family': 'sans-serif', 'legend_size': 12, 'title_size': 14})#
Create a side-by-side comparison figure from two existing plot axes.
This function copies plot elements from two source axes and combines them into a single figure with two subplots placed side by side. It handles various plot types including line plots, scatter plots (2D and 3D), heat maps, contour plots, and preserves colorbars.
The function supports:
Line plots: Standard 2D line plots with all styling preserved
Scatter plots: Both 2D and 3D scatter plots with colors and sizes
Heat maps: Image plots (imshow, contourf, pcolormesh) with discrete colorbars
Contour plots: Filled contour plots with proper styling
3D plots: Full 3D plot support with axis labels and limits
Colorbars: Individual or shared colorbars with discrete levels
- Parameters:
- ax1plt.Axes
The first source axes to copy from (left subplot in output).
- ax2plt.Axes
The second source axes to copy from (right subplot in output).
- title_mainstr
Main title for the combined figure.
- title_adslist, optional
Additional title components to append to the main title, by default [].
- cbar_paramsdict, optional
Colorbar configuration dictionary, by default
{'common_cbar': False, 'cbar_title': ''}.'common_cbar'(bool): If True, create a single shared colorbar; if False, create individual colorbars'cbar_title'(str): Title/label for the colorbar
- axis_paramsdict, optional
Axis configuration dictionary, by default
{'common_axis': False, 'axis_ylabel': '', 'axis_xlabel': ''}.'common_axis'(bool): If True, use shared axis labels'axis_ylabel'(str): Common y-axis label'axis_xlabel'(str): Common x-axis label
- fontdict, optional
Font configuration dictionary, by default
{'default_size': 12, 'cbar_size': 12, 'title_size': 14, 'legend_size': 12, 'font_family': 'sans-serif'}.'default_size'(int): Default font size for general text'cbar_size'(int): Font size for colorbar tick labels'title_size'(int): Font size for figure title'legend_size'(int): Font size for legend text'axis_size'(int): Font size for axis labels'font_family'(str): Font family (e.g., ‘sans-serif’, ‘serif’)
- Returns:
- matplotlib.figure.Figure
The new figure containing both plots arranged side by side.
Notes
The function creates discrete colorbars with centered labels for heat maps
3D plots are properly detected and handled with appropriate projection settings
All axis limits, tick positions, and labels are preserved from the source plots
The function closes the original figures to prevent memory leaks
Examples
>>> import matplotlib.pyplot as plt >>> import numpy as np >>> >>> # Create two sample plots >>> fig1, ax1 = plt.subplots() >>> x = np.linspace(0, 10, 100) >>> ax1.plot(x, np.sin(x)) >>> ax1.set_title("Sine Wave") >>> >>> fig2, ax2 = plt.subplots() >>> ax2.plot(x, np.cos(x)) >>> ax2.set_title("Cosine Wave") >>> >>> # Combine with shared x-axis label >>> combined = side_by_side_plots( ... ax1, ax2, ... title_main="Trigonometric Functions", ... axis_params={'common_axis': True, 'axis_xlabel': 'x', 'axis_ylabel': 'f(x)'} ... )