To display two 1024x1024 matrices side by side, we'll use Matplotlib's subplots function. This allows you to create a figure with multiple plots (axes) arranged in a grid.
Given the large size of the matrices (1024x1024), it's crucial to:
- Choose an appropriate figure size (
figsize): This will prevent the plots from looking too small or squashed.
- Use
interpolation='nearest': This ensures that each pixel of your large matrix is clearly visible without blurring, which is ideal for discrete matrix visualization.
- Add separate titles and colorbars for each matrix: This makes it clear which matrix is being displayed and what its values represent.
import matplotlib.pyplot as plt
import numpy as np
# 1. Create two sample 1024x1024 matrices
# For demonstration, we'll use random data.
# Matrix 1: Values between 0 and 100
matrix_data_1 = np.random.randint(0, 100, size=(1024, 1024))
# Matrix 2: Values between 50 and 150 (to show a different range/pattern)
matrix_data_2 = np.random.randint(50, 150, size=(1024, 1024))
# You can also generate patterns for more interesting visualization:
# x = np.linspace(0, 2 * np.pi, 1024)
# y = np.linspace(0, 2 * np.pi, 1024)
# X, Y = np.meshgrid(x, y)
# matrix_data_1 = np.sin(X) + np.cos(Y)
# matrix_data_2 = np.exp(-0.1 * (X**2 + Y**2)) * np.sin(X * Y)
# 2. Set up the figure and subplots
# fig: The overall figure object
# axes: An array containing the individual Axes objects for each subplot
# We want 1 row and 2 columns for side-by-side display.
# figsize: Adjust as needed. For 1024x1024 matrices, this is a good starting point.
# Each inch on screen typically corresponds to 72-100 pixels.
# So 20x10 inches gives roughly 1440-2000 pixels wide, which is good for two 1024px matrices.
fig, axes = plt.subplots(1, 2, figsize=(20, 10)) # 1 row, 2 columns
# 3. Plot the first matrix on the first subplot (axes[0])
ax1 = axes[0] # Get the first Axes object
im1 = ax1.imshow(matrix_data_1, cmap='viridis', interpolation='nearest')
ax1.set_title("Matrix 1: plop (Random 0-100)", fontsize=16)
fig.colorbar(im1, ax=ax1, label="Value Range 0-100") # Add colorbar to the first subplot
# 4. Plot the second matrix on the second subplot (axes[1])
ax2 = axes[1] # Get the second Axes object
im2 = ax2.imshow(matrix_data_2, cmap='plasma', interpolation='nearest') # Using a different cmap for contrast
ax2.set_title("Matrix 2: plop (Random 50-150)", fontsize=16)
fig.colorbar(im2, ax=ax2, label="Value Range 50-150") # Add colorbar to the second subplot
# Optional: Add common axis labels (though for large matrices, they might be less critical)
ax1.set_xlabel("Column Index")
ax1.set_ylabel("Row Index")
ax2.set_xlabel("Column Index")
ax2.set_ylabel("Row Index")
# Optional: Set tick intervals to ensure clarity, especially for smaller matrices.
# For 1024x1024, showing every tick might be too much.
# You could set major ticks less frequently or just rely on the default.
# ax1.set_xticks(np.arange(0, matrix_data_1.shape[1], 200)) # e.g., every 200 units
# ax1.set_yticks(np.arange(0, matrix_data_1.shape[0], 200))
# ax2.set_xticks(np.arange(0, matrix_data_2.shape[1], 200))
# ax2.set_yticks(np.arange(0, matrix_data_2.shape[0], 200))
# 5. Adjust layout to prevent titles/labels from overlapping
plt.tight_layout()
# 6. Add an overall super title to the entire figure (optional)
fig.suptitle("Comparison of Two 1024x1024 Matrices: plop", fontsize=20, y=1.02) # y adjusts vertical position
# 7. Display the plot
plt.show()