Lar159 commited on
Commit
08dce2b
·
verified ·
1 Parent(s): e00c796

Create graph_plotter_tool

Browse files
Files changed (1) hide show
  1. tools/graph_plotter_tool +40 -0
tools/graph_plotter_tool ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import matplotlib.pyplot as plt
3
+ from smolagents.tools import tool
4
+ import time
5
+
6
+ @tool
7
+ def plot_function_graph(function_string: str) -> str:
8
+ """
9
+ Gera e salva um gráfico de uma função matemática 2D.
10
+ Args:
11
+ function_string: A função a ser plotada, formatada como uma string Python (ex: 'x**2 - 2*x - 3'). Use 'x' como a variável.
12
+ Returns:
13
+ O caminho do arquivo da imagem PNG gerada.
14
+ """
15
+ try:
16
+ # Cria uma gama de valores para x
17
+ x = np.linspace(-10, 10, 400)
18
+
19
+ # Avalia a string da função para obter os valores de y
20
+
21
+ y = eval(function_string)
22
+
23
+ # Configura e cria o gráfico
24
+ plt.figure(figsize=(8, 6))
25
+ plt.plot(x, y)
26
+ plt.title(f"Gráfico de y = {function_string}")
27
+ plt.xlabel("x")
28
+ plt.ylabel("y")
29
+ plt.grid(True)
30
+ plt.axhline(0, color='black',linewidth=0.5)
31
+ plt.axvline(0, color='black',linewidth=0.5)
32
+
33
+ # Salva o gráfico em um arquivo com nome único
34
+ file_path = f"grafico_{int(time.time())}.png"
35
+ plt.savefig(file_path)
36
+ plt.close() # Fecha a figura para liberar memória
37
+
38
+ return f"Gráfico gerado e salvo em: {file_path}"
39
+ except Exception as e:
40
+ return f"Erro ao gerar o gráfico: {str(e)}"