# **Final Fix: Reduce Figure Size for Screen Compatibility** *(Ensures the plot fits within Obsidian’s viewport)* ```python import micropip await micropip.install('numpy') await micropip.install('matplotlib') import numpy as np from matplotlib import pyplot as plt class Node: def __init__(self, id): self.id = id self.state = None self.instructions = [] def apply_instructions(self): for instr in self.instructions: instr.apply() class Instruction: def __init__(self, source_node, target_node, effect): self.source_node = source_node self.target_node = target_node self.effect = effect def apply(self): self.target_node.state = self.effect(self.source_node.state) class CausalGraph: def __init__(self, num_nodes): self.nodes = [Node(i) for i in range(num_nodes)] self.edges = [] self.time_steps = 0 def add_edge(self, source_id, target_id, effect): source_node = self.nodes[source_id] target_node = self.nodes[target_id] instruction = Instruction(source_node, target_node, effect) source_node.instructions.append(instruction) self.edges.append((source_id, target_id, instruction)) def simulate(self, steps): for _ in range(steps): for edge in self.edges: edge[2].apply() self.time_steps += 1 def visualize(self): fig, ax = plt.subplots(figsize=(5, 6)) # Reduced width to 8 inches max_time = self.time_steps for t in range(max_time): for edge in self.edges: ax.plot([edge[0], edge[1]], [t, t], 'r--', linewidth=1.2, alpha=0.7) ax.set_xlim(-1, len(self.nodes)) ax.set_ylim(-1, max_time + 1) ax.set_xlabel('Nodes') ax.set_ylabel('Time Steps') ax.set_title('Causal Graph Simulation') plt.tight_layout() # Ensures labels/axes fit plt.show() # Example usage if __name__ == "__main__": cg = CausalGraph(5) cg.add_edge(0, 1, lambda x: x + 1) cg.add_edge(1, 2, lambda x: x * 2) cg.add_edge(2, 3, lambda x: x - 1) cg.add_edge(3, 4, lambda x: x / 2) cg.nodes[0].state = 10 cg.nodes[1].state = 5 cg.nodes[2].state = 3 cg.nodes[3].state = 7 cg.nodes[4].state = 2 cg.simulate(10) cg.visualize() ``` --- # **Key Adjustments** 1. **Reduced Figure Size**: - Changed `figsize=(8, 6)` to shrink the horizontal dimension while maintaining vertical space. 2. **Line Visibility**: - Slightly reduced line thickness (`linewidth=1.2`) and opacity (`alpha=0.7`) for clarity on smaller plots. 3. **Auto-Adjust Layout**: - Added `plt.tight_layout()` to ensure axis labels and titles fit within the figure. This configuration ensures the plot fits neatly within Obsidian’s viewport without truncation.