newer_project / modules /graph_plotter.py
YaTharThShaRma999's picture
Update modules/graph_plotter.py
9c21c62 verified
raw
history blame contribute delete
No virus
1.23 kB
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