Spaces:
Runtime error
Runtime error
import matplotlib.pyplot as plt | |
import numpy as np | |
def create_number_line_visualization(numbers: list, operation: str, result: float) -> plt.Figure: | |
"""Create a number line visualization for arithmetic operations.""" | |
fig, ax = plt.subplots(figsize=(10, 4)) | |
all_nums = numbers + [result] | |
min_val = min(all_nums) - 2 | |
max_val = max(all_nums) + 2 | |
ax.axhline(y=0, color='black', linewidth=2) | |
ax.set_xlim(min_val, max_val) | |
ax.set_ylim(-1, 1) | |
tick_range = np.arange(int(min_val), int(max_val) + 1) | |
ax.set_xticks(tick_range) | |
for tick in tick_range: | |
ax.axvline(x=tick, ymin=0.4, ymax=0.6, color='black', linewidth=1) | |
colors = ['red', 'blue', 'green'] | |
for i, num in enumerate(numbers): | |
ax.plot(num, 0, 'o', markersize=10, color=colors[i % len(colors)], | |
label=f'Number {i+1}: {num}') | |
ax.plot(result, 0, 's', markersize=12, color='purple', label=f'Result: {result}') | |
if len(numbers) == 2: | |
op_symbol = {'add': '+', 'subtract': '-', 'multiply': '×', 'divide': '÷'}.get(operation, '?') | |
title = f"{numbers[0]} {op_symbol} {numbers[1]} = {result}" | |
else: | |
title = f"Result: {result}" | |
ax.set_title(title, fontsize=14, fontweight='bold') | |
ax.legend(loc='upper right') | |
ax.set_ylabel('') | |
ax.set_xlabel('Number Line') | |
ax.grid(True, alpha=0.3) | |
return fig | |