[Python] meu bot do telegram com matplotlib não funciona

Discussão em 'Python' iniciado por Stack, Setembro 2, 2025.

  1. Stack

    Stack Membro Participativo

    Meu codigo ta dando erro, eu não consigo mostrar o gráfico em um bot de telegram, que ele quebra o código


    import matplotlib.pyplot as plt
    ​

    def gerar_grafico(despesas): if not despesas: return None categorias = {} for d in despesas: categorias[d['categoria']] = categorias.get(d['categoria'], 0) + float(d['valor'])

    labels = list(categorias.keys())
    valores = list(categorias.values())

    plt.figure(figsize=(6,6))
    plt.pie(valores, labels=labels, autopct='%1.1f%%')
    plt.title('Distribuição das Despesas por Categoria')
    caminho_arquivo = 'grafico_despesas.png'
    plt.savefig(caminho_arquivo)
    plt.close()
    return caminho_arquivo


    porém, da esse erro

    2025-09-02 19:23:12,989 (init.py:1115 MainThread) ERROR - TeleBot: "Infinity polling exception: could not convert string to float: 'Onibus'" 2025-09-02 19:23:12,995 (init.py:1117 MainThread) ERROR - TeleBot: "Exception traceback: Traceback (most recent call last): File "C:\Users\Paulo\Downloads\bot.venv\Lib\site-packages\telebot_init_.py", line 1110, in infinity_polling self.polling(non_stop=True, timeout=timeout, long_polling_timeout=long_polling_timeout, ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ logger_level=logger_level, allowed_updates=allowed_updates, restart_on_change=False, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ *args, **kwargs) ^^^^^^^^^^^^^^^^ File "C:\Users\Paulo\Downloads\bot.venv\Lib\site-packages\telebot_init_.py", line 1198, in polling self._threaded_polling(non_stop=non_stop, interval=interval, timeout=timeout, long_polling_timeout=long_polling_timeout, ~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ logger_level=logger_level, allowed_updates=allowed_updates) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Paulo\Downloads\bot.venv\Lib\site-packages\telebot_init.py", line 1273, in _threaded_polling raise e File "C:\Users\Paulo\Downloads\bot.venv\Lib\site-packages\telebot_init.py", line 1235, in _threaded_polling self.worker_pool.raise_exceptions() ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^ File "C:\Users\Paulo\Downloads\bot.venv\Lib\site-packages\telebot\util.py", line 154, in raise_exceptions raise self.exception_info File "C:\Users\Paulo\Downloads\bot.venv\Lib\site-packages\telebot\util.py", line 97, in run task(*args, **kwargs) ~~~~^^^^^^^^^^^^^^^^^ File "C:\Users\Paulo\Downloads\bot.venv\Lib\site-packages\telebot_init.py", line 10134, in _run_middlewares_and_handler result = handler'function' File "c:\Users\Paulo\Downloads\bot\comandos.py", line 88, in enviar_grafico caminho_arquivo = gerar_grafico(despesas_dict) File "c:\Users\Paulo\Downloads\bot\grafico.py", line 8, in gerar_grafico categorias[d['categoria']] = categorias.get(d['categoria'], 0) + float(d['valor']) ~~~~~^^^^^^^^^^^^ ValueError: could not convert string to float: 'Onibus' "

    Continue reading...

Compartilhe esta Página