|
import numpy as np |
|
import matplotlib.pyplot as plt |
|
from PIL import Image |
|
|
|
def plot_equation(equation, **args): |
|
|
|
equation = str(equation) |
|
equation = equation.replace('"', '').replace("'", '') |
|
m, c = map(float, equation.split('x + ')) |
|
|
|
|
|
x = np.linspace(-5, 5, 100) |
|
|
|
|
|
y = m * x + c |
|
|
|
|
|
plt.figure(figsize=(6, 6)) |
|
plt.plot(x, y, color='blue', linewidth=2) |
|
plt.xlabel('x') |
|
plt.ylabel('y') |
|
plt.title(f'Plot of {equation}') |
|
plt.grid(True, linestyle='--', alpha=0.7) |
|
plt.xlim(-5, 5) |
|
plt.ylim(-5, 5) |
|
plt.gca().set_aspect('equal', adjustable='box') |
|
random_num = random.randint(1, 1000) |
|
fig = plt.gcf() |
|
fig.canvas.draw() |
|
image = Image.frombytes('RGB', fig.canvas.get_width_height(), fig.canvas.tostring_rgb()) |
|
|
|
|
|
plt.close() |
|
img_name = f"graph{random_num}.jpg" |
|
image.save(img_name) |
|
|
|
output_dict = { |
|
"llm_output": f"Done. The graph is plotted and displayed.", |
|
"display": {"files": [img_name], "metadata": equation}, |
|
"type": "image", |
|
} |
|
return output_dict |