File size: 1,226 Bytes
9873954
 
 
 
9c21c62
9873954
9c21c62
 
9873954
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53cd8f6
9873954
 
 
 
 
 
53cd8f6
 
9873954
 
46a492c
a4afeef
9873954
 
 
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
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image

def plot_equation(equation, **args):
    # Extracting m and c from the equation string
    equation = str(equation)
    equation = equation.replace('"', '').replace("'", '')
    m, c = map(float, equation.split('x + '))

    # Generating x values
    x = np.linspace(-5, 5, 100)

    # Calculating y values
    y = m * x + c

    # Creating the plot
    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())

    # Close the plot
    plt.close()
    img_name = f"graph{random_num}.jpg"
    image.save(img_name)
    # Returning the image
    output_dict = {
        "llm_output": f"Done. The graph is plotted and displayed.",
        "display": {"files": [img_name], "metadata": equation},
        "type": "image",
    }
    return output_dict