File size: 1,914 Bytes
2dd33e7 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | #!/usr/bin/env python3
"""
Plot a single sample from the KdV-Burgers dataset.
"""
import numpy as np
import matplotlib.pyplot as plt
from burgers_dataset import KdvBurgersDataset
def plot_kdv_burgers_sample(sample, save_path="sample_plot.png"):
"""Plot a single sample from the KdV-Burgers dataset"""
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 5))
# Extract data using descriptive naming
spatial_coordinates = sample['spatial_coordinates']
u_initial = sample['u_initial']
u_trajectory = sample['u_trajectory']
time_coordinates = sample['time_coordinates']
# Plot initial condition
ax1.plot(spatial_coordinates, u_initial, 'b-', linewidth=2)
ax1.set_xlabel('x')
ax1.set_ylabel('u(x, t=0)')
ax1.set_title('Initial Condition')
ax1.grid(True, alpha=0.3)
ax1.set_xlim(0, spatial_coordinates[-1])
# Plot space-time evolution
im = ax2.pcolormesh(
spatial_coordinates,
time_coordinates,
u_trajectory,
cmap="RdBu_r",
shading="gouraud",
rasterized=True,
)
ax2.set_xlim(0, spatial_coordinates[-1])
ax2.set_ylim(0, time_coordinates[-1])
ax2.set_xlabel('x')
ax2.set_ylabel('t')
ax2.set_title("KdV-Burgers Evolution")
# Add colorbar
plt.colorbar(im, ax=ax2, label='u(x,t)')
plt.tight_layout()
plt.savefig(save_path, dpi=200, bbox_inches='tight')
plt.close()
print(f"Sample visualization saved to {save_path}")
if __name__ == "__main__":
# Set random seed for reproducibility
np.random.seed(42)
# Create dataset
dataset = KdvBurgersDataset()
# Generate a single sample
sample = next(iter(dataset))
print("Sample keys:", list(sample.keys()))
for key, value in sample.items():
print(f"{key}: shape {value.shape}")
# Plot the sample
plot_kdv_burgers_sample(sample) |