I'm trying to plot three 2-dimensional arrays using imshow that share a single colorbar. Below is an example that works, but the third plot shows up smaller than the first two. How do I make all three plots the same size?
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
# Create three random 2-dimensional arrays of size 256x256
array1 = np.random.random((256, 256))
array2 = np.random.random((256, 256))
array3 = np.random.random((256, 256))
# Create a figure with three subplots
fig, axes = plt.subplots(1, 3, figsize=(15, 5))
# Plot each array using imshow in a subplot
im1 = axes[0].imshow(array1, cmap='viridis')
im2 = axes[1].imshow(array2, cmap='viridis')
im3 = axes[2].imshow(array3, cmap='viridis')
# Set titles for the subplots
axes[0].set_title('Array 1')
axes[1].set_title('Array 2')
axes[2].set_title('Array 3')
# Use make_axes_locatable to add a colorbar to the right of the last plot
divider = make_axes_locatable(axes[2])
cax = divider.append_axes("right", size="5%", pad=0.5)
cbar = plt.colorbar(im3, cax=cax)
# Adjust spacing between subplots
plt.tight_layout()
# Display the plots
plt.show()
Output: