Spaces:
Sleeping
Sleeping
| import numpy as np | |
| import matplotlib.pyplot as plt | |
| from smolagents.tools import tool | |
| import time | |
| from typing import Optional, List, Dict | |
| def plot_line_chart(x_values: List, y_values: List[float], title: str = "Gráfico de Linhas", x_label: str = "Eixo X", y_label: str = "Eixo Y") -> str: | |
| """ | |
| Gera e salva um gráfico de linhas, ideal para mostrar tendências ao longo do tempo. | |
| Args: | |
| x_values: Uma lista de rótulos ou números para o eixo X (ex: meses, anos). | |
| y_values: Uma lista de números (int ou float) para o eixo Y. | |
| title: O título do gráfico. | |
| x_label: O rótulo do eixo X. | |
| y_label: O rótulo do eixo Y. | |
| Returns: | |
| O caminho do arquivo da imagem PNG gerada. | |
| """ | |
| try: | |
| if len(x_values) != len(y_values): | |
| return "Erro: As listas x_values e y_values devem ter o mesmo tamanho." | |
| plt.figure(figsize=(10, 6)) | |
| plt.plot(x_values, y_values, marker='o', linestyle='-') | |
| plt.title(title) | |
| plt.xlabel(x_label) | |
| plt.ylabel(y_label) | |
| plt.grid(True, which='both', linestyle='--', linewidth=0.5) | |
| plt.tight_layout() | |
| file_path = f"line_chart_{int(time.time_ns())}.png" | |
| plt.savefig(file_path) | |
| plt.close() | |
| return f"Gráfico de linhas gerado e salvo em: {file_path}" | |
| except Exception as e: | |
| return f"Erro ao gerar o gráfico de linhas: {str(e)}" | |
| def plot_bar_chart(labels: List[str], values: List[float], title: str = "Gráfico de Barras", x_label: str = "Categorias", y_label: str = "Valores", y_lim: Optional[List[float]] = None) -> str: | |
| """ | |
| Gera e salva um gráfico de barras. | |
| Args: | |
| labels: Uma lista de strings para os rótulos de cada barra. | |
| values: Uma lista de números (int ou float) correspondendo ao valor de cada barra. | |
| title: O título do gráfico. | |
| x_label: O rótulo do eixo X. | |
| y_label: O rótulo do eixo Y. | |
| y_lim: Opcional. Uma lista [min, max] para definir o limite do eixo Y, útil para criar distorções. | |
| Returns: | |
| O caminho do arquivo da imagem PNG gerada. | |
| """ | |
| try: | |
| plt.figure(figsize=(10, 6)) | |
| plt.bar(labels, values) | |
| plt.title(title) | |
| plt.xlabel(x_label) | |
| plt.ylabel(y_label) | |
| plt.xticks(rotation=45, ha="right") | |
| plt.grid(axis='y', linestyle='--', alpha=0.7) | |
| plt.tight_layout() | |
| if y_lim: | |
| plt.ylim(y_lim) | |
| file_path = f"bar_chart_{int(time.time_ns())}.png" | |
| plt.savefig(file_path) | |
| plt.close() | |
| return f"Gráfico de barras gerado e salvo em: {file_path}" | |
| except Exception as e: | |
| return f"Erro ao gerar o gráfico de barras: {str(e)}" | |
| def plot_pie_chart(labels: List[str], sizes: List[float], title: str = "Gráfico de Setores") -> str: | |
| """ | |
| Gera e salva um gráfico de pizza (setores). | |
| Args: | |
| labels: Uma lista de strings para os rótulos de cada fatia. | |
| sizes: Uma lista de números (int ou float) correspondendo ao tamanho de cada fatia. | |
| title: O título do gráfico. | |
| Returns: | |
| O caminho do arquivo da imagem PNG gerada. | |
| """ | |
| try: | |
| plt.figure(figsize=(8, 8)) | |
| plt.pie(sizes, labels=labels, autopct='%1.1f%%', startangle=140, shadow=True) | |
| plt.title(title) | |
| plt.axis('equal') # Assegura que o gráfico seja um círculo. | |
| file_path = f"pie_chart_{int(time.time_ns())}.png" | |
| plt.savefig(file_path) | |
| plt.close() | |
| return f"Gráfico de setores gerado e salvo em: {file_path}" | |
| except Exception as e: | |
| return f"Erro ao gerar o gráfico de setores: {str(e)}" | |
| def plot_histogram(data: List[float], bins: int = 10, title: str = "Histograma", x_label: str = "Valores", y_label: str = "Frequência") -> str: | |
| """ | |
| Gera e salva um histograma a partir de um conjunto de dados. | |
| Args: | |
| data: Uma lista de números (int ou float) para compor o histograma. | |
| bins: O número de 'caixas' (colunas) no histograma. | |
| title: O título do gráfico. | |
| x_label: O rótulo do eixo X. | |
| y_label: O rótulo do eixo Y. | |
| Returns: | |
| O caminho do arquivo da imagem PNG gerada. | |
| """ | |
| try: | |
| plt.figure(figsize=(10, 6)) | |
| plt.hist(data, bins=bins, edgecolor='black') | |
| plt.title(title) | |
| plt.xlabel(x_label) | |
| plt.ylabel(y_label) | |
| plt.grid(axis='y', linestyle='--', alpha=0.7) | |
| file_path = f"histogram_{int(time.time_ns())}.png" | |
| plt.savefig(file_path) | |
| plt.close() | |
| return f"Histograma gerado e salvo em: {file_path}" | |
| except Exception as e: | |
| return f"Erro ao gerar o histograma: {str(e)}" |