Spaces:
Sleeping
Sleeping
| import numpy as np | |
| import matplotlib.pyplot as plt | |
| from smolagents.tools import tool | |
| import time | |
| def plot_function_graph(function_string: str) -> str: | |
| """ | |
| Gera e salva um gráfico de uma função matemática 2D. | |
| Args: | |
| 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. | |
| Returns: | |
| O caminho do arquivo da imagem PNG gerada. | |
| """ | |
| try: | |
| # Cria uma gama de valores para x | |
| x = np.linspace(-10, 10, 400) | |
| # Avalia a string da função para obter os valores de y | |
| y = eval(function_string) | |
| # Configura e cria o gráfico | |
| plt.figure(figsize=(8, 6)) | |
| plt.plot(x, y) | |
| plt.title("Gráfico da Função") | |
| plt.xlabel("x") | |
| plt.ylabel("y") | |
| plt.grid(True) | |
| plt.axhline(0, color='black',linewidth=0.5) | |
| plt.axvline(0, color='black',linewidth=0.5) | |
| # Salva o gráfico em um arquivo com nome único | |
| file_path = f"grafico_{int(time.time_ns())}.png" | |
| plt.savefig(file_path) | |
| plt.close() # Fecha a figura para liberar memória | |
| return f"Gráfico gerado e salvo em: {file_path}" | |
| except Exception as e: | |
| return f"Erro ao gerar o gráfico: {str(e)}" |